androidandroid-recyclerviewexpandablelistviewexpandablelistadapter

Using RecyclerView as the child of ExpandableListView


I built a horizontal scrolling list as the child of ExpandableListView. The horizontal list is made up by RecyclerView and CardView. Please click this link to see the picture.

Everything seems to be fine, but some of those horizontal scrolling lists would act weird. I was hoping to keep the position of the scrolling list the same no matter how many time I expand or collapse the same or different group. For example, if I expand the group "Comp 203" and after I scroll the horizontal list, we can see "assignment14" as the first in the horizontal list. I want to keep "assignment14" as the first no matter how many time I expand or collapse that group or different group.

However, when I expand other group such as "Comp 202", the horizontal list in the group "Comp 202" would look exactly the same as the one in the group "Comp 203".

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
    
        @Override
        public View getChildView(int listPosition, final int expandedListPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
            assignmentList =
                    (List<Assignment>) getChild(listPosition, expandedListPosition);
    
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.list_item, null);
                recyclerViewAssignment = (RecyclerView) convertView
                        .findViewById(R.id.expandedListItem);
                //if (recyclerViewAssignment == null) {
                assignmentAdapter = new AssignmentAdapter(assignmentList);
                mLayoutManager =
                        new LinearLayoutManager(this.context,
                                LinearLayoutManager.HORIZONTAL, false);
                recyclerViewAssignment.setLayoutManager(mLayoutManager);
                recyclerViewAssignment.setItemAnimator(new DefaultItemAnimator());
                recyclerViewAssignment.setAdapter(assignmentAdapter);
                //}
                assignmentAdapter.notifyDataSetChanged();
            }
    
            return convertView;
        }
    }

I only left the method getChildView() in the CustomExpandableListAdapter because I am suspicious that the problem is in there.


Solution

  • In the end of if block I used a HashMap to store the newly created child view, and I tried retrieve it at the third line of 'getChildView()'.

        public View getChildView(int listPosition, final int expandedListPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {
    
            final List<Assignment> assignmentList =
                    (List<Assignment>) getChild(listPosition, expandedListPosition);
    
            String groupName = (String)getGroup(listPosition);
            convertView = assignmentRecyclerViewList.get(new Integer(listPosition));
    
            if (convertView == null) {
                LayoutInflater layoutInflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.list_item, null);
    
                AssignmentAdapter assignmentAdapter;
                RecyclerView recyclerViewAssignment;
                RecyclerView.LayoutManager mLayoutManager;
    
                recyclerViewAssignment = (RecyclerView) convertView
                    .findViewById(R.id.expandedListItem);
    
                assignmentAdapter = new AssignmentAdapter(assignmentList);
                mLayoutManager =
                        new LinearLayoutManager(this.context,
                                LinearLayoutManager.HORIZONTAL, false);
    
                recyclerViewAssignment.setLayoutManager(mLayoutManager);
                recyclerViewAssignment.setItemAnimator(new DefaultItemAnimator());
                recyclerViewAssignment.setAdapter(assignmentAdapter);
    
                assignmentRecyclerViewList.put(new Integer(listPosition), recyclerViewAssignment);
            }
    
            return convertView;
        }
    

    Then, I call the 'getChildView()' in the MainActivity inside of OnGroupExpand of ExpandableListView.

            expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                @Override
                public void onGroupExpand(int i) {
                    expandableListView.getExpandableListAdapter().getChildView(i, i, false,null,null );
                }
            });