协调器布局内的回收者视图

时间:2020-08-24 07:09:05

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

enter image description here enter image description here [![enter image description here][2]][2] android:layout_width =“ match_parent” android:layout_height =“ match_parent” android:orientation =“ vertical” 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时,出现小吃店,为此,我必须使用协调器布局将fab水平移动到小吃店上方。但是现在看不到回收器视图的前几项。它仅以item3开头。有人可以帮助您解决此问题以及要进行哪些更改。预先感谢!

5 个答案:

答案 0 :(得分:2)

您不需要使用 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"/>

答案 1 :(得分:0)

您将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>

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

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

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

android:layout_marginTop="?attr/actionBarSize"

答案 2 :(得分:0)

似乎您的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 -->

答案 3 :(得分:0)

将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>

答案 4 :(得分:0)

添加回收者查看此代码autocomplete="somerandomstring"

相关问题