I implemented an extensible recyclerview with children that are part of the list. I followed this code . Here's how it works
An implementation of an ExpandableListView using a RecyclerView is briefly described as follows. The list model has an additional parameter called "type", which determines whether the item is a header or a child. By checking this parameter, the adapter inflates the view and the viewer is appropriate for the type. If the type is HEADER, it will inflate the layout of the title element that contains the TextView and ImageView to indicate whether the child tree is expanding or not.
Now, what I want to do is make the advanced layout a grid. I usually do this by installing the layout manager in the GridLayoutManager, but in this case I use only one recyclerview, which means that I cannot change the layout manager without changing the header, which ends up turning the whole recyclerview into including headers.
My question is: how would you change the layout manager for only a few layouts inside the adapter?
Edit: I added the code.
Recycliewiew adapter:
public class ExpandableListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { // These are constants that are used to determine if the item is a child or a header and is defined with each item from the data model public static final int HEADER = 0; public static final int CHILD = 1; private List<Item> data; public ExpandableListAdapter(List<Item> data) { this.data = data; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int type) { View view = null; LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); // Check whether the item is a header or child and inflate differnet layouts switch (type) { case HEADER: // Inflate a header layout if the item is a header view = inflater.inflate(R.layout.list_header, parent, false); ListHeaderViewHolder header = new ListHeaderViewHolder(view); return header; case CHILD: // Inflate a child layout if the item is a child view = inflater.inflate(R.layout.list_child, parent, false); ListChildViewHolder child = new ListChildViewHolder(view); return child; } return null; } public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { final Item item = data.get(position); // Bind different layouts based on if the item is a header or child switch (item.getType()) { case HEADER: // ... case CHILD: // ... } } @Override public int getItemViewType(int position) { return data.get(position).type; } @Override public int getItemCount() { return data.size(); } // Viewholder for the header items private static class ListHeaderViewHolder extends RecyclerView.ViewHolder { // ... } // Viewholder for the child items private static class ListChildViewHolder extends RecyclerView.ViewHolder { // ... }
And this is the main activity when I declare the layout manager:
recyclerview = (RecyclerView) findViewById(R.id.recyclerview); recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
source share