在網(wǎng)上搜了一下EditText和ScrollView的滾動(dòng)沖突,發(fā)現(xiàn)幾乎所有的解決方案都是觸摸EditText的時(shí)候就將事件交由EditText處理,否則才將事件交由ScrollView處理。這樣確實(shí)初步解決了兩者之間的滾動(dòng)沖突,但并不是最好的解決方案。比如,EditText本來(lái)可以顯示6行文本,但是目前只顯示了5行文本,此時(shí)我們?cè)贓ditText區(qū)域進(jìn)行滑動(dòng)并期望整個(gè)頁(yè)面能夠滾動(dòng),但由于我們將事件交給了EditText進(jìn)行處理,所以頁(yè)面并不能滾動(dòng),這樣的體驗(yàn)是極差的。其實(shí)我們更希望當(dāng)EditText出現(xiàn)滾動(dòng)條的時(shí)才將滾動(dòng)事件交由它本身處理,其他情況下應(yīng)當(dāng)讓ScrollView來(lái)處理。那么該如何進(jìn)行實(shí)現(xiàn)呢?接下來(lái)咱們就做一個(gè)小Demo來(lái)實(shí)現(xiàn)這種方案。
1.布局文件
首先編寫布局文件,可以看出這是非常簡(jiǎn)單的一個(gè)布局:一個(gè)ScrollView包裹著一個(gè)垂直方向的LinearLayout,LinearLayout中有兩個(gè)TextView和一個(gè)EditText,其中為了區(qū)分EditText的范圍,給其設(shè)置了一個(gè)背景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的代碼,更沒(méi)有什么技術(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)前點(diǎn)擊的區(qū)域是否為EditText,如果為EditText區(qū)域則再判斷是否可以在垂直方向上進(jìn)行滾動(dòng),如果可以滾動(dòng)則將事件交由EditText處理,否則將事件交由ScrollView處理。
此處最重要的就是如何判斷EditText區(qū)域在垂直方向上可以滾動(dòng),此處的代碼已經(jīng)封裝成了一個(gè)方法,大家可以直接使用。那么為什么要這樣判斷呢?如果大家仍有興趣,請(qǐng)繼續(xù)閱讀完美解決EditText和ScrollView的滾動(dòng)沖突(下)。
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可以滾動(dòng)則將事件交給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豎直方向是否可以滾動(dòng) * @param editText 需要判斷的EditText * @return true:可以滾動(dòng) false:不可以滾動(dòng) */ private boolean canVerticalScroll(EditText editText) { //滾動(dòng)的距離 int scrollY = editText.getScrollY(); //控件內(nèi)容的總高度 int scrollRange = editText.getLayout().getHeight(); //控件實(shí)際顯示的高度 int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom(); //控件內(nèi)容總高度與實(shí)際顯示高度的差值 int scrollDifference = scrollRange - scrollExtent; if(scrollDifference == 0) { return false; } return (scrollY > 0) || (scrollY < scrollDifference - 1); }}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選