问题
I'm developing an application that showing a game list, inside every game's itemView, i also have a video list to be showing. preview and structure are following.

I deploy a RecyclerView as the window root view, and then for the videos, i used grid-style RecyclerView to display, so here we got a nested RecyclerView structure.
But there is trouble because the number of videos is inconsistent between games, i don't want the RecyclerView of video list able to being scroll, so the best way is make the View's height stretch dynamically depends on how much rows it have. I heard a disappointing conclusion said there is no a way to accomplish this from internet. Is that really? any alternative can doing this?
So far about this question, i haven't idea to solve it, so i change the RecyclerView's height manually as a temporary solution.
videoGridRecyclerView.getLayoutParams().height = rowCount * 242;
Given that video items are completely same structure, it would be nice if those RecyclerViews can re-use that video's itemViews once they already swipe off with the screen, it shall takes much better performance improvement.
Then i tried to have a particular RecycledViewPool to propagating to every 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 done this effort, i keep notice that the onCreateViewHolder method being performed and the inflate counter getting increase during i swipe the root RecyclerView circularly.
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)) {};
}
}
Is this idea of mine will work? or i've doing it wrong? I want to achieve this effect, any other advices even if that aren't about RecyclerView would be appreciative, thanks in advance.
回答1:
By now, I used GridLayout instead of the nested RecyclerView, and recycling views which inside those GridLayout myself. But it is probably not a good way because my recycling logical was very simplicity. The application still a little bit stutter while fast scrolling, I haven't figure it out. As a viable ending, I push my code in github. Please advising me if you have ideas, thanks.
回答2:
Yes, what you are doing is right.
Just create a RecyclerViewPool (as you've done) and give it to all of your RecyclerView instances via setRecycledViewPool. This way, they'll share the same view pool.
You may prefer to increase its cache size to avoid churning Views when # of views between items differ a lot.
Btw, as a side suggestion, try to merge it into one RecyclerView instead of nested RecyclerViews. This way, animations etc can run much nicer. Also, scrolling at the same direction is problematic when you use RV inside RV and you can avoid that problem by merging them into one.
来源:https://stackoverflow.com/questions/26717928/is-there-a-way-to-share-a-same-layoutmanager-between-multiple-nested-recyclervie