android scrollview 嵌套listview
2016-01-14 15:32
435 查看
需求出来的时候感觉挺奇葩的,本来想用header与footer做,但由于有以前同事写过的代码,进度又比较赶,只能重写下listview。关于scrollview嵌套listview的文章早就满天飞,但试过了一些解决方案后感觉并不完全有用,不管是重新测量每个item的高度来确定listview的整体高度,还是重写onMesure方法如下:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
实现的思路还是重写了onMeasure()方法,但具体不甚了解。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
当item中包含的view有多行的时候,得到的高度并不完全对。无奈之后google发现了stackoverflow上的一段代码
<pre name="code" class="java">public class MyListView extends ListView implements OnTouchListener, OnScrollListener { private int listViewTouchAction; private static final int MAXIMUM_LIST_ITEMS_VIEWABLE = 99; private LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); listViewTouchAction = -1; setOnScrollListener(this); setOnTouchListener(this); } public MyListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @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(params); } 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; } }
实现的思路还是重写了onMeasure()方法,但具体不甚了解。
相关文章推荐
- android 获取activity的根view
- Android之史上最全最简单最有用的第三方开源库收集整理
- android Notification分析——你可能遇到的各种问题
- Android隐藏标题状态栏的方法
- Android 模仿网易新闻简单标题栏 《零基础学安卓》
- Android之DatePickerDialog用法(日历的用法)
- Android 6.0 新特性之 RunTime Permissions
- FragmentTabHost的使用
- Android SDK 目录下有很多文件夹,主要都是干什么的呢?
- Android开发笔记(四十八)Thread类实现多线程
- Android 去掉状态栏的方法汇总
- Android 自定义View练习之水波纹
- android输入类型邮箱格式
- Android-TextView换行排版混乱-允许设置SpannableString
- Android Navigation Bar
- 【Android】错误:Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
- AlertDialog crashes with BadTokenException
- Android Studio 快捷键
- Android长按imageview把图片保存到本地
- android实现多界面左右滑动切换