【问题标题】:Using RecyclerView as the child of ExpandableListView使用 RecyclerView 作为 ExpandableListView 的子级
【发布时间】:2018-06-21 11:09:09
【问题描述】:

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 中,因为我怀疑问题出在哪里。

【问题讨论】:

  • 折叠“Comp 203”并再次展开时会发生什么?它会直接进入第一个位置吗?这是默认行为。
  • 现在不会,但是会复制其他组的位置。在这个版本之前,它确实排在了第一位,因为我创建了 RecyclerView 而没有检查 convertView 是否为空。我想知道无论我们展开和折叠相同或不同的组多少次,我们是否可以保持该位置。
  • 您必须自己跟踪(使用 apt 类型的变量,例如 int[]),并在返回列表后使用它重新定位列表。

标签: android android-recyclerview expandablelistview expandablelistadapter


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2020-10-10
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多