3

I need to make an expandable RecyclerView, with only one opened item per click (all others must close). I know there is possibility to do this with the help of ExpandableListView and then use next code:

elv.setOnGroupExpandListener(new OnGroupExpandListener() {

    @Override
    public void onGroupExpand(int groupPosition) {
            if (lastExpandedPosition != -1
                    && groupPosition != lastExpandedPosition) {
                elv.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
    }
});

but is there the way to make the same with using RecyclerView?

Simas
  • 43,548
  • 10
  • 88
  • 116
Stan Malcolm
  • 2,740
  • 5
  • 32
  • 53
  • I think this may help you http://stackoverflow.com/questions/26419161/expandable-list-with-recyclerview http://stackoverflow.com/questions/27203817/recyclerview-expand-collapse-items – Mauricio Flores Sep 18 '15 at 07:22

4 Answers4

3

Here is a nice tutorial for expending RecyclerView from Big Nerd Ranch

Summary:

  • Define two layouts: One for parent items and one for child items.
  • Define two ViewHolders for these layouts.
  • Define two classes for parent and child objects.
  • Parent object has to implement ParentObject interface.
  • Define custom adapter.
  • Call setParentAndIconExpandOnClick(true) on adapter.

For only one expanded view at a time, you can keep track of the last expanded view and close it when user cliced new one.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
0

You can do it by using some libraries, I'll recommend Advanced RecyclerView

cw fei
  • 1,534
  • 1
  • 15
  • 33
0

Take a look at Advanced RecyclerView. And here is the example of expandable item.

mr.icetea
  • 2,607
  • 3
  • 24
  • 42
0

Here's the way of tracking and implementing only one open item per click if using Expandable RecyclerView form big nerd ranch-

      Stack<Integer> mStack = new Stack<>();

     mRecyclerViewAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
                @Override
                public void onListItemExpanded(int position) {


                    try {
                        int x = mStack.pop();
                        mRecyclerViewAdapter.collapseParent(x);

                    } catch (EmptyStackException ex) {
                    }

                    mStack.push(position);


                }

                @Override
                public void onListItemCollapsed(int position) {

                    try {
                        mStack.pop();
                    } catch (EmptyStackException ex) {
                    }

                }
            });
Rahul
  • 189
  • 1
  • 13