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

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

在此处输入图像描述

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

 list.setTranscriptMode(ExpandableListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); 

将列表的头部设置为底部

 list.setStackFromBottom(true); 

您也可以在xml中设置

 android:transcriptMode="alwaysScroll" 

尝试这个:

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

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

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

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

      

第2步:创建如下function

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