Expandable RecyclerView: How to have different “ch

2019-08-15 18:36发布

I am trying to use this library project by dgreenhalgh for creating an expandable RecyclerView, but my requirement is to have an expandable RecyclerView with different layouts for different rows.

So I first wrote a small example application to test the library project. I am posting it here. Now I need to make it use different layouts for different children-rows - i.e. when the user clicks on an item in the list (let's call that parent-row for now), it is expanded to show its children-rows, like in the following screenshot.

enter image description here

But I need different layouts for different child-rows. I searched around and found this example for "RecyclerView with Multiple Viewtype" quite simple and nice. So I want to do this like in the example, that is by extending the ViewGroup...

In this approach, they extend a main ViewGroup with different ViewGroups (each corresponding to a layout for a row-style). Then in getItemViewType of the RecyclerAdapter, they decide what viewtype to return based on the position passed in the method, and then in onCreateViewHolder, decide which layout to inflate based on the viewtype passed.

In my use-case, it would have been straightforward if it was the parent-rows that I needed the multiple layouts for, because it is the data-list containing the data to show in the parent-rows which is passed to our ExpandableRecyclerAdapter. So in our getItemViewType, we can do something like

@Override
public int getItemViewType ( int position ) {
    int viewType;
    if ( groups.get ( position ).getImagePath () != null ) { //****groups is the list containing the data to be displayed in the recyclerview's "parent" items
        ...
    } else {
        ...
    }
    return viewType;
}

where groups is the ArrayList passed to the RecyclerViewAdapter which contains the data to be displayed in the parent-rows.

This ArrayList contains objects of type ParentObject (actually a class I wrote which extends ParentObject). Inside this class, a method by the name of getChildObjectList returns a List<ChildObject> which contains the data to be displayed in the child-rows.

QUESTION:

So I can not have a getItemViewType and the entire functionality of the RecyclerViewAdapter inside my class which extends ParentObject, which is (creating) and returning the ArrayList which contains the data to be displayed in the child-rows.

So how should I go about it?

0条回答
登录 后发表回答