【问题标题】:Button becomes invisible when recyclerview is filled with data当 recyclerview 填充数据时,按钮变得不可见
【发布时间】:2020-09-02 09:27:42
【问题描述】:

我有这样的 xml 用于 RV 项目:

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


    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/dialog_background"
        android:textColor="@color/colorAccent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/dialog_background"
        android:textColor="@color/gray_color"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/counter" />

    <de.applicatione.jobs.jobbSeek.adapters.LollipopFixedWebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:fontFamily="@font/opensans_semibold"
        android:labelFor="@id/webView"
        android:nestedScrollingEnabled="true"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/question" />

    <EditText
        android:id="@+id/user_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:autofillHints="@string/enter_your_answer_polls"
        android:background="@drawable/polls_field"
        android:fontFamily="@font/opensans_semibold"
        android:hint="@string/enter_your_answer_polls"
        android:inputType="text"
        android:textColor="#666666"
        android:textColorHint="@color/colorPrimary"
        android:visibility="gone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/webView" />


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/answers_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/dialog_background"
        android:overScrollMode="never"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/user_input" />


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/question_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="20"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/answers_options">

        <Button
            android:id="@+id/prev_question"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="10"
            android:text="@string/previous_question_polls"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/next_question"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="10"
            android:background="@drawable/main_screen_btn"
            android:text="@string/next_question_polls"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@id/prev_question"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的 RV xml:

 <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never"
        android:descendantFocusability="blocksDescendants"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/divider">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/main_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:overScrollMode="never"
                android:descendantFocusability="blocksDescendants"
                android:visibility="gone" />
    </androidx.core.widget.NestedScrollView>

问题是我没有从项目 xml 中看到我的按钮。当我用一些数据填充 RV 时,它们变得不可见。我检查了所有内容,但无法理解如何解决此问题。我很确定问题出在 xml 上,但我不明白它在哪里。我使用了布局检查器,我看到我在 RV 项中的 RV 下方的按钮,但我无法滚动并查看它们。也许我在xml中犯了任何错误?我认为问题可能在于固定 RV 大小或嵌套滚动并添加了以下代码:

!answersList.hasFixedSize()
answersList.isNestedScrollingEnabled = false

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    您希望 RecyclerView 的高度为 0dp。这将迫使它采用你给它的任何约束的高度。此外,您的顶级 ConstraintLayout 的高度应为match_parent。我还调整了一些约束,并将嵌套的 ConstraintLayout 切换为 LinearLayout:

    (我在 IDE 中切换/删除了显示为红色的资源,但布局相同)

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:textColor="@color/colorAccent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    
        <TextView
            android:id="@+id/question"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/counter" />
    
        <TextView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:labelFor="@id/webView"
            android:nestedScrollingEnabled="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/question" />
    
        <EditText
            android:id="@+id/user_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:inputType="text"
            android:text="editText"
            android:textColor="#666666"
            android:textColorHint="@color/colorPrimary"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/webView" />
    
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/answers_options"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="10dp"
            android:overScrollMode="never"
            android:background="#FF0000"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toTopOf="@id/question_buttons"
            app:layout_constraintTop_toBottomOf="@id/user_input" />
    
    
        <LinearLayout
            android:id="@+id/question_buttons"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:id="@+id/prev_question"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="10" />
    
            <Button
                android:id="@+id/next_question"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="10" />
    
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 但是由于您的顶部/底部限制,RV 变得不可见 :(
    • 检查我的编辑。这个已经过测试并且可以工作,尽管我不能保证LollipopFixedWebView 会产生什么影响。 WebViews 通常应该有match_parent 的高度。我现在只是把它变成了一个 TextView。
    • 它仍然不起作用:(也许我在活动类中做错了,但另一方面 xml 表示不依赖于活动操作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2018-04-28
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多