Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Expandable items with LinearLayoutManager (introduced with Android L)

Tags:

RecyclerView has been introduced with Android L and is part of the AppCompat v7 library. I then decided to update my app with this brand new RecyclerView to replace my ListViews. For doing so, when it comes to set a LayoutManager for the RecyclerView, I am using the LinearLayoutManager, which works fine.

Here is the hard stuff: when I want to change my ExpandableListView to a RecyclerView. Since Google has not created an "ExpandableLayoutManager", this is quite tricky and I can't achieve this.

The documentation mentions children but it appears to be children of the root view of the RecyclerView not of children themselves.

Has someone a workaround or some clues about it? Or at least some information like where to start so that I can implement my own LayoutManager.

like image 784
Lucas S. Avatar asked Oct 27 '14 14:10

Lucas S.


People also ask

What is LinearLayoutManager Android?

This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A RecyclerView. LayoutManager implementation which provides similar functionality to android. widget. ListView .

Which method must be overridden to implement a RecyclerView adapter?

Override onBindViewHolder instead if Adapter can handle efficient partial bind. The ViewHolder which should be updated to represent the contents of the item at the given position in the data set. The position of the item within the adapter's data set.

What is ViewHolder in RecyclerView Android?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive findViewById results. While LayoutParams belong to the LayoutManager , ViewHolders belong to the adapter.


1 Answers

This library helps you to group your items into "sections" and you can then implement the expand/collapse functionality following in this example.

First you create your section class:

class MySection extends StatelessSection {

    String header;
    List<String> list;
    boolean expanded = true;

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

    @Override
    public int getContentItemsTotal() {
        return expanded? list.size() : 0;
    }

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

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

        headerHolder.tvTitle.setText(title);

        headerHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expanded = !expanded;
                headerHolder.imgArrow.setImageResource(
                        expanded ? R.drawable.ic_keyboard_arrow_up_black_18dp : R.drawable.ic_keyboard_arrow_down_black_18dp
                );
                sectionAdapter.notifyDataSetChanged();
            }
        });
    }

    @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));
    }
}

Then create instance of your sections and set up your adapter:

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

// Add your Sections
sectionAdapter.addSection(new MySection("Section 1", Arrays.asList(new String[] {"Item 1", "Item 2", "Item 3", "Item 4" })));
sectionAdapter.addSection(new MySection("Section 2", Arrays.asList(new String[] {"Item 1", "Item 2" })));

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
like image 139
Gustavo Avatar answered Oct 07 '22 22:10

Gustavo