Android 解决EditText和ScrollView的滚动冲突

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

1.布局文件
首先编写布局文件,可以看出这是非常简单的一个布局:一个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);
    }
}

————————————————
版权声明:本文为CSDN博主「曾博文」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/z191726501/article/details/50701165

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值