5

I am using RecyclerView that displays different categories list. Each row item also contains RecyclerView to show category items' list. Parent RecyclerView is populated with vertial LinearLayoutManager and child RecyclerViews are populated with GridLayoutManager (2 columns). Child RecyclerViews are not showing all category items except first 2 even it has many category items. All remaining category items are hiding for specific category. In other words, you can say child RecyclerView is not expanding as much as items it contains.

I found different solution here but no one worked. That's why posting again.

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
momersaleem
  • 153
  • 2
  • 5
  • You need to use only one recycler view. Provide more code of image with result and I will try to explain more – Michael Spitsin Jul 20 '16 at 15:29
  • Even if you could have a nested recyclerview, you wouldn't be able to scroll it without some additional work. – OneCricketeer Jul 20 '16 at 16:22
  • 1
    You CAN have nested RecyclerViews (blew my mind when I heard this)...but this problem can't be solved without sharing code. – Booger Jul 20 '16 at 16:27
  • If I understand correctly, you want the top-level view to only support vertical scrolling and the children to _also_ only support vertical - is that right? A mockup or some sample code would really help here. – Cliabhach Jul 20 '16 at 16:31
  • https://stackoverflow.com/a/67779647/6314955 check this – Malith Kuruwita May 31 '21 at 20:03

1 Answers1

2

You can easily achieve it with only one RecyclerView using this library. See the image below:

enter image description here

First create a Section class:

class MySection extends StatelessSection {

    String title;
    List<String> list;

    public MySection(String title, 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.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 MyHeaderViewHolder(view);
    }

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

        // bind your header view here
        headerHolder.tvItem.setText(title);
    }
}

Then you set up the RecyclerView with your Sections:

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

// Create your sections with the list of data for each year
MySection section1 = new MySection("Categories 1", categories1DataList);
MySection section2 = new MySection("Categories 2", categories2DataList);

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

// 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);
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
  • I used this library and it worked great but little bit jerky. Now I want to add load more (want to load more records from server) functionality to this recyclerview list. How can I make it smooth and achieve pagination? – momersaleem Jul 21 '16 at 13:57
  • 1) create "addItem" method in your MySection class to add new items to your List; 2) Add items to your section using the "addItem" method; 3) call notifyDataSetChanged of sectionAdapter; – Gustavo Pagani Jul 21 '16 at 16:39
  • I didn't asked for this. I wanted to know how shall I know that my list has reached at bottom on scroll so that i can call an api for fetching next page records from server? I know that how to add new items in list. Could you please also help me to make it smooth because its little bit jerky. – momersaleem Jul 21 '16 at 19:50
  • for the "jerky" issue you should open another question and paste your code and your xml layouts, otherwise nobody can't help, for the load more you can check this out: http://stackoverflow.com/questions/27841740/how-to-know-whether-a-recyclerview-linearlayoutmanager-is-scrolled-to-top-or-b# – Gustavo Pagani Jul 21 '16 at 20:18
  • Glad to hear it worked, please consider marking my answer as correct then, clicking on the tick on the right top side of my answer. cheers – Gustavo Pagani Jul 26 '16 at 10:07