在网上搜了一下EditText和ScrollView的滚动冲突,发现差点儿全部的解决方式都是触摸EditText的时候就将事件交由EditText处理,否则才将事件交由ScrollView处理。这样确实初步攻克了两者之间的滚动冲突,但并非最好的解决方式。比方,EditText本来能够显示6行文本,可是眼下仅仅显示了5行文本,此时我们在EditText区域进行滑动并期望整个页面能够滚动,但因为我们将事件交给了EditText进行处理,所以页面并不能滚动,这种体验是极差的。事实上我们更希望当EditText出现滚动栏的时才将滚动事件交由它本身处理,其它情况下应当让ScrollView来处理。那么该怎样进行实现呢?接下来咱们就做一个小Demo来实现这种方案。

1.布局文件

首先编写布局文件,能够看出这是很easy的一个布局:一个ScrollView包裹着一个垂直方向的LinearLayout。LinearLayout中有两个TextView和一个EditText,当中为了区分EditText的范围,给其设置了一个背景rectangle_shape。

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="Hello World Begin!"/> <EditText
android:id="@+id/edit_text"
android:hint="EditText"
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="top"
android:background="@drawable/rectangle_shape"/> <TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text="Hello World End!"/>
</LinearLayout> </ScrollView>

2.rectangle_shape

背景rectangle_shape的代码,更没有什么技术含量。。。。。。

<?

xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<stroke android:color="#cccccc"
android:width="1dp"/> </shape>

3.MainActivity中的代码

这里就是基本的代码逻辑了。先给EditText设置OnTouchListener,然后先在OnTouch方法中推断当前点击的区域是否为EditText。假设为EditText区域则再推断能否够在垂直方向上进行滚动,假设能够滚动则将事件交由EditText处理,否则将事件交由ScrollView处理。

此处最重要的就是怎样推断EditText区域在垂直方向上能够滚动,此处的代码已经封装成了一个方法。大家能够直接使用。那么为什么要这样推断呢?假设大家仍有兴趣。请继续阅读完美解决EditText和ScrollView的滚动冲突(下)

public class MainActivity extends Activity implements View.OnTouchListener {

    private EditText mEditText;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mEditText = (EditText) findViewById(R.id.edit_text);
mEditText.setOnTouchListener(this);
} @Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//触摸的是EditText而且当前EditText能够滚动则将事件交给EditText处理。否则将事件交由其父类处理
if ((view.getId() == R.id.edit_text && canVerticalScroll(mEditText))) {
view.getParent().requestDisallowInterceptTouchEvent(true);
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
view.getParent().requestDisallowInterceptTouchEvent(false);
}
}
return false;
} /**
* EditText竖直方向能否够滚动
* @param editText 须要推断的EditText
* @return true:能够滚动 false:不能够滚动
*/
private boolean canVerticalScroll(EditText editText) {
//滚动的距离
int scrollY = editText.getScrollY();
//控件内容的总高度
int scrollRange = editText.getLayout().getHeight();
//控件实际显示的高度
int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom();
//控件内容总高度与实际显示高度的差值
int scrollDifference = scrollRange - scrollExtent; if(scrollDifference == 0) {
return false;
} return (scrollY > 0) || (scrollY < scrollDifference - 1);
}
}

完美解决EditText和ScrollView的滚动冲突(上)的更多相关文章

  1. 完美解决HALCON C#编程目标平台冲突问题

    完美解决HALCON C#编程目标平台冲突问题   楼主# 更多发布于:2016-11-23 10:06     背景: 目标机器工控机使用11.0.1 32位Halcon 原因你懂的.开发环境Win ...

  2. Android 解决Gallery下ScrollView滑动事件冲突

    在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义 ...

  3. 解决EditText和ScrollView滑动冲突问题

    该类需要调用 OnTouchListener接口 黄色部分是需要更改部分,改为自己的edittext@Override public boolean onTouch(View view, Motion ...

  4. 解决EditText跟ScrollView滑动冲突

    etContent.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, Motion ...

  5. 完美解决ListView 与 ScrollView 共存问题

    1:首先设置ListView的高度,在setAdapter之后调用此方法. public static void setListViewHeightBasedOnChildren(ListView l ...

  6. 【转】完美解决Python与anaconda之间的冲突问题

    本文转自:https://blog.csdn.net/sinat_41898105/article/details/80660332 anaconda指的是一个开源的Python发行版本,其包含了co ...

  7. 完美解决Python与anaconda之间的冲突问题

    anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.因为包含了大量的科学包,Anaconda 的下载文件比较大(约 515 MB),如果 ...

  8. 解决distinct与order by 的冲突

    sql="select distinct id from test order by otherfield desc" 需要找到不同的id,同时又想让记录按fbsj排序.但是这样一 ...

  9. 使用LinearLayout实现ListView,解决ListView和ScrollView滚动冲突

    在项目中,我们常常会遇到一个ScrollView里面会嵌套ListView的情况,但往往你会发现,ListView和ScrollView的滚动时间会有冲突问题,造成ListView不能完全显示.虽然网 ...

随机推荐

  1. 【Linux】 JDK安装及配置 (linux-tar.gz版)

    安装环境:Linux(CentOS 7 64位 版) JDK安装:tar.gz为解压后就可以使用的版本,这里使用jdk-8u211-linux-x64.tar.gz版,安装到/usr/java/(us ...

  2. CAD多个点构造选择集(网页版)

    主要用到函数说明: IMxDrawSelectionSet::SelectByPolygon 在多个点组合的闭合区域里,构造选择集.详细说明如下: 参数 说明 [in] IMxDrawPoints* ...

  3. (转载)不错的CSS写法

    根据微信订阅号“设计达人”推送的文章,学到了如题知识.个人尝试了一下,感觉还不错.原文链接:http://mp.weixin.qq.com/s/g9TyBwB9xIi45TGwTBOLSQ. 字体 从 ...

  4. 散列(hash)

    散列(hash)是常用的算法思想之一,在很多程序中都会有意无意地使用到. 先来看一个简单的问题:给出N个正整数,再给出M个正整数,问这M个数中每个数分别是否在N个数中出现过. 例如N=5,M=3,N个 ...

  5. POJ2454——Jersey Politics

    POJ2454——Jersey Politics 题目大意: 在泽西奶牛和荷斯坦奶牛的最新普查中,威斯康星奶牛在谷仓中获得了三个档位. 泽西奶牛目前控制着国家重新分配委员会. 他们想将国家分为三个相当 ...

  6. Linux 源码

    https://elixir.bootlin.com/linux/latest/source

  7. ubuntu 14.04 挂载window共享目录

    (1) 先在ubuntu系统里,新建一个目录用于挂载,目录假设为 /mnt/win: sudo mkdir /mnt/win (2)在windows系统,共享出一个文件夹,共享名称假设为www sud ...

  8. svn汉化包安装无效的解决办法

    下载svn汉化包要和对应的svn客户端版本对应,否则安装无效, 在安装前要想将svn安装目录下的languages目录下的文件全部删除 还有一点要注意的是 汉化包安装要放在svn安装目录下进行安装,它 ...

  9. buf.readUInt16BE()

    buf.readUInt16BE(offset[, noAssert]) buf.readUInt16LE(offset[, noAssert]) offset {Number} 0 <= of ...

  10. streamwise veloicty along the jet axis using Matlab/Octave

    input file: v.csv scpirts as follows filename='v.csv'; % assign file to a variable m=csvread(filenam ...