【问题标题】:Add another Layout (on bottom of screen and only on 1 screen) under the RecyclerView and my RecyclerView is on multiple screen在 RecyclerView 下添加另一个布局(在屏幕底部,仅在 1 个屏幕上),我的 RecyclerView 在多个屏幕上
【发布时间】:2022-01-21 18:20:09
【问题描述】:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="@dimen/recycler_view_padding_top"
            android:paddingStart="@dimen/recycler_view_padding_start"
            android:paddingEnd="@dimen/recycler_view_padding_end" />
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/button_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"    
            android:layout_marginEnd="@dimen/fab_horizontal_margin"
            android:layout_marginBottom="@dimen/fab_vertical_margin"
            android:layout_gravity="bottom|end"
            android:src="@drawable/ic_add_button"
            android:visibility="gone"
            app:fabSize="mini"
            app:maxImageSize="@dimen/fab_max_image_size"
            tools:visibility="visible"
            />
    </FrameLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

上面是我的 RecyclerView,它在多个屏幕上显示列表。(一个布局显示在多个屏幕上

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <RadioGroup
        android:id="@+id/radio1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccentSecondary"
        android:orientation="horizontal"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/radio2"
        >

        <Button
            android:id="@+id/button"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_gravity="center_vertical"
            android:text="@string/label_random_question"
            style="@style/TrainingButtonStyle"
            />

        <Button
            android:id="@+id/button2"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="@string/label_start_question"
            style="@style/TrainingButtonStyle"
            />

    </RadioGroup>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/radio2"
        android:gravity="center"
        android:orientation="horizontal"
        android:background="@color/question_training_background"
        app:layout_constraintTop_toBottomOf="@+id/radio1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button3"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="@string/label_all_question"
            style="@style/TrainingButtonStyle"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button4"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="@string/label_10_question"
            style="@style/TrainingButtonStyle"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button5"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="@string/label_20_question"
            style="@style/TrainingButtonStyle"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button6"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="@string/label_30_question"
            style="@style/TrainingButtonStyle"
            />

    </RadioGroup>
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的另一个视图(包含按钮),我必须在 RecyclerView 的底部而不是里面添加它。(仅在特定屏幕上而不是在多屏幕显示上

****可以做些什么来设置屏幕底部的视图(包含按钮)? (我无法添加视图。)****

【问题讨论】:

标签: kotlin android-layout android-recyclerview android-viewholder


【解决方案1】:

请分享一些对回答也有用的代码(作为布局 xml)。

您可以做的是在您的视图中放入一个具有给定 id 的布局。 然后在您的活动中将您要隐藏的布局的可见性设置为“GONE”,如下所示:

findViewById<LinearLayout>(R.id.page_layout_3).visibility = View.GONE // this goes into the activity 

请参阅下面的示例,其中 ID 为“page_layout_3”的 LinearLayout 在我编写时被隐藏。 以下布局将生成一个具有可滚动列表和底部导航栏的页面。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true"
    android:id="@+id/main_layout">

    <LinearLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/page_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/connectBtn"
                android:layout_width="250dp"
                android:layout_height="60dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="20dp"
                android:gravity="center"
                android:text="Scan">
                ></com.google.android.material.button.MaterialButton>

            <ProgressBar
                android:id="@+id/progress_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:visibility="invisible" />

            <ListView
                android:id="@+id/discoveredDevices"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"/>

            <LinearLayout
                android:id="@+id/page_layout_3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center|bottom"
                android:orientation="horizontal">

                <com.google.android.material.button.MaterialButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="BTN 1"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"/>

                <com.google.android.material.button.MaterialButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="BTN 2"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"/>

                <com.google.android.material.button.MaterialButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="BTN 2"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

您可以将 ListView 替换为您的 ReciclerView。

更新答案:

一旦你有你的“recyvleviewlayout.xml”或其他什么,你可以使用 android (https://developer.android.com/training/improving-layouts/reusing-layouts) 提供的 Include 标签包含它,这样你就可以在你所在的活动(屏幕?)上使用或不挂起。 确保你有一个布局基础,通过为每个活动设置一个布局来包含它。

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include layout="@layout/recyvleviewlayout"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:orientation="vertical">

            <RadioGroup
                android:id="@+id/radio1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center|bottom"
                android:orientation="horizontal"
                app:layout_constraintBottom_toTopOf="@id/radio2"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <Button
                    android:id="@+id/button"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:text="label_random_question" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:text="label_start_question" />

            </RadioGroup>

            <RadioGroup
                android:id="@+id/radio2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/radio1">

                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:text="label_all_question" />

                <Button
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:text="label_10_question" />

                <Button
                    android:id="@+id/button5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:text="label_20_question" />

                <Button
                    android:id="@+id/button6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="label_30_question" />

            </RadioGroup>
        </LinearLayout>
    </RelativeLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

【讨论】:

  • 我已经添加了两者的代码。问题是我在不同屏幕上的所有 3 个不同列表对回收站视图使用相同的布局。
  • 你有 3 个屏幕,你的意思是 3 个不同的活动吗?您可以使用 "" 在活动布局中,您需要使用您的 reciclerview。我将更新代码,它确实有效。刚刚测试过,如果我没有弄错什么你要求的。
猜你喜欢
  • 2012-03-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-24
  • 2012-01-24
  • 1970-01-01
相关资源
最近更新 更多