I have created expandable recycle view in my app looks like below
It has parent Items Menu category and Category has child items as shown in image
Following is part of my parent adapter code.
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (i > HEADER_COUNT - 1) {
FoodMenuItem foodMenuItem = foodMenuItems.get(i - HEADER_COUNT);
MenuViewHolder menuViewHolder = (MenuViewHolder) viewHolder;
//menuViewHolder.rvMenu.setLayoutManager(new LinearLayoutManager(viewHolder.itemView.getContext(), LinearLayoutManager.VERTICAL, false));
if (isVegOnly) {
List<Item> items = new ArrayList<>();
for (Item item : foodMenuItem.getItems()) {
if (item.getType().equalsIgnoreCase(Item.FOOD_TYPE_VEG)) {
items.add(item);
}
}
menuViewHolder.rvMenu.setAdapter(new FoodMenuChildRecycler(items, foodMenuParentListener, i, isAddAllowed));
menuViewHolder.tvNumItems.setText("(" + items.size() + " items)");
} else {
menuViewHolder.rvMenu.setAdapter(new FoodMenuChildRecycler(foodMenuItem.getItems(), foodMenuParentListener, i, isAddAllowed));
menuViewHolder.tvNumItems.setText("(" + foodMenuItem.getItems().size() + " items)");
}
menuViewHolder.ivArrow.setVisibility(menuViewHolder.rvMenu.getAdapter().getItemCount() > 0 ? View.VISIBLE : View.GONE);
menuViewHolder.ivArrow.setImageResource(foodMenuItem.isExpanded() ? R.drawable.chev_up : R.drawable.chev_down);
menuViewHolder.tvTitle.setText(foodMenuItem.getCategoryName());
menuViewHolder.rvMenu.setNestedScrollingEnabled(false);
if (foodMenuItem.isExpanded()) {
menuViewHolder.rvMenu.setVisibility(View.VISIBLE);
} else {
menuViewHolder.rvMenu.setVisibility(View.GONE);
}
}
}
Following is code for child adapter onBindViewHolder
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
ChildMenuHolder childMenuHolder = (ChildMenuHolder) viewHolder;
Context context = viewHolder.itemView.getContext();
Item item = itemList.get(i);
childMenuHolder.tvTitle.setText(item.getTitle());
childMenuHolder.txtQty.setText(String.valueOf(item.getQuatity()));
childMenuHolder.txtCartInc.setVisibility(item.isAddedToCart() ? View.VISIBLE : View.GONE);
childMenuHolder.txtQty.setVisibility(item.isAddedToCart() ? View.VISIBLE : View.GONE);
childMenuHolder.txtCartDec.setVisibility(item.isAddedToCart() ? View.VISIBLE : View.GONE);
childMenuHolder.btnAdd.setVisibility(!item.isAddedToCart() ? View.VISIBLE : View.GONE);
childMenuHolder.btnAdd.setBackgroundResource(isAddAllowed ? R.drawable.rounded_bottom_edge_shape_food : R.drawable.rounded_bottom_edge_shape_disable);
childMenuHolder.btnAdd.setTextColor(isAddAllowed ? ContextCompat.getColor(context, R.color.button_green) : ContextCompat.getColor(context, R.color.gray_a1));
childMenuHolder.ivType.setColorFilter(item.getType().equalsIgnoreCase(Item.FOOD_TYPE_VEG) ? ContextCompat.getColor(context, R.color.selected_green) : ContextCompat.getColor(context, R.color.app_red));
}
Following code inside Parent adapter to expand & collapse
public MenuViewHolder(@NonNull View itemView) {
super(itemView);
tvTitle = (TextView) itemView.findViewById(R.id.tvTitle);
tvNumItems = (TextView) itemView.findViewById(R.id.tvNumItems);
ivArrow = (ImageView) itemView.findViewById(R.id.ivArrow);
rvMenu = (RecyclerView) itemView.findViewById(R.id.rvMenu);
rvMenu.setLayoutManager(new LinearLayoutManager(itemView.getContext()));
// rvMenu.setNestedScrollingEnabled(false);
rlArrow = itemView.findViewById(R.id.rlArrow);
rlArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (foodMenuParentListener != null) {
foodMenuParentListener.OnExpandClick(getAdapterPosition());
}
}
});
tvTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (foodMenuParentListener != null) {
foodMenuParentListener.OnExpandClick(getAdapterPosition());
}
}
});
}
onExpandClick inside listener is as below
@Override
public void OnExpandClick(int position) {
FoodMenuRecyclerAdapter foodMenuRecyclerAdapter = (FoodMenuRecyclerAdapter) recyclerView.getAdapter();
foodMenuRecyclerAdapter.setExpandIndex(position);
foodMenuRecyclerAdapter.notifyItemChanged(position);
}
setExpandIndex inside adapter as below
public void setExpandIndex(int expandIndex) {
foodMenuItems.get(expandIndex - HEADER_COUNT).setExpanded(!foodMenuItems.get(expandIndex - HEADER_COUNT).isExpanded());
}
Since there are too many items in ChildRecycler adapter I facing a big lag issue when there are too many child items. It works properly when number of child items is less
How to fix this?
Also I am finding it difficult to have single recycler view due to presence cardview in background as shown in image otherwise I would have used single recycler view and for collapse/expand would have implemented add and remove items at a position
New Note: Date 5 May
New solution I tried using NestedScrollView and then added recycler views to this nested RecyclerView
Following is code
for (FoodMenuItem item : foodResult.getFoodMenuItems()) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_food_parent, llMenu, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.rvMenu);
((TextView)view.findViewById(R.id.tvtitle)).setText(item.getCategoryName());
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
view.findViewById(R.id.rlArrow).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recyclerView.setVisibility(recyclerView.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
recyclerView.setAdapter(new FoodMenuChildRecycler(item.getItems(), getAddItemListener(), i, isFlightAssociatedWithLocation));
llMenu.addView(view);
}
}, 200);
i++;
}
Following is XML code
<android.support.v4.widget.NestedScrollView
android:id="@+id/rvParentMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="160dp">
<LinearLayout
android:id="@+id/llMenu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</android.support.v4.widget.NestedScrollView>
Issue I am facing is code to add views to linear layout inside nested scroll view and set adapter call for recycler views is freezing UI almost for few seconds added handler for it but does not seem to a nice solution