页面加载时只显示一个可展开列表视图

时间:2018-01-03 09:37:37

标签: java android android-layout expandablelistview expandablelistadapter

我在滚动视图中的片段中使用了两个不同的ExpandableListViews,一个在另一个下面。

问题是调用活动时只显示一个ExpandableListView标题。请参考下图:

enter image description here

此外,当我单击可展开列表视图时,列表视图会展开,另一个ExpandableListView也会显示。请参考下图:

enter image description here

我希望第一次调用活动时显示可扩展列表视图。

这是我的xml:

<ExpandableListView
      android:id="@+id/exLInTheMoodFor"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:scrollbars="none"
      android:groupIndicator="@null"
      android:layout_below="@+id/lblInTheMoodFor"
      android:layout_marginTop="10dp"/>

这是用于定义和初始化Expandable列表视图并设置height:

的java代码
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View convertView = inflater.inflate(R.layout.cafes_more_fragment, container, false);

        final ExpandListChild1 items = new ExpandListChild1();

        exLInTheMoodFor = (ExpandableListView) convertView.findViewById(R.id.exLInTheMoodFor);
        ExpListItems = SetStandardGroups();
        ExpAdapter = new ExpandListAdapterProduct(activity, ExpListItems);
        exLInTheMoodFor.setAdapter(ExpAdapter);

        exLInTheMoodFor.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                setListViewHeight(exLInTheMoodFor, i);
                return false;
            }
        });


        return convertView;
    }

    //for set height show method in expnadable list view
    private void setListViewHeight(ExpandableListView listView, int group) {
        android.widget.ExpandableListAdapter listAdapter = (android.widget.ExpandableListAdapter) listView.getExpandableListAdapter();
        int totalHeight = 0;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
                View.MeasureSpec.EXACTLY);
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            View groupItem = listAdapter.getGroupView(i, false, null, listView);
            groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

            totalHeight += groupItem.getMeasuredHeight();

            if (((listView.isGroupExpanded(i)) && (i != group))
                    || ((!listView.isGroupExpanded(i)) && (i == group))) {
                for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {
                    View listItem = listAdapter.getChildView(i, j, false, null,
                            listView);
                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
                    totalHeight += listItem.getMeasuredHeight();
                }
            }
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        int height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
        if (height < 10)
            height = 200;
        params.height = height;
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

2 个答案:

答案 0 :(得分:0)

扩展列表中的所有组

for(int i=0; i < myAdapter.getGroupCount(); i++)
mexpandableListView.expandGroup(i);

如果扩展列表中的一个项目

mexpandableListView.expandGroup(0);

一项扩展和平衡项目压缩

mexpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                    @Override
                    public void onGroupExpand(int i) {
                        ExpandableAdapter myAdapter= (ExpandableHomeworkAdapter) mexpandableListView.getExpandableListAdapter();
                        if (myAdapter== null){
                            return;
                        }
                        for (int k=0;k<myAdapter.getGroupCount();k++){
                            if (k!=i){
                                mexpandableListView.collapseGroup(k);
                            }
                        }
                    }
                });

答案 1 :(得分:0)

问题是在scrollview中使用listview / expandablelistview。通常,此组合会导致滚动视图中存在listview的可见性问题。可以从this回答中找到完整的解释。 This链接和this链接也有助于解决您的问题。我希望这会有所帮助。