【问题标题】:RecyclerView content goes behind and not over TextView aboveRecyclerView 内容落后而不是上面的 TextView
【发布时间】:2020-04-21 22:46:34
【问题描述】:

考虑一下这个来自 Material Design 的关于拖放项目的视频:

Material design reordering list

在视频中,您会看到一个 RecyclerView,在其上方是一个带有文本“播放列表”的 TextView。当该行被向上拖动时,您会看到它越过播放列表。

在我的代码中,该行位于 Textview 后面。我已将 RecyclerView 的 layout_height 设置为 match_parent。我使用了 FrameLayout 并将 TextView 放在 RecyclerView 下方。为什么不过去呢?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layoutRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/general_margin"
        android:text="@string/activity_bible_order_explanation"
        android:layout_gravity="start|top" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:layout_marginTop="96dp"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</FrameLayout>

【问题讨论】:

  • 什么是父布局?
  • 对不起,缩进!框架布局。
  • 只需将位置 垂直方向
  • 回收站视图应该是 android:layout_width="match_parent" android:layout_height="0dip" weightSum="1"
  • @AkarshM 不起作用,同样的问题。

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


【解决方案1】:

使用约束布局作为父布局,并通过为 textview 提供顶部约束来在顶部添加 textview。然后加 recyclerview 的 top 约束到 textview 的底部,recyclerview 的底部约束到 parent 的底部,然后设置 height 0 它将起作用

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:id="@+id/layoutRoot"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:orientation="vertical">

    <TextView
            android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            android:padding="@dimen/general_margin"
            android:text="@string/activity_bible_order_explanation"
            />

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@id/txtTitle"
            android:background="@android:color/transparent"
            android:layout_marginTop="96dp"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • AAPT:错误:未找到属性 layout_constraintTop_toTopOf。需要专门的库吗?
  • 你添加约束布局了吗?
  • 请发布您最新的布局文件
  • 不要将你的回收站视图高度设置为 matchparent
【解决方案2】:

你试过海拔吗?问题可能是在 z 轴上 TextView 高于 RecyclerView。尝试添加android:elevation="10dp" 或更高...像这样:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layoutRoot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/general_margin"
        android:text="@string/activity_bible_order_explanation"
        android:layout_gravity="start|top" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="10dp" (if it doesn't work try maybe higher elevation, 20dp etc.)
        android:background="@android:color/transparent"
        android:layout_marginTop="96dp"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</FrameLayout>

希望它对你有用(:让我知道!

【讨论】:

    【解决方案3】:

    这个的父布局是什么?

    因为您在 recyclerview 中设置了 match_parent 并且列表中有很多内容,这就是面临此问题的原因。

    请给定高度或使用相对布局并在recyclerview中应用文本视图上方的位置

    【讨论】:

    • 但它必须是 match_parent 否则它永远不会超出它自己的界限。
    • @JimClermonts 请使用 ScrollView 或 NestedScrollView 作为这两者的父级并设置 recyclerView.setNestedScrollingEnabled(false)。我希望你能找到这个。
    【解决方案4】:

    您的布局代码有问题:

    1. 您正在使用FrameLayout
    2. FrameLayout 内部,您还使用了RecyclerView 的上边距 android:layout_marginTop="96dp" &lt;-- 这导致了问题。

    解决方案:

    • 您应该LinearLayout 而不是FrameLayout。并且,还将LinearLayout 的方向设置为垂直。
      android:orientation="vertical"
    • 同时从您的RecyclerView 中删除此行:-
      android:layout_marginTop="96dp"

    解决方案代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/layoutRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/txtTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:text="Hello World"/>
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
    
    </LinearLayout>
    

    【讨论】:

    • 我不知道我做错了什么,但我尝试了您的解决方案,但它不起作用。
    • 你也可以试试ConstraintLayout
    【解决方案5】:

    问题不在于被拖动的视图在 TextView 后面,而是被拖动的视图没有被绘制到 RecyclerView 的边界之外。效果是一样的。

    要允许 RecyclerView 在自身之外绘制,请为 RecyclerViewparent 设置以下内容:

    android:clipChildren="false"
    

    对于RecyclerView

    android:clipToPadding="false"
    

    这应该可以解决问题。

    【讨论】:

      【解决方案6】:

      RecyclerView 位于 FrameLayout 中的文本视图上方。目前,基础TextView 似乎在RecyclerView 之上,因为您已经为RecyclerView 提供了背景:

      android:background="@android:color/transparent"
      

      要使其正常工作,请删除此行或将RecyclerView 的背景更改为透明以外的任何值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 2016-03-30
        • 1970-01-01
        • 1970-01-01
        • 2010-12-08
        相关资源
        最近更新 更多