2017-10-09 145 views
1

我的類從BottomSheetDialogFragment擴展而來,在此佈局中使用2個recyclerViews。但總是1 recyclerView可滾動和其他recyclerView不起作用。如何在BottomSheetDialogFragment中使用2 recyclerView

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainCoordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <LinearLayout 
     android:id="@+id/mainBottomSheet" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 


     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerViewOne" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recyclerViewTwo" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

    </LinearLayout> 
</android.support.design.widget.CoordinatorLayout> 
+1

解決了這個問題,你想在同一時間滾動兩者兼而有之? – Kathi

+0

@Kathi no,sperate scroll –

+0

但總是1 recyclerView可滾動和其他recyclerView不起作用。你可以編輯你的問題來更好地理解 – Kathi

回答

3

終於得到了答案。 在CoordinatorLayout中使用2個RecyclerView。

Two RecyclerViews in CoordinatorLayout

<android.support.design.widget.CoordinatorLayout 
     android:id="@+id/mainBottomSheet" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/white"> 

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

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

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

注意,RecyclerView之一必須是match_parent另一種是任意大小的。建議爲第一個RecyclerView提供match_parent

這將導致兩個RecyclerViews可滾動。

您可以使用下面的代碼輕鬆地將RecyclerViews更改爲一半。

WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
      DisplayMetrics displayMetrics = new DisplayMetrics(); 
      windowManager.getDefaultDisplay().getMetrics(displayMetrics); 
      deviceScreenUtilsWidth = displayMetrics.widthPixels; 
recyclerViewLeft.getLayoutParams().width = deviceScreenUtilsWidth/2; 
0

我有一個類似的情況,但在我的情況下,第一個recyclerview是水平的,第二個是垂直的。我無法直接滾動第二個。因此,我通過以下方式

<android.support.design.widget.CoordinatorLayout 
     <android.support.v4.widget.NestedScrollView 

     <android.support.v7.widget.RecyclerView 
     <android.support.v7.widget.RecyclerView 

,並通過設置第二recyclerview

 recycler.setNestedScrollingEnabled(false); 
相關問題