Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

enter image description here

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.

like image 694
VinceStyling Avatar asked Nov 03 '14 15:11

VinceStyling


People also ask

What is nested recycler view?

A nested RecyclerView is an implementation of a RecyclerView within a RecyclerView. An example of such a layout can be seen in a variety of apps such as the Play store where the outer (parent) RecyclerView is of Vertical orientation whereas the inner (child) RecyclerViews are of horizontal orientations.

What is the purpose of the LayoutManager of the RecyclerView?

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

What are different types of Layout manager can RecyclerView use to give different directions for items?

RecyclerView provides these built-in layout managers: LinearLayoutManager shows items in a vertical or horizontal scrolling list. GridLayoutManager shows items in a grid. StaggeredGridLayoutManager shows items in a staggered grid.

What is Layout enlist Layout managers in android?

Layout Managers (or simply layouts) are said to be extensions of the ViewGroup class. They are used to set the position of child Views within the UI we are building. We can nest the layouts, and therefore we can create arbitrarily complex UIs using a combination of layouts.


1 Answers

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.

like image 193
VinceStyling Avatar answered Sep 22 '22 07:09

VinceStyling