Is there a way to share the same LayoutManager between multiple nested RecyclerViews

I am developing an application showing a list of games, inside each itemView item, I also have a list of videos to be displayed. preview and structure.

enter image description here

I deploy RecyclerView as a view of the root window, and then for the video, I used the RecyclerView grid-style to display, so here we got the nested RecyclerView structure.

But there are problems, because the number of videos is incompatible between games, I donโ€™t want RecyclerView from the list of videos to scroll, so the best way is to stretch the viewing height dynamically, it depends on how many lines it has. I heard a disappointing conclusion that there isnโ€™t a way to achieve this from the internet. It's true? can any alternative do this?

So far, I am not going to solve this issue, so I manually change the height of the RecyclerView as a temporary solution.

videoGridRecyclerView.getLayoutParams().height = rowCount * 242; 

Given that video streams have the same structure, it would be nice if these RecyclerViews could reuse this element of a video ad when they are already scrolling from the screen, this should significantly improve performance.

Then I tried to propagate a specific RecycledViewPool to each nested RecyclerViews.

 public class MainActivity extends Activity { private RecyclerView.RecycledViewPool mViewPool; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // create the RecycledViewPool from first and re-use for each nested RecyclerViews. mViewPool = new RecyclerView.RecycledViewPool(); ... } } 

Even I made this effort, I notice all the time that the onCreateViewHolder executable method and incrementing inflation counter increase while scrolling through the root RecyclerView in a circular way.

 private static class VideoAdapter extends RecyclerView.Adapter<GridViewHolder> { private List<Video> mVideoList; private VideoAdapter(List<Video> videoList) { mVideoList = videoList; } // this method would always invoke during i swipe the RecyclerView. @Override public GridViewHolder onCreateViewHolder(ViewGroup container, int viewType) { Log.e("", String.format("createViewCount : %d", ++MainActivity.STAT_GAME_VIDEO_ITEM_COUNT)); return new GridViewHolder(inflate(container, R.layout.game_video_item)) {}; } } 

Will my idea work? or am i doing it wrong? I want to achieve this effect, any other advice, even if it is not about RecyclerView, will be grateful, thanks in advance.

+5
source share
2 answers

So far, I have used GridLayout instead of the nested RecyclerView and redesigned the views that are inside the GridLayout themselves. But this is probably not a very good way, because the processing logic was very simple. The app still stutters a bit when scrolling fast, I donโ€™t understand. As a viable ending, I am pushing my code on github . Please advise me if you have any ideas, thanks.

+3
source

Yes, what you are doing is right.

Just create a RecyclerViewPool (as you did) and pass it to all RecyclerView instances via setRecycledViewPool . This way they will use the same pool of views.

You can increase the size of your cache to avoid hacking Views when the number of views between elements varies greatly.

Btw, as a side sentence, try combining it into one RecyclerView instead of nested RecyclerViews. Thus, animations, etc. They can work much better. In addition, scrolling in one direction is problematic when you use the RV inside the RV, and you can avoid this problem by combining them into one.

+2
source

Source: https://habr.com/ru/post/1206099/


All Articles