I used recyclerview
inside viewpager
. Now I want to change the view of my fragment
when I scroll the recycler view.
I am using mvvm
and liveData
and I want to use viewModel
and send the scroll data to my viewModel
and in view
I observe
the viewModel
data that comes from viewPager
adapter.
How can I send the data from viewPager
adapter and use them in its fragment
?
val linearLayoutManager =
LinearLayoutManager(container.context, LinearLayoutManager.VERTICAL, false)
recCafeList = binding.recCafeList
recCafeList.layoutManager = linearLayoutManager
recCafeList.adapter = mAdapter
recCafeList.setOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val po = linearLayoutManager.findFirstVisibleItemPosition()
val po2 = linearLayoutManager.findFirstCompletelyVisibleItemPosition()
// I want to send @po and @po2 to viewModel and use them in fragment
Log.i("scrollListener", "$po //// $po2")
}
})
container.addView(binding.root)
return binding.root
You can use custom Listener. Create it inside the fragment and transfer it to pagers adapter. And in any time you can call listeners method. Like that - Android communication between fragment and baseadapter
It is easy and nice practice solution.