如何滚动可扩展列表视图直到结束?

时间:2013-05-04 06:31:23

标签: android scroll

我做了一个扩展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。但是,如果我点击多个项目,它不会滚动到最后,然后长度增加并且隐藏最终项目。 我想要这样的东西:

enter image description here

3 个答案:

答案 0 :(得分:5)

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

list.setTranscriptMode(ExpandableListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

将列表的头部设置为底部

list.setStackFromBottom(true);

您也可以在xml中设置

android:transcriptMode="alwaysScroll"

答案 1 :(得分:3)

试试这个:

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

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

How to scroll to the bottom of ListView programmatically?

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


}

第3步:在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 在适配器集

之后调用setListViewHeight()
 Categories categories[] = response_data.getCategories();
                        elv_categories.setAdapter(new ExpandableListAdapter(categories, getApplicationContext()));
                        setListViewHeight(elv_categories, 0, 1);