1

I have 2 RecyclerView (Vertical) one inside the other. Outer RecyclerView is having a List of Child and within this Inner, RecyclerView is having list of devices available to the specific child. The inner RecyclerView is having a condition that it should show 2 items for the first time and these are having a button on this button click the entire list of inner RecyclerView will be visible for the particular position(child). I have easily implemented the above scenario but facing 2 problems:

  1. In Outer Recyclerview item click - only the last position item is expanding, on clicking to other position the item remain collapsed.
  2. After scrolling expanded view automatically collapsed itself.

Below I am sharing my code :

  1. Sending complete Child list to Outer Adapter.

    adapterHomeChildList.setList(responseChildList); // send list to the adapter.

2.Set list function and BindViewHolder of Outer Adapter

public void setList(List<UserWithDeviceListResponse> mChildLists) {
    childLists = new ArrayList<>();
    childLists = mChildLists;

    notifyDataSetChanged();
}

onBindViewHolder(....)

AdapterHomeDeviceList adapterHomeDeviceList = new AdapterHomeDeviceList(context);
    adapterHomeChildListBinding.setAdapter(adapterHomeDeviceList); // set inner device list adapter

    if (childLists.get(position).getDeviceList() != null && !childLists.get(position).getDeviceList().isEmpty()) {
        if (childLists.get(position).isExpanded()) {
            adapterHomeChildListBinding.setIsSeeMore(false);  // set see more button visibility
            adapterHomeDeviceList.setList(childLists.get(position).getDeviceList());
        } else {
            if (childLists.get(position).getDeviceList().size() > 2) {
                adapterHomeChildListBinding.setIsSeeMore(true);
                filter(childLists.get(position).getDeviceList(), adapterHomeDeviceList); // show top 2 item only
            } else {
                adapterHomeDeviceList.setList(childLists.get(position).getDeviceList());
            }
        }
    }
  1. Item click interface handle

    private void setRecyclerViewExpanding(int position) { for (int i = 0; i < responseChildList.size(); i++) { if (position == i) { if (responseChildList.get(i).isExpanded()) { responseChildList.get(i).setExpanded(false); } else { responseChildList.get(i).setExpanded(true); } } else { responseChildList.get(i).setExpanded(false); } } adapterHomeChildList.notifyDataSetChanged(); }

I have searched so many questions related to this but mostly answers are using expandable listview but I am not using an expandable view. Please do help me. Any help will be really grateful.

Brahma Datta
  • 1,102
  • 1
  • 12
  • 20
developer
  • 423
  • 1
  • 4
  • 12
  • It's difficult to understand your problem without seeing the code. However, there are a bunch of libraries to handle list situation. You can check this one - https://github.com/thoughtbot/expandable-recycler-view – Reaz Murshed Sep 29 '20 at 20:12
  • thanks for the help and let me edit my question , I will share my code with same. – developer Sep 30 '20 at 06:33
  • You better use `ExpandableListView` which is provided by android. It automatically handles expanded states for you. https://developer.android.com/reference/android/widget/ExpandableListView – Rohit Sep 30 '20 at 07:17
  • I don't think I could use expandable view as I am having multiple view tapping function within inner recycler view and every time different view will expand with different features. – developer Sep 30 '20 at 08:09

0 Answers0