溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中如何解決嵌套滑動(dòng)沖突

發(fā)布時(shí)間:2022-04-15 15:56:19 來(lái)源:億速云 閱讀:280 作者:iii 欄目:編程語(yǔ)言

這篇文章主要講解了“Android中如何解決嵌套滑動(dòng)沖突”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“Android中如何解決嵌套滑動(dòng)沖突”吧!

一.會(huì)產(chǎn)生滑動(dòng)沖突的情況

那么什么時(shí)候會(huì)產(chǎn)生滑動(dòng)沖突呢?比如你有個(gè)activity,activity的上半部分是一個(gè)布局,下半部分是一個(gè)可滑動(dòng)控件(RecyclerView、ListView等),或者下半部分是個(gè)viewpager,里面的fragment布局是一個(gè)可滑動(dòng)控件,這樣的頁(yè)面就會(huì)產(chǎn)生滑動(dòng)沖突。

二.以前的做法

雖然我以前的筆記丟失了,但是當(dāng)時(shí)的解決問題的思路我依然記得。

(1)重寫一個(gè)viewpager繼承系統(tǒng)的ViewPager,至于怎么重寫的我不太記得了

(2)重寫RecyclerView繼承系統(tǒng)的RecyclerView,因?yàn)槲矣浀脮?huì)出現(xiàn)高度的原因?qū)е翿ecyclerView不設(shè)置固定高度的話會(huì)不顯示或者只顯示一個(gè)Item,所以要重寫RecyclerView去動(dòng)態(tài)衡量Item x count 的高度。

當(dāng)時(shí)雖然能解決,但是最后的效果很變扭。

三.現(xiàn)在的做法

現(xiàn)在我肯定不會(huì)像之前一樣做,因?yàn)槌隽艘粋€(gè)新控件NestedScrollView。它能夠很好的幫我們解決滑動(dòng)沖突,接下來(lái)我會(huì)盡我所能分析所有可能出現(xiàn)的情況。

1.布局只嵌套R(shí)ecyclerView的情況

就是如下圖的情況:

Android中如何解決嵌套滑動(dòng)沖突

這種情況最容易解決,就直接使用NestedScrollView做父布局,然后嵌套R(shí)ecyclerView就行。

<android.support.v4.widget.NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/m_nsv"
  android:fillViewport="true"
  xmlns:android="http://schemas.android.com/apk/res/android">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
      android:layout_width="match_parent"
      android:layout_height="150dp"
      android:text="Hello World!" />

    <android.support.v7.widget.RecyclerView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/c_rv"
      >
    
    </android.support.v7.widget.RecyclerView>

  </LinearLayout>

</android.support.v4.widget.NestedScrollView>

這樣就行,切記要記住兩點(diǎn):

(1)在父布局NestedScrollView加android:fillViewport="true",然后RecyclerView會(huì)不顯示出來(lái),不顯示出來(lái)的原因是RecyclerView是一個(gè)動(dòng)態(tài)展示的View,而直接使用的話用我之前說的話叫做會(huì)被壓扁,所以加這個(gè)屬性讓子View顯示match_parent的效果。

(2)有的人說要給RecyclerView設(shè)setNestedScrollingEnabled(false),不然滑動(dòng)時(shí)會(huì)卡,這個(gè)我沒試過,我設(shè)的是true,目前感覺滑動(dòng)時(shí)沒什么影響。

2.布局嵌套其它可滾動(dòng)控件的情況

就是在第一種情況下把RecyclerView換成其它可滑動(dòng)控件。

直接說吧,你要用NestedScrollView才有用,原因是解決滑動(dòng)沖突的關(guān)鍵在于NestedScrollingParent和NestedScrollingChild兩個(gè)接口(下面會(huì)詳細(xì)說)

而RecyclerView和NestedScrollView都實(shí)現(xiàn)NestedScrollingChild接口,并在內(nèi)部封裝了解決滑動(dòng)沖突的邏輯處理,所以只有NestedScrollView直接嵌套R(shí)ecyclerView或NestedScrollView不會(huì)產(chǎn)生滑動(dòng)沖突。

NestedScrollView的用法和RecyclerView一樣,記得加那些屬性。

3.布局嵌套ViewPager,ViewPager嵌套R(shí)ecyclerView等可滑動(dòng)控件的情況

這種情況處理起來(lái)比較麻煩,而很多人都是碰到這種情況。如下圖:

Android中如何解決嵌套滑動(dòng)沖突

