Scrollview嵌套listviwe简单解决方案
来源:互联网 发布:临沂软件开发吧 编辑:程序博客网 时间:2024/05/20 07:58
原文链接
The shortest & easiest solution for the ListView inside a ScrollView problem.
You do not have to do anything special in layout.xml file nor handle anything on the parent ScrollView. You only have to handle the child ListView. You can also use this code to use any type of child view inside a ScrollView & perform Touch operations.
Just add these lines of code in your java class :
ListView lv = (ListView) findViewById(R.id.layout_lv);lv.setOnTouchListener(new OnTouchListener() { // Setting on Touch Listener for handling the touch inside ScrollView @Override public boolean onTouch(View v, MotionEvent event) { // Disallow the touch request for parent scroll on touch of child view v.getParent().requestDisallowInterceptTouchEvent(true); return false; } });
If you put ListView inside a ScrollView then all the ListView does not stretch to its full height. Below is a method to fix this issue.
/**** Method for Setting the Height of the ListView dynamically. **** Hack to fix the issue of not showing all the items of the ListView **** when placed inside a ScrollView ****/ public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) return; int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED); int totalHeight = 0; View view = null; for (int i = 0; i < listAdapter.getCount(); i++) { view = listAdapter.getView(i, view, listView); if (i == 0) view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, LayoutParams.WRAP_CONTENT)); view.measure(desiredWidth, MeasureSpec.UNSPECIFIED); totalHeight += view.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); listView.requestLayout();}
To use this method just pass the ListView inside this method :
ListView list = (ListView) view.findViewById(R.id.ls);setListViewHeightBasedOnChildren(list);
For using with ExpandableListView - credit Benny
ExpandableListView: view = listAdapter.getView(0, view, listView); int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.EXACTLY);int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);view.measure(widthMeasureSpec, heightMeasureSpec);
For ListView with variable items height use the below link :
Listview inside ScrollView is not scrolling on Android
- Scrollview嵌套listviwe简单解决方案
- ScrollView嵌套ListView一种简单解决方案
- ScrollView 嵌套ListView 解决方案
- ScrollView嵌套ListView解决方案
- ScrollView嵌套ListView解决方案
- ScrollView嵌套ListView解决方案
- ScrollView嵌套listView解决方案
- ScrollView嵌套ListView的解决方案
- GridView ListView 嵌套 ScrollView 解决方案
- ScrollView嵌套ListView的解决方案
- ScrollView嵌套ListView的解决方案
- ScrollView里嵌套ListView解决方案
- scrollview嵌套listview的解决方案
- ScrollView嵌套ListView的解决方案
- iOS ScrollView嵌套ScrolloView解决方案
- 在android中ScrollView嵌套ScrollView解决方案
- ScrollView ViewPager嵌套导致滑动冲突解决方案
- ScrollView嵌套listView显示不完全对应解决方案
- 空间滤波
- php FAQ
- 用一个异质链表(多态性)保存学生数据
- css 两个table td表格合并
- 用HTML开发Windows桌面应用程序2
- Scrollview嵌套listviwe简单解决方案
- linux命令详解——ln
- 十二周——杭电——1004The Last Practice
- ActiveMQ Queue和Topic
- 常用JAVA虚拟机参数
- 微信公众平台获取“openid”的方法
- 挣钱是需要智慧和能力的
- java中的sleep()和wait()的区别
- 边开火边移动