簡體   English   中英

在 notifyDataSetChanged() 之后 EditText 失去焦點

[英]EditText lose focus after notifyDataSetChanged()

我嘗試使用此答案https://stackoverflow.com/a/9528547/13368783

editText.setOnFocusChangeListener { editText, focus ->
                    testDataObject.fieldHasFocus = focus
                }

但它不起作用,因為android在notifyDataSetChanged之后清除焦點並且焦點始終為false。 我可能錯誤地實施了答案。 而這個https://stackoverflow.com/a/42137741/13368783但這也不起作用

我也嘗試延遲然后設置焦點。

      GlobalScope.launch(Dispatchers.Main) {
           delay(1000)
           editText.requestFocus()
      }

但效果很差

完整代碼github

適配器

package com.`package`.viewpagertest

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.RecyclerView
import by.lwo.viewpagertest.R
import by.lwo.viewpagertest.databinding.HolderTestBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class ViewPagerAdapter(var list: List<TestDataObject>) : RecyclerView.Adapter<ViewPagerAdapter.TestHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.holder_test, parent, false)
        val binding: HolderTestBinding = DataBindingUtil.bind(view)!!
        return TestHolder(binding)
    }

    override fun onBindViewHolder(holder: TestHolder, position: Int) {
        holder.bind(list[position])
    }

    override fun getItemCount(): Int = list.size

    fun updateData(items: List<TestDataObject>) {
        list = items
        notifyDataSetChanged()
    }

    inner class TestHolder(private val binding: HolderTestBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(testDataObject: TestDataObject) {
            binding.apply {
                titleTv.text = testDataObject.titleText
                textTv.text = testDataObject.infoText
                editText.setOnFocusChangeListener { editText, focus ->
                    testDataObject.fieldHasFocus = focus
                }
                if(testDataObject.fieldHasFocus) editText.requestFocus()
//                GlobalScope.launch(Dispatchers.Main) {
//                    delay(1000)
//                    editText.requestFocus()
//                }
            }
        }
    }

}

data class TestDataObject(val titleText: String = "", val infoText: String = "", var fieldHasFocus: Boolean = false)

主要活動

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import by.lwo.viewpagertest.databinding.ActivityMainBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.random.Random

class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding
    private var adapter: ViewPagerAdapter? = null
    val testList: MutableList<TestDataObject> = emptyList<TestDataObject>().toMutableList()

    private val _testLiveData = MutableLiveData<Event<List<TestDataObject>>>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setDataToList()
        setAdapter()
        constantlyUpdateList()

        _testLiveData.observeEvent(this) {
            adapter?.updateData(it)
            constantlyUpdateList()
        }


    }

    private fun constantlyUpdateList() {
        lifecycleScope.launch {
            delay(5000)
            setDataToList()
            _testLiveData.value = Event(testList)
        }
    }

    private fun setDataToList() {
        testList.clear()
        repeat(6) {
            testList.add(
                TestDataObject(
                    titleText = "title for page $it ${Random.nextInt()}",
                    infoText = "infoText for page $it ${Random.nextInt()}"
                )
            )
        }
    }

    private fun setAdapter(){
        binding.apply {
            adapter = ViewPagerAdapter(testList)
            viewPager.adapter = adapter
            binding.viewPager.offscreenPageLimit = testList.size
        }
    }
}

你需要在片段中設置

viewPagerAdapter.setHasStableIds(true)

分段

private fun setAdapter(){
        binding.apply {
            val viewPagerAdapter = ViewPagerAdapter(testList)
            viewPagerAdapter.setHasStableIds(true)
            viewPager.adapter = viewPagerAdapter
            (binding.viewPager.getChildAt(0) as RecyclerView).itemAnimator = null // Add if the item is blinking
        }
    }

並在您的 ViewPagerAdapter 中覆蓋 getItemId

適配器

override fun getItemId(position: Int) = position.toLong()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM