在网上搜了一下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. TASKCTL5.0日志乱码解决方案

    从大学毕业到现在,做了不少银行外包项目,数据类的项目基本都用到taskctl调度产品,一直习以为然,觉得调度产品都应该是这样的,所以也没觉得怎样,直到后来有两个外包项目没用taskctl调度工具,要接 ...

  2. Win7 下 PB (PowerBuilder) Insert Control 崩溃的解决办法

    环境: WIN7 x86  PB8.0, x64系统目录不同,不过也可以试试 Insert -> OLE... -> Insert Control  - 崩溃 如果网上提供的办法解决不了你 ...

  3. Analysis of container and Injection in Java, their history and future.

    Container: 发展历程: 2000 年的时候 FreeBSD 开发了一个类似于 chroot 的容器技术 Jails,这是最早期,也是功能最多的容器技术.Jails 英译过来是监狱的意思,这个 ...

  4. CF450B Jzzhu and Sequences(矩阵加速)

    CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...

  5. Linux之 sed用法

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  6. 基于 Ubuntu 搭建 FTP 文件服务

    搭建 FTP 文件服务 安装并启动 FTP 服务 任务时间:5min ~ 10min 安装 VSFTPD 使用 apt-get 安装 vsftpd: sudo apt-get install vsft ...

  7. L2-006. 树的遍历(不建树)

    L2-006. 树的遍历   给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(<=30),是二叉树中结点 ...

  8. Codeforces 990D - Graph And Its Complement

    传送门:http://codeforces.com/contest/990/problem/D 这是一个构造问题. 构造一张n阶简单无向图G,使得其连通分支个数为a,且其补图的连通分支个数为b. 对于 ...

  9. Leetcode 76.最小覆盖子串

    最小覆盖子串 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = "A ...

  10. 48. spring boot单元测试restfull API【从零开始学Spring Boot】

    回顾并详细说明一下在在之前章节中的中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...