I am looking to implement a material list of this style. How can I do this on Android? What classes should I look at? Are there any existing libraries that could make implementing this easy?
How can I implement a Material Design Expand/Collapse List on Android?
Asked Answered
Did you find a solution for this, what library did you end up using? I'm currently using this one but looking for collapsible items like your screenshots –
Mayolamayon
Yes, you can easily implement it with the library SectionedRecyclerViewAdapter. There is a full working example here.
Basically you create a section class:
class MySection extends StatelessSection {
String title;
List<String> list;
boolean expanded = true; // true if you want it to be displayed expanded initially
public MySection(String title, List<String> list) {
// call constructor with layout resources for this Section header, footer and items
super(R.layout.section_header, R.layout.section_item);
this.title = title;
this.list = list;
}
@Override
public int getContentItemsTotal() {
return expanded? list.size() : 0;
}
@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));
}
@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new SimpleHeaderViewHolder(view);
}
@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
// bind your header view here
headerHolder.tvItem.setText(title);
// handles the click on the header to toggle the expanded variable
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();
}
});
}
}
Then you set up the RecyclerView with your sections:
// Create an instance of SectionedRecyclerViewAdapter
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();
// Create your sections with the list of data for each topic
MySection topic1Section = new MySection("Attractions", attractionsList);
MySection topic2Section = new MySection("Dining", diningList);
// Add your Sections to the adapter
sectionAdapter.addSection(topic1Section);
sectionAdapter.addSection(topic2Section);
// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
I implemented such a list using this library:
expandable-recycler-view
There is a related blog, but it refers to an old version:
Expand a RecyclerView in Four Steps
It's basically an adapter where you can supply a list of parent elements that contain the children. You further have to specify two holders for the parents and the children. See the library's page for more details.
class MyChild {
// add data
}
class MyParentListItem implements ParentListItem {
private final List<MyChild> mChildren;
MyParentListItem(List<MyChild> children) {
mChildren = children;
// add other data
}
@Override
public List<MyChild> getChildItemList() {
return mChildren;
}
@Override
public boolean isInitiallyExpanded() {
return false;
}
}
class MyParentViewHolder extends ParentViewHolder {
MyParentViewHolder(View itemView) {
super(itemView);
// get other views with itemView.findViewById(..);
}
}
class MyChildViewHolder extends ChildViewHolder {
MyParentViewHolder(View itemView) {
super(itemView);
// get other views with itemView.findViewById(..);
}
}
public class MyExpandableAdapter extends ExpandableRecyclerAdapter<MyParentViewHolder, MyChildViewHolder> {
private final LayoutInflater mInflater;
public MyExpandableAdapter(List<MyParentListItem> parentItemList, Context context) {
super(parentItemList);
mInflater = LayoutInflater.from(context);
}
@Override
public MyParentViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
final View itemView = mInflater.inflate(R.layout.parent_layout, parentViewGroup, false);
return new MyParentViewHolder(itemView);
}
@Override
public MyChildViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
final View itemView = mInflater.inflate(R.layout.child_layout, childViewGroup, false);
return new MyChildViewHolder(itemView);
}
// bind data to holders in the onBind methods
}
© 2022 - 2024 — McMap. All rights reserved.