Scrollview嵌套Listview出现的问题
来源:互联网 发布:淘宝盖楼是什么 编辑:程序博客网 时间:2024/04/29 12:35
先贴出曾经用来解决问题的代码:
/** 重新计算listview的高度-曾经用过的方法*/
public static void setListViewHeightBasedOnChildren(ListView listView) {
/* ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
if (listItem != null) {
listItem.measure(0, 0);
}
totalHeight += listItem.getMeasuredHeight();
}
}
以上方法,看起来是可以解决listview嵌套问题,但事实是,如果你的Item里面有Textview恰好这个Textview的值比较大,以至于要占用两行的空间的时候,问题就出来了。listview仍然不能显示完全就是因为这个方法只计算单行时没数据时候的高度。下面完美解决这个问题的方法出现
感谢 :http://stackoverflow.com/questions/6210895/listview-inside-scrollview-is-not-scrolling-on-android/17503823#17503823
方法如下:自定义listview
public class NestedListView extends ListView implements OnTouchListener, OnScrollListener { private int listViewTouchAction; private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99; public NestedListView(Context context, AttributeSet attrs) { super(context, attrs); listViewTouchAction = -1; setOnScrollListener(this); setOnTouchListener(this); } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) { if (listViewTouchAction == MotionEvent.ACTION_MOVE) { scrollBy(0, -1); } } } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int newHeight = 0; final int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); if (heightMode != MeasureSpec.EXACTLY) { ListAdapter listAdapter = getAdapter(); if (listAdapter != null && !listAdapter.isEmpty()) { int listPosition = 0; for (listPosition = 0; listPosition < listAdapter.getCount() && listPosition < MAXIMUM_LIST_ITEMS_VIEWABLE; listPosition++) { View listItem = listAdapter.getView(listPosition, null, this); //now it will not throw a NPE if listItem is a ViewGroup instance if (listItem instanceof ViewGroup) { listItem.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } listItem.measure(widthMeasureSpec, heightMeasureSpec); newHeight += listItem.getMeasuredHeight(); } newHeight += getDividerHeight() * listPosition; } if ((heightMode == MeasureSpec.AT_MOST) && (newHeight > heightSize)) { if (newHeight > heightSize) { newHeight = heightSize; } } } else { newHeight = getMeasuredHeight(); } setMeasuredDimension(getMeasuredWidth(), newHeight); } @Override public boolean onTouch(View v, MotionEvent event) { if (getAdapter() != null && getAdapter().getCount() > MAXIMUM_LIST_ITEMS_VIEWABLE) { if (listViewTouchAction == MotionEvent.ACTION_MOVE) { scrollBy(0, 1); } } return false; }}
- Scrollview嵌套Listview出现的问题
- ScrollView嵌套listview出现的问题
- ScrollView和listView嵌套出现的问题
- 解决ListView嵌套ListView或者ScrollView嵌套ListView出现的问题
- 当ScrollView中嵌套ListView时出现的问题
- scrollView嵌套listview的问题
- ScrollView嵌套ListView的问题,
- ScrollView嵌套listview的问题
- ScrollView嵌套ListView的问题
- ScrollView嵌套ListView的问题
- listview嵌套listview ScrollView嵌套listview 的冲突问题
- 使用scrollview嵌套listview或gridview后出现scrollview乱滚动的问题
- 【问题汇总】ScrollView嵌套ListView的问题
- ScrollView嵌套ListView问题
- ScrollView嵌套ListView问题
- ScrollView嵌套ListView问题
- ScrollView嵌套ListView问题
- ScrollView嵌套listView问题
- HDU 1846 Brave Game(巴什博奕)
- 常用的hibernate注解标签
- Git命令总结
- [Unity]关于多角色自由切换主操作角色的问题
- 4.Swift初探-2048小游戏中的问题
- Scrollview嵌套Listview出现的问题
- 通过时间戳防止scroll 反复触发
- ASP代码开发规范
- android访问数据库时报runtiameException,和空指针异常
- iOS UIlabel sizeToFit失效的问题
- UE3 基本的分析及优化技术
- 黑马程序员——反射
- 黑马程序员——图形化界面
- 所有外包项目威客网站列表