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

完美解決EditText和ScrollView的滾動沖突(上)

width ontouch 完美解決 his 滾動 on() ack can @+

在網上搜了一下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的滾動沖突(上)