2017-10-09 141 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); 
相关问题