使用 RecyclerView 作为 ExpandableListView 的 child

Using RecyclerView as the child of ExpandableListView

我建了一个水平滚动列表作为ExpandableListView的child。水平列表由 RecyclerView 和 CardView 组成。请点击此link查看图片。

一切似乎都很好,但其中一些水平滚动列表会表现得很奇怪。无论我展开或折叠相同或不同的组多少次,我都希望保持滚动列表的位置不变。例如,如果我展开组 "Comp 203" 并滚动水平列表后,我们可以看到 "assignment14" 作为水平列表中的第一个。无论我展开或折叠该组或不同组多少次,我都想将 "assignment14" 保留为第一个。

但是,当我扩展其他组时,例如 "Comp 202",组 "Comp 202" 中的水平列表看起来与组 "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;
        }
    }

我只在 CustomExpandableListAdapter 中留下了 getChildView() 方法,因为我怀疑问题出在那里。

在 if 块的末尾,我使用了一个 HashMap 来存储新创建的子视图,并尝试在 '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;
    }

然后,我在ExpandableListView的OnGroupExpand里面的MainActivity中调用了'getChildView()'

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