将RecyclerView用作ExpandableListView的子级

问题描述 投票:0回答:1

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.

一切似乎都很好,但是其中一些水平滚动列表会显得很奇怪。无论我将相同或不同组展开或折叠多少次,我都希望滚动列表的位置保持不变。例如,如果我展开“ 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;
        }
    }

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

android android-recyclerview expandablelistview expandablelistadapter
1个回答
0
投票

在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 );
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.