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()
|
}
|
}
|