1.在ScrollView里面嵌套ListView时,ListView的滑动事件无法响应。

先看下事件分发的过程:

由父View层的  onInterceptTouchEvent    到中间层的onInterceptTouchEvent   再到我们View层的  onTouchEvent

在回到中间层的  onTouchEvent  最后回到父View的onTouchEvent。

 我们在view中设置的OnTouchEvent没有响应事件,那么很清楚,在父View的OnInterceptTouchEvent 被拦截了。

 这样我们可以很明确的去重新父View的OnInterceptTouchEvent方法。

2.看下实现的效果图

 

3.实现

  布局效果:

  

    <myapplication.com.myasynctask.entity.MyScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <ImageView
android:layout_height="300dp"
android:layout_width="match_parent"
android:src="@mipmap/background"
android:scaleType="centerCrop"/> <ListView
android:layout_height="200dp"
android:layout_width="match_parent"
android:id="@+id/listView"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal">
<ImageView
android:layout_height="400dp"
android:layout_width="match_parent"
android:src="@drawable/zuo"/>
</LinearLayout> </LinearLayout>
</myapplication.com.myasynctask.entity.MyScrollView>

  重写ScrollView

public class MyScrollView extends ScrollView {
public MyScrollView(Context context) {
super(context);
} public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}

  代码:

/**
* ScrollView listView嵌套,保证listView能够滑动,
* 这里需要确保,上层View不会拦截onTouch,则重写ScrollView的onInterceptTouchEvent事件,设置 return false;
*/
public class Main2Activity extends AppCompatActivity { ListView listView;
MyScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
} public void initView(){
scrollView= (MyScrollView) findViewById(R.id.scrollView); listView= (ListView) findViewById(R.id.listView);
String [] a=new String[50];
for(int i=0;i<;i++){
a[i]="模仿填充数据"+i;
} ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,a);
listView.setAdapter(adapter);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { }
}); listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) { return false;
}
}); }
}

4.实现(2)

  除去重新scrollView我们还可以在listView的onTouch事件中拦截父View的事件分发。

scrollView.requestDisallowInterceptTouchEvent(true);

  布局:一样 需要设置scrollView的属性

android:fillViewport="true"

  看代码:

/**
* ScrollView 里面嵌套listView,listView 能够滑动展示数据,滑动其他地方,scrollView能够上下滑动
*
* 1: 设置scrollView属性fillViewport="true"
* 2: listView.setOnTouchListener事件中加入:
* scrollView.requestDisallowInterceptTouchEvent(true);
return false;
*/
public class ListActivity extends AppCompatActivity { ListView listView;
ScrollView scrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
initView();
} public void initView(){
scrollView= (ScrollView) findViewById(R.id.scrollView); listView= (ListView) findViewById(R.id.listView);
String [] a=new String[50];
for(int i=0;i<50;i++){
a[i]="模仿填充数据"+i;
} ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,a);
listView.setAdapter(adapter);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { }
}); listView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
scrollView.requestDisallowInterceptTouchEvent(true);
return false;
}
}); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String s= (String) parent.getItemAtPosition(position);
Toast.makeText(ListActivity.this,s,Toast.LENGTH_SHORT).show();
}
}); }
}

  

ScrollView和ListView滑动冲突问题的更多相关文章

  1. ScrollView 与ListView 滑动冲突完美解决

    一.介绍ListView高度的设置方法 二.根据实际需求解决冲突问题 一.介绍ListView高度的设置方法 在ScrollView中使用ListView,ListView的高度会不正常. 方式一:在 ...

  2. Android ScrollView和ListView滑动冲突解决记录

    private int mLastX; private int mLastY; public View.OnTouchListener onTouchListener = new View.OnTou ...

  3. scrollview 和 listview滑动冲突解决

    http://blog.csdn.net/wanghao200906/article/details/51084975 http://www.cnblogs.com/shitianzeng/artic ...

  4. (转)ViewPager,ScrollView 嵌套ViewPager滑动冲突解决

    ViewPager,ScrollView 嵌套ViewPager滑动冲突解决 本篇主要讲解一下几个问题 粗略地介绍一下View的事件分发机制 解决事件滑动冲突的思路及方法 ScrollView 里面嵌 ...

  5. 关于ScrollView和listview的冲突关于的滑动和宽度

    listview和ScrollView嵌套有两个冲突,关于listview显示不全的问题和listview和scrollview的滑动冲突 自定义listview package com.exmple ...

  6. ScrollView与ListView的冲突

    众所周知ListView与ScrollView都具有滚动能力,对于这样的View控件,当ScrollView与ListView相互嵌套会成为一种问题: 问题一:ScrollView与ListView嵌 ...

  7. 解决ScrollView与ListView事件冲突

    1,在最近做项目的时候使用ScrollView嵌套ListView的时候发现ListView的滑动效果失效,简单的网上搜索了一下,也就有了下面的解决方法,在ListView中设置事件的监听listvi ...

  8. Android ScrollView与ViewPager滑动冲突

    前段时间做项目碰到在ScrollView里添加ViewPager,但是发现ViewPager的左右滑动和ScrollView的滑动冲突了,解决这个问题的方法是重写ScrollView. 代码: pub ...

  9. Android布局中ScrollView与ListView的冲突的最简单方法

    看到网上流行的一种使用方法是: public class Utility { public static void setListViewHeightBasedOnChildren(ListView ...

随机推荐

  1. Wireshark抓包过滤

    主要说明下抓包前准备工作,及wireshark里面的两个过滤器:捕获过滤器和应用显示过滤器 1.捕获过滤器.顾名思义就是捕获时的过滤器.主要用来决定你要抓包抓哪个IP哪个端口,明确自己要抓哪个IP和端 ...

  2. python与图灵机器人交互(ITCHAT版本)

    #!/usr/bin/env python#-*- coding:utf-8 -*- @Author : wujf @Time:2018/9/5 17:42import requestsimport ...

  3. Eclipse maven工程 Missing artifact com.sun:tools:jar:1.7.0:system 解决方法

    解决方案一:通过maven取运行时参数,eclipse提供的环境变量,基本类似System.getProperty("java.home") <dependency> ...

  4. Python笔记8----DataFrame(二维)

    目录: DataFrame概念 DataFrame创建 基本操作 查看.索引 修改.删除 统计功能 条件筛选 合并 去除空值 4. 一些常用的函数 apply memory_usage pivot_t ...

  5. Node笔记(2)

    写一个可以生成多层级文件夹的函数 const fs = require('fs'); const path = require('path'); function mkdirs (pathname,c ...

  6. Echarts堆积柱状图排序问题

    Echarts堆积柱状图排序是按照堆积柱状图的柱子高度进行从大到小(或者从小到大)进行排序,方便查阅各坐标情况.以下是我自己研发的方法,有不对的地方敬请谅解,随时欢迎指教. 排序后效果如下图: (1) ...

  7. 以checked选中作为判断条件的各种写法

    <input type="radio" name="choice" id="ipt1"> <label for=" ...

  8. liunx 里面安装phpstudy环境s

    ngixwget -c http://lamp.phpstudy.net/phpstudy.bin  chmod +x phpstudy.bin    #权限设置 ./phpstudy.bin #运行 ...

  9. Jquery 根据HTML内容选择元素

    选择所有包含 "is" 的 元素: $("p:contains(is)")

  10. python第三周:集合、函数、编码、文件

    1.集合: 集合的创建: list_1 = set([1,2,3,4,5]) list_2 = set([2,3,44,7,8]) 集合的特性:集合是无序的,集合可以去掉重复的元素 集合的操作:求交集 ...