簡體   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>

單擊 fab 時,會出現snack bar ,為此,我必須使用協調器布局將工廠水平移動到小吃店上方。 但是現在recyclerview的前幾項是不可見的。 它僅從 item3 開始。 有人可以幫忙解決這個問題嗎?要做出哪些改變?

提前致謝!!

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

您正在為RecyclerView使用layout_below ,但此屬性旨在供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"

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

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

你可以很容易地在協調器布局中使用 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>

干杯..!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM