5

我做了一个类扩展ExpandableListView。以下是代码:

class CustomExpandableListView extends ExpandableListView {
        public CustomExpandableListView(Context context) {
            super(context);
        }

        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            /*
             * Adjust height
             */
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(
                    700, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        }
    } 

我用它作为。

ExpandableListView list = new CustomExpandableListView(KaasMain.this);
Adapter adapter = new Adapter(KaasMain.this, objectsLvl1);
                list.setAdapter(adapter);

                parent.addView(list);

                // list.setGroupIndicator();
                list.setTranscriptMode(ExpandableListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

                list.setIndicatorBounds(0, 0);
                list.setChildIndicatorBounds(0, 0);

我还设置了它的转录模式TRANSCRIPT_MODE_ALWAYS_SCROLL。但是,如果我单击多个项目,它不会滚动到最后,然后长度会增加并且它会隐藏最终项目。我想要这样的东西:

在此处输入图像描述

4

3 回答 3

5

您需要在列表视图中使用这些参数:

list.setTranscriptMode(ExpandableListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

将列表的头部设置为底部

list.setStackFromBottom(true);

你也可以在你的xml中设置

android:transcriptMode="alwaysScroll"
于 2013-05-04T06:43:05.347 回答
3

尝试这个:

list.smoothScrollToPosition(list.getCount() - 1);

如果需要您在更新列表后立即尝试滚动,请参阅以下答案:

如何以编程方式滚动到 ListView 的底部?

于 2013-12-12T00:03:06.880 回答
1

第 1 步: 使用以下方法创建布局:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="@dimen/dp10"
    android:layout_marginTop="@dimen/toolbar_after_margin"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ExpandableListView
            android:id="@+id/expand"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/dp10"
            android:dividerHeight="1dp"
            android:indicatorLeft="@dimen/dp20"
            android:layoutDirection="rtl" />
    </LinearLayout>


</ScrollView>

第 2 步: 创建一个函数,如下所示

public void setListViewHeight(ExpandableListView listView,
                                      int group,int type) {

    ExpandableListAdapter listAdapter =  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(type!=1){
            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();


}

第三步:在 groupClickListener 上调用 setListViewHeight

    elv_categories.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            setListViewHeight(parent, groupPosition, 2);
            return false;
        }
    });
}

step4 Adapter Set 后调用 setListViewHeight()

 Categories categories[] = response_data.getCategories();
                        elv_categories.setAdapter(new ExpandableListAdapter(categories, getApplicationContext()));
                        setListViewHeight(elv_categories, 0, 1);
于 2018-05-01T08:08:42.020 回答