【问题标题】:how use 2 recyclerView in BottomSheetDialogFragment如何在 BottomSheetDialogFragment 中使用 2 recyclerView
【发布时间】:2018-01-02 17:10:31
【问题描述】:

我的类从 BottomSheetDialogFragment 扩展,在这个布局中使用 2 个 recyclerViews。但总是 1 个可滚动的 recyclerView 和其他 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>

【问题讨论】:

  • 你想同时滚动两者吗?
  • @Kathi 不,单独滚动
  • 但总是 1 个可滚动的 recyclerView 和其他 recyclerView 不起作用。你可以编辑你的问题以更好地理解

标签: java android


【解决方案1】:

终于找到答案了。 在 CoordinatorLayout 中使用 2 个 RecyclerView。

<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,而另一个是任意大小。建议将match_parent 提供给第一个 RecyclerView。

这将导致两个 RecyclerView 可滚动。

您可以使用下面的代码轻松地将 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;

【讨论】:

    【解决方案2】:

    我有类似的情况,但在我的情况下,第一个 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-18
      • 2020-10-19
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      相关资源
      最近更新 更多