完美解決EditText和ScrollView的滾動沖突(上)
在網(wǎng)上搜了一下EditText和ScrollView的滾動沖突,發(fā)現(xiàn)幾乎所有的解決方案都是觸摸EditText的時候就將事件交由EditText處理,否則才將事件交由ScrollView處理。這樣確實初步解決了兩者之間的滾動沖突,但并不是最好的解決方案。比如,EditText本來可以顯示6行文本,但是目前只顯示了5行文本,此時我們在EditText區(qū)域進行滑動并期望整個頁面能夠滾動,但由于我們將事件交給了EditText進行處理,所以頁面并不能滾動,這樣的體驗是極差的。其實我們更希望當(dāng)EditText出現(xiàn)滾動條的時才將滾動事件交由它本身處理,其他情況下應(yīng)當(dāng)讓ScrollView來處理。那么該如何進行實現(xiàn)呢?接下來咱們就做一個小Demo來實現(xiàn)這種方案。
1.布局文件
首先編寫布局文件,可以看出這是非常簡單的一個布局:一個ScrollView包裹著一個垂直方向的LinearLayout,LinearLayout中有兩個TextView和一個EditText,其中為了區(qū)分EditText的范圍,給其設(shè)置了一個背景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的代碼,更沒有什么技術(shù)含量。。。。。。
<?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設(shè)置OnTouchListener,然后先在OnTouch方法中判斷當(dāng)前點擊的區(qū)域是否為EditText,如果為EditText區(qū)域則再判斷是否可以在垂直方向上進行滾動,如果可以滾動則將事件交由EditText處理,否則將事件交由ScrollView處理。
此處最重要的就是如何判斷EditText區(qū)域在垂直方向上可以滾動,此處的代碼已經(jīng)封裝成了一個方法,大家可以直接使用。那么為什么要這樣判斷呢?如果大家仍有興趣,請繼續(xù)閱讀完美解決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并且當(dāng)前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(); //控件內(nèi)容的總高度 int scrollRange = editText.getLayout().getHeight(); //控件實際顯示的高度 int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom(); //控件內(nèi)容總高度與實際顯示高度的差值 int scrollDifference = scrollRange - scrollExtent; if(scrollDifference == 0) { return false; } return (scrollY > 0) || (scrollY < scrollDifference - 1); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android TextView不用ScrollViewe也可以滾動的方法
- android 實現(xiàn)ScrollView自動滾動的實例代碼
- ScrollView滾動條顏色的設(shè)置方法
- android開發(fā)教程之文本框加滾動條scrollview
- Android垂直滾動控件ScrollView使用方法詳解
- iOS應(yīng)用開發(fā)中UIScrollView滾動視圖的基本用法總結(jié)
- android scrollview 自動滾動到頂部或者底部的實例
- Android使用HorizontalScrollView實現(xiàn)水平滾動
- iOS ScrollView嵌套tableView聯(lián)動滾動的思路與最佳實踐
- Android使用ScrollView實現(xiàn)滾動效果
相關(guān)文章
Android實現(xiàn)自適應(yīng)屏幕的彈窗廣告
這篇文章主要為大家詳細介紹了Android實現(xiàn)自適應(yīng)屏幕的彈窗廣告,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07android使用AsyncTask實現(xiàn)多線程下載實例
這篇文章主要介紹了android使用AsyncTask實現(xiàn)多線程下載實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Android ListView適配器(Adapter)優(yōu)化方法詳解
這篇文章主要介紹了Android ListView優(yōu)化方法詳解的相關(guān)資料,這里舉例說明該如何對ListView 進行優(yōu)化,具有一定的參考價值,需要的朋友可以參考下2016-11-11Android開源框架的SlidingFragment的使用示例
今天小編就為大家分享一篇關(guān)于Android開源框架的SlidingFragment的使用示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03Android Studio+MAT實戰(zhàn)內(nèi)存泄漏
這篇文章主要介紹了Android Studio+MAT實戰(zhàn)內(nèi)存泄漏的相關(guān)技術(shù)內(nèi)容,并在需要注意的地方做了提示,需要參考學(xué)習(xí)下吧。2017-12-12Android實現(xiàn)獲取聯(lián)系人電話號碼功能
這篇文章主要為大家詳細介紹了Android實現(xiàn)獲取聯(lián)系人電話號碼功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03Android中實現(xiàn)滑動的七種方式總結(jié)
這篇文章主要介紹了Android中實現(xiàn)滑動的七種方式總結(jié),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02