在notifyItemChanged之后,TabLayout跳到其威力的起点

问题描述

我正在构建测验应用程序,并且始终无法将更新的项目通知RecyclerView。 无论如何,DiffUtil对我不起作用-我可以使用adapter.notifyItemChanged(),但是在此调用之后,我的TabLayout跳转到其宽度的开始。 我已将onClickListener设置为我的floatButton,以确保它是notifyItemChanged()问题。 我在做错什么吗?

片段

class TestFragment : Fragment() {
    private lateinit var viewmodel: Testviewmodel
    override fun onCreateView(
        inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View? {
        val binding: FragmentTestBinding = DataBindingUtil.inflate(
            inflater,R.layout.fragment_test,container,false
        )
        val args = TestFragmentArgs.fromBundle(requireArguments())
        val viewmodelFactory = TestviewmodelFactory(args.course)
        viewmodel = viewmodelProvider(this,viewmodelFactory).get(Testviewmodel::class.java)
        binding.viewmodel = viewmodel
        binding.lifecycleOwner = viewLifecycleOwner
        val adapter = PagerAdapter(ClickListener { questionDatabase: QuestionDatabase,string: String ->
            val questionUpdated = questionDatabase.apply { answer = string }
            viewmodel.updateAnswer(questionDatabase,questionUpdated)
        })
        binding.viewPager.adapter = adapter
        TabLayoutMediator(binding.tabLayout,binding.viewPager) { tab: TabLayout.Tab,i: Int ->
            tab.text = (i + 1).toString()
        }.attach()
        viewmodel.pytania.observe(viewLifecycleOwner,Observer {
            adapter.differ.submitList(it)
        })

        viewmodel.score.observe(viewLifecycleOwner,Observer {
            if (it != null) {
                findNavController().navigate(
                    TestFragmentDirections.actionTestFragmentToscoreFragment(it)
                )
                viewmodel.score.value = null
            }
        })
        binding.bttnFinish.setonClickListener {
            adapter.notifyItemChanged(binding.viewPager.currentItem)
        }
        return binding.root
    }
}

viewmodel

class Testviewmodel(
    test: String
): viewmodel() {
    private val _pytania = mutablelivedata<List<QuestionDatabase>>()
    val pytania: LiveData<List<QuestionDatabase>>
        get() = _pytania
    val score = mutablelivedata<Int?>()
    init {
        val listofQuestions = mutablelistof<QuestionDatabase>()
        for (x in 1..15) {
            listofQuestions.add(
                QuestionDatabase(
                    question = "Question $x",answerA = "AnswerA",answerB = "AnswerB",answerC = "AnswerC",answerD = "AnswerD",correctAnswer = "AnswerD",image = 0,questionNumber = 0
                )
            )
        }
        _pytania.value = listofQuestions.toList()
    }
    fun updateAnswer(questionDatabase: QuestionDatabase,questionUpdated: QuestionDatabase) {
    _pytania.value = _pytania.value?.replace(questionDatabase,questionUpdated)
    }
}

ViewPagerAdapter

class PagerAdapter(
    private val clickListener: ClickListener
) : RecyclerView.Adapter<ViewHolder>() {
    private val differCallBack = object : DiffUtil.ItemCallback<QuestionDatabase>() {
        override fun areItemsTheSame(oldItem: QuestionDatabase,newItem: QuestionDatabase): Boolean {
            return oldItem.question == newItem.question
        }

        override fun areContentsTheSame(oldItem: QuestionDatabase,newItem: QuestionDatabase): Boolean {
            return oldItem == newItem
        }
    }
    val differ = AsyncListDiffer(this,differCallBack)
    override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): ViewHolder {
        return ViewHolder.from(parent)
    }

    override fun onBindViewHolder(holder: ViewHolder,position: Int) {
        holder.bind(differ.currentList[position],clickListener)
    }

    override fun getItemCount(): Int {
        return differ.currentList.size
    }
}
class ViewHolder private constructor(val binding: VpItemQuestionBinding): RecyclerView.ViewHolder(
    binding.root
) {
    fun bind(
        question: QuestionDatabase,clickListener: ClickListener
    ) {
        binding.question = question
        binding.onClickListener = clickListener
        binding.executePendingBindings()
    }
    companion object {
        fun from(parent: ViewGroup): ViewHolder {
            val layoutInflater = LayoutInflater.from(parent.context)
            val binding = VpItemQuestionBinding.inflate(layoutInflater,parent,false)
            return ViewHolder(binding)
        }
    }
}
class ClickListener(val clickListener: (QuestionDatabase,string: String) -> Unit) {
    fun onClick(question: QuestionDatabase,string: String) = clickListener(question,string)
}

在notifyItemChanged()之前

before onClick

notifyItemChanged()之后

after onClick

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)