那就是使用CoordinatorLayout,使用CoordinatorLayout能解決這種情況。
但是CoordinatorLayout有BUG,使用起來(lái)卡得像坨屎一樣,不管你能不能忍,反正我是不能忍,所以我不會(huì)使用CoordinatorLayout。

不用CoordinatorLayout還有以下三種解決辦法:

(1)使用github上面開源的那個(gè)自定義CoordinatorLayout來(lái)解決,叫什么我忘了。

但是我們老大說了,最好別用別人的開源View。于是我只能用第二種方法。

(2)放棄使用ViewPager

為什么,因?yàn)橄到y(tǒng)的ViewPager做不到,上面有說到能解決沖突是因?yàn)镹estedScrollingParent和NestedScrollingChild,并且NestedScrollingChild的ViewGroup要是實(shí)現(xiàn)NestedScrollingParent接口的View,NestedScrollingParent的ChildView要是實(shí)現(xiàn)NestedScrollingChild接口的View。

而圖中的父布局和RecyclerView隔著一個(gè)ViewPager,也就是說NestedScrollingParent的ChildView是ViewPager,NestedScrollingChild的ViewGroup是ViewPager。所以說直接嵌套一層ViewPager的情況是無(wú)法解決滑動(dòng)沖突的。

那有一個(gè)很直接的辦法就是不用ViewPager,用FragmentManager,這樣就能實(shí)現(xiàn)解決滑動(dòng)沖突。

<android.support.v4.widget.NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:fillViewport="true"
  android:id="@+id/nsv"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

      <LinearLayout
        android:descendantFocusability="blocksDescendants"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="horizontal"
        android:minHeight="10dp"
        android:padding="10dp"
        android:id="@+id/ll_header">

        .........................................

       </LinearLayout>
      </LinearLayout>


      <android.support.v4.widget.NestedScrollView
        android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:id="@+id/c_nsv"
        >
        <FrameLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/fl_content"
          >

        </FrameLayout>

      </android.support.v4.widget.NestedScrollView>

    </LinearLayout>
  </android.support.v4.widget.NestedScrollView>

這里的FrameLayout就是給FragmentManager來(lái)顯示FrameLayout。

這樣做就能解決一個(gè)activity多個(gè)fragment的情況下的滑動(dòng)沖突。

但是有的朋友說不嘛,我就要Viewpager,我就要酷酷的滑動(dòng)動(dòng)畫效果。唉,那你就用最實(shí)在的第三中方法吧。

(3)自定義

沒辦法了,那就用自定義吧,自定義一個(gè)VIewGroup實(shí)現(xiàn)NestedScrollingParent接口,然后自定義一個(gè)View實(shí)現(xiàn)NestedScrollingChild接口?;蛘吣憧梢酝鈱邮褂肗estedScrollView,內(nèi)層自定義ViewPager來(lái)實(shí)現(xiàn)NestedScrollingChild接口。

你以為這樣就完啦?當(dāng)然沒這么簡(jiǎn)單。在NestedScrollingChild接口中有這些方法。

Android中如何解決嵌套滑動(dòng)沖突

你需要在這些方法里面自己寫上處理滑動(dòng)沖突的邏輯,你可以參考RecyclerView的去寫,也可以在網(wǎng)上找,網(wǎng)上有一些大神是真的有介紹,但也有一些人要么瞎JB抄別人的又不抄完,要么只會(huì)說用CoordinatorLayout。我其實(shí)也不是很會(huì)里面的細(xì)節(jié)處理,只是知道流程而已,所以也不裝X了。

四.其它使用時(shí)的問題

并非解決滑動(dòng)沖突就沒有其它問題。

1.NestedScrollView(RecyclerView)重新加載數(shù)據(jù)時(shí)會(huì)自動(dòng)滾動(dòng)到底部。

如果你碰到這種情況,只要給父布局的NestedScrollView設(shè).scrollTo(0, 0)就行,和ScrollView一樣的。

2.禁止滑動(dòng)。

如果你想在某些情況下禁止NestedScrollView滑動(dòng),可以像處理ScrollView一樣,在父布局的NestedScrollView加入監(jiān)聽,例如我這:

public void isScroll(boolean bol){
    Nsv.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        return !bol;
      }
    });
  }

這個(gè)方法是設(shè)置可滑動(dòng)和不可滑動(dòng)。

3.記得設(shè)android:fillViewport="true"

如果你嵌套的布局沒有顯示,那有可能你忘了給父布局NestedScrollView設(shè)置android:fillViewport屬性。

感謝各位的閱讀,以上就是“Android中如何解決嵌套滑動(dòng)沖突”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Android中如何解決嵌套滑動(dòng)沖突這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI