有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java页面加载时仅显示一个可扩展列表视图

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

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

enter image description here

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

enter image description here

我想在第一次调用活动时显示两个可展开列表视图

这是我的xml:

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

这是定义和初始化可扩展列表视图以及设置高度的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) {
        安卓.widget.ExpandableListAdapter listAdapter = (安卓.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();
    }

共 (1) 个答案

  1. # 1 楼答案

    展开列表中的所有组

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