2017-04-19 74 views
0

我在我的活動中有多個視圖,包括2個垂直(1行)RecyclerViews。我使用setNestedScrollingEnabled(false)禁用了RecyclerViews上的滾動功能,只是想讓整個活動可以滾動。 這並不按預期方式工作。觸摸RecyclerViews(我的AppBarLayout)以外的視圖會觸發滾動,但只要我嘗試在RecyclerViews之一內滾動,它就不再工作了。我必須將觸摸事件傳遞給我的主視圖還是有其他解決方案?不可滾動的RecyclerView與可滾動父母

我的佈局看起來像這樣(剝離):

<android.support.design.widget.AppBarLayout> 

    <android.support.design.widget.CollapsingToolbarLayout> 

     <RelativeLayout> 

      <ImageView> 

      <LinearLayout> 

       <TextView /> 

       <TextView /> 

      </LinearLayout> 

     </RelativeLayout> 

     <android.support.v7.widget.Toolbar /> 

    </android.support.design.widget.CollapsingToolbarLayout> 

</android.support.design.widget.AppBarLayout> 

<LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <Button /> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:clipToPadding="false" 
      android:scrollbars="none" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     <Button /> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:clipToPadding="false" 
      android:scrollbars="none" /> 

    </LinearLayout> 
</LinearLayout> 

編輯:只是增加了一個圖片。觸摸紅色部分按預期滾動整個活動,但觸摸藍色部分不會滾動任何內容。 enter image description here

+0

試試我的答案。希望這會幫助你 – FAT

回答

1
  1. 使用NestedScrollView爲您ButtonRecyclerView佈局的容器。
  2. 設置app:layout_behavior="@string/appbar_scrolling_view_behavior"NestedScrollView
  3. 使用CoordinatorLayout作爲根佈局。

試試這個結構:

<android.support.design.widget.CoordinatorLayout> 

<android.support.design.widget.AppBarLayout> 
    <android.support.design.widget.CollapsingToolbarLayout> 
     <RelativeLayout> 
      <ImageView> 
      <LinearLayout> 
       <TextView /> 
       <TextView /> 
      </LinearLayout> 
     </RelativeLayout> 
     <android.support.v7.widget.Toolbar /> 
    </android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button /> 

      <android.support.v7.widget.RecyclerView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:clipToPadding="false" 
       android:scrollbars="none" /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"> 

      <Button /> 

      <android.support.v7.widget.RecyclerView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:clipToPadding="false" 
       android:scrollbars="none" /> 

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

</android.support.design.widget.CoordinatorLayout> 

希望這將有助於〜

2

使用RecyclerView內NestedScrollView

<android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

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

<android.support.v7.widget.RecyclerView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:nestedScrollingEnabled="false" /> 

<android.support.v7.widget.RecyclerView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:nestedScrollingEnabled="false" /> 


    </LinearLayout> 

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

剛更新我的問題與圖片。你的答案仍然有效嗎? – Chris

+0

如果我正確理解你的問題,那麼它應該是。確保您的組件在NestedScrollView中 –

0

覆蓋onInterceptTouchEvent方法並返回true。這會阻止兒童處理觸摸事件。

@Override 

public boolean onInterceptTouchEvent(MotionEvent ev) 
{ 
    return true; 
}