Expandable ListView inside a RecyclerListView in Android
compile 'net.cachapa.expandablelayout:expandablelayout:2.9.1'
use this library for expandable layout.
Using RecyclerView as the child of ExpandableListView
In the end of if block I used a HashMap to store the newly created child view, and I tried retrieve it at the third line of 'getChildView()'.
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final List<Assignment> assignmentList =
(List<Assignment>) getChild(listPosition, expandedListPosition);
String groupName = (String)getGroup(listPosition);
convertView = assignmentRecyclerViewList.get(new Integer(listPosition));
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
AssignmentAdapter assignmentAdapter;
RecyclerView recyclerViewAssignment;
RecyclerView.LayoutManager mLayoutManager;
recyclerViewAssignment = (RecyclerView) convertView
.findViewById(R.id.expandedListItem);
assignmentAdapter = new AssignmentAdapter(assignmentList);
mLayoutManager =
new LinearLayoutManager(this.context,
LinearLayoutManager.HORIZONTAL, false);
recyclerViewAssignment.setLayoutManager(mLayoutManager);
recyclerViewAssignment.setItemAnimator(new DefaultItemAnimator());
recyclerViewAssignment.setAdapter(assignmentAdapter);
assignmentRecyclerViewList.put(new Integer(listPosition), recyclerViewAssignment);
}
return convertView;
}
Then, I call the 'getChildView()' in the MainActivity inside of OnGroupExpand of ExpandableListView.
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int i) {
expandableListView.getExpandableListAdapter().getChildView(i, i, false,null,null );
}
});
Recycleview with nested expandable lists in header
You were right to search a library that does most of the work for you, but I don't like the library you picked. It does not seem very flexible and concise. I suggest to take a look at Groupie, its API is pretty clean. Also check Reddit for some discussion on libraries.
If you want to write it yourself I think you can solve it without nested Adapter
's. Just create an 'expandable group' item type. Then in getItemCount()
you count all items and their nested items (when expanded). Take a look at the Groupie source code.
Some additional feedback on your code:
- I would explicitly add the header to the list of items you give to your adapter. So instead of a
List<Plants>
, you rather provide aList<Item>
and have aHeaderItem
andPlantsItem
. This way you have a clear separation between your domain models (Plants
) and view models (the items) in your adapter. - Your
onBindViewHolder()
method does way too much. Let yourViewHolder
subclasses take care of that. Create an abstractViewHolder
with an abstractbindTo(Item item)
method. Then in yourHeaderViewHolder
subclass it and do the actual work (after an instanceof check). - Have a look at view binding, it can make your code more concise. (So does Kotlin.)
Related Topics
How to Automate the Android Sdk Installation
ASP.NET Web API Authentication
Customize Check Box Preference
How to Display Map in Android with Marker
Install_Failed_Shared_User_Incompatible While Using Shared User Id
Sharing a Png Image in Drawable Folder
Android:How to Capture a Image of Gps Location
Importing Module in Android Studio
Rotating Phone Quickly 180 Degrees, Camera Preview Turns Upside Down
Android: How to Center Title in Toolbar
Search in Listview with Edittext
Drawing Multiple Lines in Edittext E.G. Notepad
Why a New Viewmodel Is Created in Each Compose Navigation Route
Android Eclipse Noclassdeffounderror for External .Jar Files