【问题标题】:RelativeLayout in CoordinatorLayout doesn't scrollCoordinatorLayout 中的 RelativeLayout 不滚动
【发布时间】:2017-05-30 15:17:08
【问题描述】:

我的CoordinatorLayout 中有一个RelativeLayout(如下所示)。另请注意,在RelativeLayout 中我有一个RecyclerView

但是,当我滚动时,我只能在RecyclerView 上滚动,并且只有屏幕的RecyclerView 部分实际上会滚动。如果我尝试在RecyclerView 上方的布局上滚动,它根本不会滚动屏幕。这是我为正在发生的事情制作的插图:

所以我的问题是,为什么我不能在内部 RelativeLayout 上滚动?我应该以不同的方式组织布局吗?

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <RelativeLayout
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="250dp">

                // some imageviews and textviews

            </RelativeLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:scrollbars="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/header" />

        </RelativeLayout>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin" />

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_drawer"
        app:menu="@menu/menu_navigation_drawer" />

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

我该如何解决这个问题?

【问题讨论】:

    标签: android android-layout android-recyclerview android-coordinatorlayout


    【解决方案1】:

    RelativeLayout 如果不在任何可滚动视图中,则它是不可滚动的。
    尝试将您的RelativeLayout 作为RecyclerView 的标题。

    在此示例中,您将能够滚动其内容

    <ScrollView 
         ...
         ...>
             <!-- Scrollable Contents -->
    </ScrollView>
    

    在您的情况下,我认为最好的方法是让您的 RecyclerView 占据整个屏幕,然后分配其标题。关注此linkthis。有很多,所以用谷歌搜索吧

    【讨论】:

      【解决方案2】:

      好的,这可以使用单个recyclerview 并将您的标题relativelayout 作为recyclerview 的一个项目来完成。

      请大家熟悉getItemViewType方法,用来决定recyclerview item的item type。

      将数据添加到您的dataset,您将使用它来决定项目的类型。考虑下面的例子......

          public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
      
          private final int ITEM_ROW = 1;
          private final int ITEM_HEADER = 2;
      
          // Will be your dataset which will be containing first item as relativeLayout payload.
          private ArrayList<Object> mData;
      
          public MyRecyclerViewAdapter(ArrayList<Object> mData) {
              this.mData = mData;
          }
      
          @Override
          public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              RecyclerView.ViewHolder vh = null;
              if (viewType == ITEM_ROW) {
                  // Inflate your row item of recyclerview here...
      
                  View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_checkout_review_cart_item, parent, false);
                  vh = new ItemHolder(view);
              } else if (viewType == ITEM_HEADER) {
      
                  // Inflate your relativeLayout here...
      
                  View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_order_detail_grand_total_item, parent, false);
                  vh = new RelativeLayoutHolder(view);
              }
              return vh;
          }
      
          @Override
          public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
              if (holder instanceof ItemHolder) {
      
                  // Bind item of recyclerview here...
      
              } else if (holder instanceof RelativeLayoutHolder) {
      
                  // Bind data of your relativeLayout
      
              }
          }
      
          @Override
          public int getItemCount() {
              return mData.size();
          }
      
          @Override
          public int getItemViewType(int position) {
              // Decide type of an item using payload instances..
              Object obj = mData.get(position);
              if (obj instanceof ItemInfo) {
                  return ITEM_ROW;
              } else if (obj instanceof RelativeLayoutInfo) {
                  return ITEM_HEADER;
              } else {
                  return -1;
              }
          }
      
          class ItemHolder extends RecyclerView.ViewHolder {
      
              ItemHolder(View itemView) {
                  super(itemView);
              }
          }
      
          class RelativeLayoutHolder extends RecyclerView.ViewHolder {
      
              RelativeLayoutHolder(View itemView) {
                  super(itemView);
              }
          }
      }
      

      您的Payload of dataset 将决定 itemType。两种类型的Pojo 将被插入到您的dataset - RelativeLayoutInfoItemInfo 中。举个例子

      public class ItemInfo implements Serializable {
      
           // Consider adding object and getter setter here
      }
      

      public class RelativeLayoutInfo implements Serializable {
      
         // Consider adding object and getter setter here
      }
      

      给你。您的relativeLayout 被添加为recyclerview 的项目。 编码愉快!

      【讨论】:

        猜你喜欢
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-18
        • 2019-09-17
        • 1970-01-01
        • 2012-01-09
        • 1970-01-01
        相关资源
        最近更新 更多