Answer the question
In order to leave comments, you need to log in
How to use ViewPager inside RecyclerView items?
How can I get the ViewPager to work correctly in the RecyclerView elements of the ViewPager list? Created a small test application to demonstrate the problem:
In the screenshot RecyclerView + LinearManager with absolutely identical (in theory) elements in the amount of 5 pieces. However, only the first element works correctly (shows fragments inside the ViewPager). Each element contains only ViewPager. I set the background color of the ViewPager itself to red. It has a FragmentPagerAdapter connected to it that creates blue background fragments.
Code: https://gist.github.com/gim-/9336a2aa05d7db96de562...
Whole project: ViewPagerInsideRecyclerView.zip
Already spent quite a lot of time trying to understand what is happening, but so far to no avail. Anyone met with something like this? What are the options for solving the problem?
UPD:
If you do not set the PagerAdapter to the first element in the onBindViewHolder method of the RecyclerView adapter, then all list elements will be red:
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
FragmentManager fragmentManager = ((AppCompatActivity) mContext).getSupportFragmentManager();
if (position == 0) return;
holder.viewPager.setAdapter(new MyPagerAdapter(fragmentManager));
}
Answer the question
In order to leave comments, you need to log in
After long painful dances with a tambourine, I finally managed to find a solution. In the onBindViewHolder method of the RecyclerView adapter, you need to assign a unique ID to the ViewPager. I did this:
The internal magic of Android only understands that the pagers in each element of the list are different.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question