I tried to use this answer https://stackoverflow.com/a/9528547/13368783
editText.setOnFocusChangeListener { editText, focus ->
testDataObject.fieldHasFocus = focus
}
but it doesn't work because android clears focus after notifyDataSetChanged and focus is always false. I may have implemented the answer incorrectly. And this https://stackoverflow.com/a/42137741/13368783 but that also doesn't work
I also tried to make a delay and then set focus.
GlobalScope.launch(Dispatchers.Main) {
delay(1000)
editText.requestFocus()
}
But it works very badly
full code github
Adapter
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)
MainActivity
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
}
}
}
you need set in fragment
viewPagerAdapter.setHasStableIds(true)
Fragment
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
}
}
and override getItemId in your ViewPagerAdapter
Adapter
override fun getItemId(position: Int) = position.toLong()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.