【问题标题】:Recycler view inside Coordinator layoutCoordinator 布局内的回收站视图
【发布时间】:2020-12-12 19:27:02
【问题描述】:

enter image description here [![enter image description here][2]][2] android:layout_width="match_parent" android:layout_height="match_parent" 机器人:方向=“垂直” android:id="@+id/rootLayout_survey_ques" 工具:context=".survey.SurveyQuesActivity">

<include layout="@layout/toolbar"
    android:id="@+id/survey_ques_toolbar"/>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/survey_ques_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/survey_ques_toolbar"
    android:layout_marginTop="2dp" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:elevation="10dp"
    android:id="@+id/fab_submit"
    android:src="@drawable/ic_submit" />

当点击 fab 时,会出现小吃店,为此我必须使用协调器布局将工厂水平移动到小吃店上方。但是现在recycler view的前几项是不可见的。它仅从 item3 开始。有人可以帮助解决这个问题以及要进行哪些更改。提前致谢!!

【问题讨论】:

  • 您的布局没有问题,错误可能在RecyclerView 的适配器中。显示它的代码和一些屏幕它的外观,如果可能的话,应该如何
  • @snachmsm,我添加了布局图片
  • 您尝试向下滚动吗?
  • @SahilGoyal 是的,在输出中第一项丢失了一半

标签: android android-layout android-recyclerview floating-action-button android-coordinatorlayout


【解决方案1】:

你可以很容易地在 coordintorLayout 中使用 Recyclerview,你也可以参考下面的代码。

  <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >


    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/myRefreshLayout">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/myRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="@dimen/dimen_6dp"
            android:paddingBottom="@dimen/dimen_12dp"
            android:cacheColorHint="@android:color/transparent"
            android:clipToPadding="false"
            tools:listitem="@layout/item_layout_list" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Widget.MaterialComponents.FloatingActionButton"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="@dimen/dimen_70dp"
        android:layout_marginEnd="@dimen/dimen_16dp"
        app:backgroundTint="@color/textColorSecondary"
        app:tint="@null"
        app:fabSize="normal"
        android:src="@drawable/app_add_record_icon" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

干杯..!

【讨论】:

    【解决方案2】:

    添加回收站查看此代码 app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"

    【讨论】:

      【解决方案3】:

      您不需要使用 marginTop 或 RelativeLayout,在 CoordinatiorLayout 中已经有针对这种情况的解决方案:layout_behaviour 尝试在您的主 XML 中使用此结构并为 RecyclerView 添加滚动布局行为

      <androidx.coordinatorlayout.widget.CoordinatorLayout...
      <com.google.android.material.appbar.AppBarLayout...
         <androidx.appcompat.widget.Toolbar...
      
      </com.google.android.material.appbar.AppBarLayout>
      <androidx.recyclerview.widget.RecyclerView
      android:id="@+id/survey_ques_recycler_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
        app:layout_behavior = "com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"/>
      

      【讨论】:

        【解决方案4】:

        将 recyclerview 放在相对布局或任何其他布局中,如下所示:

        <?xml version="1.0" encoding="utf-8"?>
        <androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">
        //Toolbar
        
        
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
        
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/survey_ques_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="2dp" />
            </RelativeLayout>
        
            <com.google.android.material.floatingactionbutton.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"
                app:srcCompat="@android:drawable/ic_dialog_email" />
        
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
        

        【讨论】:

          【解决方案5】:

          您的 recyclerView 似乎被包含的工具栏布局所覆盖。如果硬编码或使用默认高度?attr/actionBarSize

          ,您能否尝试添加与工具栏布局高度相同的上边距
          <androidx.recyclerview.widget.RecyclerView
          android:id="@+id/survey_ques_recycler_view"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_below="@id/survey_ques_toolbar"
          android:layout_marginTop="?attr/actionBarSize" /><!-- or height you gave in toolbar layout -->
          

          【讨论】:

            【解决方案6】:

            您将layout_below 用于RecyclerView,但此属性旨在供RelativeLayout 孩子使用,而不是CoordinatorLayout。所以你的项目在那里,但它们被Toolbar覆盖,因为RecyclerView不尊重attr告诉在Toolbar下方对齐。更改 RelativeLayout 的主容器以使 layout_below 属性正常工作

            <RelativeLayout
                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:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/rootLayout_survey_ques"
                tools:context=".survey.SurveyQuesActivity">
            ...
            </RelativeLayout>
            

            请注意,为FloatingActionButton 设置的android:layout_gravity="bottom|end" 属性将停止工作,因为此属性旨在与LinearLayoutFrameLayout 一起使用,而不是RelativeLayout。你可能想改用

            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            

            编辑:评论的好点。而不是上述建议,可以尝试为RecyclerView 添加适当的上边距,以便将其置于Toolbar 下方

            android:layout_marginTop="?attr/actionBarSize"
            

            【讨论】:

            • 当点击 fab 时,会出现小吃店,为此我必须使用协调器布局将工厂水平移动到小吃店上方。这种移动不能通过相对布局来执行,所以我使用了协调器布局
            • true,查看我的编辑。让我们尝试为此RecyclerView 添加一些边距,以防止被Toolbar 覆盖
            • 考虑支持/接受我的回答 :) 祝你好运!
            猜你喜欢
            • 2018-05-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-17
            • 2016-09-22
            • 2022-01-18
            • 2019-10-19
            • 1970-01-01
            相关资源
            最近更新 更多