In my app, top bar contains radio button group of 2 radio buttons (feed, recent). below this, I have recyclerview(rv) which displays data coming from API.
if I click feed button, rv is loading feed data coming from API and If I click recent button, rv is loading recent data coming from API.
Now if I am scrolling down in feed section and go to recent and scroll down to recent section and coming back to feed section, it is reloading the data and last scrolled index is lost.
How to prevent this and display exactly same feed data before I switched to recent section?
code for HomeFragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
view_root = inflater.inflate(R.layout.fragment_home, container, false)
init()
mcontext = this.requireActivity()
prefHandler = PrefHandler(mcontext)
feedRecentRadioGroup.check(feedRecentRadioGroup.getChildAt(0).id)
setFeedData()
feedRecentRadioGroup.setOnCheckedChangeListener { group, checkedId ->
when (checkedId) {
R.id.feedRadioBtn -> {
setFeedData()
}
R.id.recentRadioBtn -> {
setRecentData()
}
}
}
...
setFeedData() and setRecentData() -> inside this functions, separate API call is made and it loads data into recyclerview -> working fine for both section.
layout of Homefragment
I suggest you to use TabLayout
to solve this problem.
https://developer.android.com/reference/com/google/android/material/tabs/TabLayout