简体   繁体   English

Coordinator 布局内的回收站视图

[英]Recycler view inside Coordinator layout

<?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"
    android:orientation="vertical"
    android:id="@+id/rootLayout_survey_ques"
    tools: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" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

when fab is clicked, the snack bar appears, for that, I have to use the coordinator layout to move the fab horizontally above the snack bar.单击 fab 时,会出现snack bar ,为此,我必须使用协调器布局将工厂水平移动到小吃店上方。 But now the first few items of recyclerview is not visible.但是现在recyclerview的前几项是不可见的。 It only starts with item3.它仅从 item3 开始。 Can someone please help in solving this and what changes are to be made?有人可以帮忙解决这个问题吗?要做出哪些改变?

Thanks in advance!!提前致谢!!

You don't need to use marginTop or RelativeLayout there is already a solution for this case in CoordinatiorLayout: layout_behaviour Try using this structure in your main XML and add scrolling layout behavior for RecyclerView .您不需要使用 marginTop 或 RelativeLayout 在 CoordinatiorLayout 中已经有针对这种情况的解决方案: layout_behaviour尝试在您的主XML中使用此结构并为RecyclerView添加scrolling layout behavior

<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"/> 

you are using layout_below for RecyclerView , but this attribute is intended to be used by RelativeLayout childrens, not CoordinatorLayout .您正在为RecyclerView使用layout_below ,但此属性旨在供RelativeLayout子级使用,而不是CoordinatorLayout so your items are there, but they are covered by Toolbar , as RecyclerView doesn't respect attr telling to align below Toolbar .所以你的项目在那里,但它们被Toolbar覆盖,因为RecyclerView不尊重 attr 告诉在Toolbar下方对齐。 Change your main container for RelativeLayout to get layout_below attribute working更改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>

note that android:layout_gravity="bottom|end" attribute set for FloatingActionButton will stop working, as this attr is inteneded to use with LinearLayout or FrameLayout , not RelativeLayout .请注意,为FloatingActionButton设置的android:layout_gravity="bottom|end"属性将停止工作,因为此属性旨在与LinearLayoutFrameLayout一起使用,而不是RelativeLayout you may want to use instead你可能想改用

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

edit: good point in comment.编辑:评论的好点。 instead of above suggestions maybe try to add proper top margin for RecyclerView for placing it below Toolbar而不是上述建议,可能会尝试为RecyclerView添加适当的上边距,以便将其放置在Toolbar下方

android:layout_marginTop="?attr/actionBarSize"

It seems your recyclerView is covered by included toolbar layout.似乎您的 recyclerView 被包含的工具栏布局覆盖。 Can you try adding the top margin as the same as the height of your toolbar layout if hardcoded or if you used default Height ?attr/actionBarSize如果硬编码或使用默认高度,您可以尝试添加与工具栏布局的高度相同的上边距?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 -->

Put recyclerview inside Relative layout or any other layout like below:将 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>

添加 recyclerview 这段代码app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"

You can use Recyclerview within the coordinator layout very easily, you can refer to the below code as well.你可以很容易地在协调器布局中使用 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">

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

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

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

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Cheers..!干杯..!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM