简体   繁体   English

带有GridLayoutManager的RecyclerView和带有LinearLayoutManager的RecyclerView

[英]RecyclerView with GridLayoutManager inside RecyclerView with LinearLayoutManager

I am basically trying the achieve this design principle ( from Google's Material Design ): 我基本上是在尝试实现这一设计原则( 来自Google的Material Design ): 在此处输入图片说明 Thus I've made a parent RecyclerView with a LinearLayoutManager , then in the RecyclerView adapter, I've put the child RecyclerView with a GridLayoutManager for the "rich media" section (Action area 2). 因此,我已经使用LinearLayoutManager制作了一个父RecyclerView ,然后在RecyclerView适配器中为“富媒体”部分(操作区域2)添加了一个带有GridLayoutManager的子RecyclerView Everything works fine, except for the fact that I've set the internal RecyclerView grid to have a match_parent width and a wrap_content height, but it won't calculate the size of the content properly, seemingly leaving it at 0 & thus hidden. 一切正常,除了我将内部RecyclerView网格设置为具有match_parent宽度和wrap_content高度的事实外,但它无法正确计算内容的大小,似乎将其保留为0并因此被隐藏了。 If I set the child RecyclerView to a specific height, the items show within, but are then of course cut off at the bottom. 如果我将子级RecyclerView设置为特定高度,则项目会显示在其中,但是当然会在底部将其切除。 Others seem to have come across this problem, but in their case, both have linear layouts . 其他人似乎也遇到了这个问题, 但是在他们的情况下,它们都具有线性布局 (Also see "Khay's" answer here .) (另请参阅此处的“凯”的答案 。)

Now my question is, how would one override the onMeasure method as "pptang" did in the accepted answer of the linked question above, but within a custom GridLayoutManager instead of a custom LinearLayoutManager ? 现在我的问题是,如何在上面链接的问题的公认答案中,像在“ pptang”中那样覆盖onMeasure方法,但是要在自定义GridLayoutManager而不是自定义LinearLayoutManager I haven't posted my code here, because it's essentially the identical to the one linked, only that I need to make a custom GridLayoutManager instead for the child RecyclerView , so that it measures correctly as "pptang's" answer states. 我没有在这里发布我的代码,因为它基本上与链接的代码相同,只是我需要为子级RecyclerView制作一个自定义GridLayoutManager ,以便它可以正确地作为“ pptang's”答案状态进行度量。

Otherwise, is there a better way than to use 1 RecyclerView inside a 2nd RecyclerView? 否则,有没有比在第二个RecyclerView中使用1个RecyclerView更好的方法? Can only 1 RecyclerView populate an activity/fragment both with a list of the above CardViews and a grid of unique items within each CardView ? 只能有1个RecyclerView用上面的CardViews列表和每个CardView的唯一项网格填充活动/片段吗?

You can build it with only one RecyclerView using the library SectionedRecyclerViewAdapter . 您可以使用SectionedRecyclerViewAdapter库仅使用一个RecyclerView来构建它。

You can find the full code for the example of the image below here . 您可以在下面找到图像示例的完整代码。

在此处输入图片说明

First create a Section class: 首先创建一个Section类:

class MySection extends StatelessSection {

    String title;
    String subtitle;
    List<String> list;

    public MySection(String title, String subtitle, List<String> list) {
        // call constructor with layout resources for this Section header, footer and items 
        super(R.layout.section_header, R.layout.section_item);

        this.title = title;
        this.subtitle = subtitle;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(list.get(position));
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new SimpleHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

        // bind your header view here
        headerHolder.tvTitle.setText(title);
        headerHolder.tvSubTitle.setText(subtitle);
    }
}

Then you set up the RecyclerView with your Sections: 然后,使用您的Sections设置RecyclerView:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data for each year
MySection section1 = new MySection("Title", "Subhead", firstDataList);

// Add your Sections to the adapter
sectionAdapter.addSection(section1);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
GridLayoutManager glm = new GridLayoutManager(getContext(), 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        switch(sectionAdapter.getSectionItemViewType(position)) {
            case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
                return 2;
            default:
                return 1;
        }
    }
});
recyclerView.setLayoutManager(glm);
recyclerView.setAdapter(sectionAdapter);

To sum up. 总结一下。 You shouldn't use recycler inside of a recycler. 您不应该在回收站内部使用回收站。 You need to implement a custom gridLayoutManager. 您需要实现一个自定义的gridLayoutManager。 To achieve this read these: 为此,请阅读以下内容:

From docs https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration.html 从文档https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration.html

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. ItemDecoration允许应用程序从适配器的数据集向特定的项目视图添加特殊的图形和布局偏移。 This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more. 这对于在项目,突出显示, 可视分组边界等之间绘制分隔线很有用。

So if you use the above along with this http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html you can definitely achieve what you are looking for. 因此,如果将以上内容与此http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html结合使用,您肯定可以实现所需的功能。 Just think of the image you presented from material guidelines as a group in your gridLayoutManager. 只需将您从材质指南中呈现的图像想象成一个在gridLayoutManager中的组。

  • You can have different types of views 您可以有不同类型的视图
  • Each row might have multiple views 每行可能有多个视图
  • Each row might be decorated differently 每行的装饰方式可能不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM