簡體   English   中英

帶有GridLayoutManager的RecyclerView和帶有LinearLayoutManager的RecyclerView

[英]RecyclerView with GridLayoutManager inside RecyclerView with LinearLayoutManager

我基本上是在嘗試實現這一設計原則( 來自Google的Material Design ): 在此處輸入圖片說明 因此,我已經使用LinearLayoutManager制作了一個父RecyclerView ,然后在RecyclerView適配器中為“富媒體”部分(操作區域2)添加了一個帶有GridLayoutManager的子RecyclerView 一切正常,除了我將內部RecyclerView網格設置為具有match_parent寬度和wrap_content高度的事實外,但它無法正確計算內容的大小,似乎將其保留為0並因此被隱藏了。 如果我將子級RecyclerView設置為特定高度,則項目會顯示在其中,但是當然會在底部將其切除。 其他人似乎也遇到了這個問題, 但是在他們的情況下,它們都具有線性布局 (另請參閱此處的“凱”的答案 。)

現在我的問題是,如何在上面鏈接的問題的公認答案中,像在“ pptang”中那樣覆蓋onMeasure方法,但是要在自定義GridLayoutManager而不是自定義LinearLayoutManager 我沒有在這里發布我的代碼,因為它基本上與鏈接的代碼相同,只是我需要為子級RecyclerView制作一個自定義GridLayoutManager ,以便它可以正確地作為“ pptang's”答案狀態進行度量。

否則,有沒有比在第二個RecyclerView中使用1個RecyclerView更好的方法? 只能有1個RecyclerView用上面的CardViews列表和每個CardView的唯一項網格填充活動/片段嗎?

您可以使用SectionedRecyclerViewAdapter庫僅使用一個RecyclerView來構建它。

您可以在下面找到圖像示例的完整代碼。

在此處輸入圖片說明

首先創建一個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);
    }
}

然后,使用您的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);

總結一下。 您不應該在回收站內部使用回收站。 您需要實現一個自定義的gridLayoutManager。 為此,請閱讀以下內容:

從文檔https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ItemDecoration.html

ItemDecoration允許應用程序從適配器的數據集向特定的項目視圖添加特殊的圖形和布局偏移。 這對於在項目,突出顯示, 可視分組邊界等之間繪制分隔線很有用。

因此,如果將以上內容與此http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html結合使用,您肯定可以實現所需的功能。 只需將您從材質指南中呈現的圖像想象成一個在gridLayoutManager中的組。

  • 您可以有不同類型的視圖
  • 每行可能有多個視圖
  • 每行的裝飾方式可能不同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM