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. php生成唯一识别码uuid

    /*生成唯一标志*标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(8-4-4-4-12)*/ function uuid() { $chars = md ...

  2. form 表单的另类触发方式:报错触发

    在用form表单提交的时候,遇到一个问题:表单未验证完,表单就提前提交了. 然后通过断点调试,发现form提交会因为函数报错提前提交. 即如果你的form提交过程中,没有执行到return true之 ...

  3. jQuery删除元素

    remove() - 删除被选元素(及其子元素) empty() - 从被选元素中删除子元素 $("#div1").remove();删除被选元素及其子元素. $("#d ...

  4. 获取淘宝sessionkey 实时保存

    <?php/* * To change this license header, choose License Headers in Project Properties. * To chang ...

  5. 树(6)-----DFS

    1.二叉树的反向层次遍历 def levelOrderBottom1(self, root): res = [] self.dfs(root, 0, res) return res def dfs(s ...

  6. IOS - NSDate 自己挖的坑,自己跳

    NSDate:5是坑啊啊! NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDat ...

  7. 【SPOJ 104】HIGH - Highways (高斯消元)

    题目描述 In some countries building highways takes a lot of time- Maybe that's because there are many po ...

  8. [GSS5] Can you answer these queries V

    大力讨论. luogu上交spoj的题卡的一比... 难受 wa了好几次,原因大概首先求的是非空区间,不能乱和0取max,第二点是求无相交的解时,在两段求lmx和rmx的时候可以取max(0). 区间 ...

  9. deep learning 经典网络模型之Alexnet、VGG、Googlenet、Resnet

    CNN的发展史 上一篇回顾讲的是2006年Hinton他们的Science Paper,当时提到,2006年虽然Deep Learning的概念被提出来了,但是学术界的大家还是表示不服.当时有流传的段 ...

  10. FreeMarker 页面静态化解决方案

    一.网页的静态化方案 1.生成静态页面的时机:在做添加操作时,同时生成该新增内容的对应的静态页面 2.静态页面的名称:内容 id + ".html" 3.静态页面所在的路径:工程外 ...