1. 程式人生 > >完美解決EditText和ScrollView的滾動衝突(上)

完美解決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);
    }
}