There is a recyclerView list in which my items are located. In some ( which contain categories ) of them, there is a push button.

How can I click on this button to open a list with these categories?

ps: I refer to these categories data[position].category . I do analog expadableList for recycler'a .

    1 answer 1

    Example of implementing an adapter for drop-down lists with RecyclerView

    Add the expanded parameter to the category class:

     public class ItemsGroup<T> { private List<T> items; private boolean expanded; public List<T> getItems() { return items; } public void setItems(List<T> items) { this.items = items; } public boolean isExpanded() { return expanded; } public void expand() { expanded = true; } public void collapse() { expanded = false; } } 

    Then in the adapter, add a method for calculating the "flat list" and a method for expanding / collapsing the list by pressing:

     public class RecyclerViewExpandableAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private List<ItemsGroup<?>> groups; private List<?> getFlatItemsList() { List<Object> items = new ArrayList<>(); for (ItemsGroup<?> group : groups) { items.add(group); if (group.isExpanded()) { items.addAll(group.getItems()); } } return items; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ... } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ... } @Override public int getItemCount() { return getFlatItemsList().size(); } private void onHeaderClicked(ItemsGroup<?> header) { int idx = getFlatItemsList().indexOf(header); if (header.isExpanded()) { header.collapse(); notifyItemRangeRemoved(idx + 1, header.getItems().size()); } else { header.expand(); notifyItemRangeInserted(idx + 1, header.getItems().size()); } } } 

    The onHeaderClicked method onHeaderClicked be called in the OnClickListener categories.