lmw
2023-06-13 adf8013576cbdd12e5ebea8ff7e32baf5d558b27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.kuanzhai.user.ui.mine.adapter
 
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import androidx.core.os.bundleOf
import cn.sinata.xldutils.activity.BaseActivity
import cn.sinata.xldutils.adapter.HFRecyclerAdapter
import cn.sinata.xldutils.adapter.util.ViewHolder
import com.kuanzhai.user.R
import com.kuanzhai.user.dialog.TipDialog
import com.kuanzhai.user.network.entity.CloseAccount
import com.kuanzhai.user.network.HttpManager
import com.kuanzhai.user.network.request
import java.util.ArrayList
 
class CloseAccountAdapter(data: ArrayList<CloseAccount>):HFRecyclerAdapter<CloseAccount>(data, R.layout.item_close_account) {
    var nameWatcher:TextWatcher? = null
    var phoneWatcher:TextWatcher? = null
 
    var foucPosition = 0 //焦点位置
 
    override fun onBind(holder: ViewHolder, position: Int, data: CloseAccount) {
        val etName = holder.bind<EditText>(R.id.et_name)
        val etPhone = holder.bind<EditText>(R.id.et_phone)
        val tvDel = holder.bind<View>(R.id.tv_del)
        val line = holder.bind<View>(R.id.line)
 
 
        if (data.id == null){ //编辑
            etName.isEnabled = true
            etPhone.isEnabled = true
            tvDel.visibility = if (position == 0) View.GONE else View.VISIBLE
        }else{//展示
            etName.isEnabled = false
            etPhone.isEnabled = false
            tvDel.visibility = View.VISIBLE
        }
        line.visibility = tvDel.visibility
 
        tvDel.setOnClickListener {
            if (data.id!=null&&data.id!=0){
                val tipDialog = TipDialog()
                tipDialog.arguments = bundleOf("msg" to "是否确认解绑${data.name}")
                tipDialog.setCallback(object :TipDialog.OnClickCallback{
                    override fun onOk() {
                        HttpManager.unbundleUserUser(data.id).request(context as BaseActivity,true,{_,_->
                            mData.removeAt(position)
                            if (mData.isEmpty())
                                mData.add(CloseAccount())
                            notifyDataSetChanged()
                        }){_,_->
                        }
                    }
 
                    override fun onCancel() {
                    }
                })
                tipDialog.show((context as BaseActivity).supportFragmentManager,"tip")
            }else{
                mData.removeAt(position)
                notifyDataSetChanged()
            }
        }
 
        if (nameWatcher == null){
            nameWatcher = object :TextWatcher{
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
 
                }
 
                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                }
 
                override fun afterTextChanged(s: Editable) {
                    if (etName.isEnabled)
                        mData[foucPosition].name = s.toString()
                }
            }
        }
        if (phoneWatcher == null){
            phoneWatcher = object :TextWatcher{
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
 
                }
 
                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                }
 
                override fun afterTextChanged(s: Editable) {
                    if (etPhone.isEnabled)
                        mData[foucPosition].content = s.toString()
                }
            }
        }
        etName.setText(data.name)
        etPhone.setText(data.content)
 
        etName.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus)
                foucPosition = position
        }
        etPhone.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus)
                foucPosition = position
        }
    }
 
    override fun onViewAttachedToWindow(holder: ViewHolder) {
        super.onViewAttachedToWindow(holder)
        val etName = holder.bind<EditText>(R.id.et_name)
        val etPhone = holder.bind<EditText>(R.id.et_phone)
        etName.addTextChangedListener(nameWatcher)
        etPhone.addTextChangedListener(phoneWatcher)
        if (foucPosition == holder.adapterPosition) {
            etName.requestFocus()
        }
    }
 
    override fun onViewDetachedFromWindow(holder: ViewHolder) {
        super.onViewDetachedFromWindow(holder)
        val etName = holder.bind<EditText>(R.id.et_name)
        val etPhone = holder.bind<EditText>(R.id.et_phone)
        //删除文字变化监听器
        etName.removeTextChangedListener(nameWatcher)
        etPhone.removeTextChangedListener(phoneWatcher)
        //清除焦点
        etName.clearFocus()
        etPhone.clearFocus()
    }
}