使用RecyclerView作为ExpandableListView的子级

时间:2018-01-12 02:12:38

标签: android android-recyclerview expandablelistview expandablelistadapter

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;
        }
    }

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

1 个答案:

答案 0 :(得分: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 );
            }
        });