Android ScrollView中的ListView不能滑动
来源:互联网 发布:叶罗丽娃娃淘宝 编辑:程序博客网 时间:2024/05/21 20:27
首先来看一个实例:
Activity中的代码:
private void initView() { ListView listView = (ListView)findViewById(R.id.lv); ArrayList list = new ArrayList(); for(int i=0;i<30;i++){ list.add("item"+i); } ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list); listView.setAdapter(adapter); }
xml中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="200dp"> </ListView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="1000dp" android:text="看我干啥,找挨揍啊!"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="没脸啊 还看啊"/> </LinearLayout> </ScrollView></LinearLayout>
运行效果:
可以发现,listView中的条目有很多,没能显示全,想滚动ListView查看更多条目,却发现事件被ScrollView拦截。解决的办法有两种,都是通过自定义父布局ScrollView。
1 在dispatchTouchEvent()方法中调用 requestDisallowInterceptTouchEvent(true)方法。
public class MyScrollView extends ScrollView { public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); }}
2 或者时采用重写onInterceptTouchEvent()方法并返回false。
public class MyScrollView extends ScrollView { public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return false; } }
上述两种方式都可以,然后在布局中引入自定义的ScrollView。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.practice_click.MyScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="200dp"> </ListView> <TextView android:layout_marginTop="1000dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="看我干啥,找挨揍啊!"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="没脸啊 还看啊"/> </LinearLayout> </com.example.practice_click.MyScrollView></LinearLayout>
效果图
可以看到滑动ListView时候ScrollView不在滚动。
0 0
- Android ScrollView中的ListView不能滑动
- android scrollview 不能滑动
- Android自定义View之ScrollViewforListView(解决ScrollView嵌套listview,listview不能滑动)
- Android ScrollView中的ListView
- 解决scrollView嵌套viewpager+listview,scrollview不能滑动的问题
- android 滑动冲突 ScrollView Viewpager listview 等
- android ScrollView与ListView,GridView滑动冲突
- Android ScrollView和ListView滑动冲突问题
- Android解决ScrollView中的滑动效果导致GestureDetector中的OnFling不能正常工作问题
- ScrollView+ListView禁止滑动
- listView scrollview滑动冲突
- Android ListView中嵌套ScrollView,ScrollView不能滚动
- scrollview 不能滑动
- ListView套上ScrollView; ListView显示不全; ListView只显示一条 不能全局滑动问题
- Android 中RelativeLayout 中的scrollview无法滑动
- Android中ScrollView与listView冲突以及ScrollView与listView无法滑动(拖动)解决办法
- Android 自定义ScrollView ListView 体验各种纵向滑动的需求
- android 标题动态变换(listview scrollview 滑动距离计算)
- Nginx使用brotli代替gzip
- JENKINS 项目集成部署
- Android 6权限
- 堆和栈的区别(内存和数据结构)(转)
- Nexus3.x.x上传第三方jar
- Android ScrollView中的ListView不能滑动
- 测试某个tty设备是否可用
- JavaWeb前端开发知识总结(HTML)
- APUE exercises 10.6 pp 381
- 判断网络
- python项目练习六:使用CGI进行远程编辑
- 常用排序算法的Java实现
- HDU2104 hide handkerchief
- 赛码网-基本算法-4.路灯