在网上搜了一下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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
Xamarin 安卓 相机 各种坑 目标API Android 6以后,我选的目标API版本是 Android 10
Android 9.0+软件某些功能突然暴毙:cleartext not permitted
Android : Resource is not a Drawable (color or path)
Flutter问题集锦——Android Studio 4.0.1 在AVD Manger中启动模拟器报“unable to locate abd”
网友评论