RecyclerView的内容在TextView上方并不覆盖。

huangapple go评论138阅读模式
英文:

RecyclerView content goes behind and not over TextView above

问题

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

Material Design重新排序列表

在视频中,您会看到一个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>
英文:

Consider this video from material design on drag & dropping items:

Material design reordering list

In the video you see a RecyclerView and above it a TextView with the text "Playlist". When the row is dragged up you see it going over the Playlist.

In my code the row goes behind the Textview. I've put RecyclerView's layout_height to match_parent. And I used a FrameLayout and placed the TextView below the RecyclerView. Why isn't it going over it?

&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:id=&quot;@+id/layoutRoot&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/txtTitle&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:padding=&quot;@dimen/general_margin&quot;
        android:text=&quot;@string/activity_bible_order_explanation&quot;
        android:layout_gravity=&quot;start|top&quot; /&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;@android:color/transparent&quot;
        android:layout_marginTop=&quot;96dp&quot;
        android:orientation=&quot;vertical&quot;
        app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot; /&gt;

&lt;/FrameLayout&gt;

RecyclerView的内容在TextView上方并不覆盖。

答案1

得分: 4

请使用约束布局作为父布局,并通过将顶部约束应用于文本视图来将文本视图添加到顶部。然后,将RecyclerView的顶部约束设置为文本视图的底部,并将RecyclerView的底部约束设置为父布局的底部,然后将高度设置为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>
英文:

Use Constraint layout as a parent layout and add textview at the top by giving top constraints to the textview. then add
recyclerview's top constraint to the the bottom of textview and recyclerview bottom constraint to the bottom of parent then set height 0 it will work

&lt;androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
             xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
             android:id=&quot;@+id/layoutRoot&quot;
             android:layout_width=&quot;match_parent&quot;
             android:layout_height=&quot;match_parent&quot;
             android:orientation=&quot;vertical&quot;&gt;

    &lt;TextView
            android:id=&quot;@+id/txtTitle&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            app:layout_constraintTop_toTopOf=&quot;parent&quot;
            android:padding=&quot;@dimen/general_margin&quot;
            android:text=&quot;@string/activity_bible_order_explanation&quot;
            /&gt;

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

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案2

得分: 4

你尝试过提升高度吗?问题可能是在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" <!-- 如果不起作用,请尝试更高的高度,如20dp等。 -->
        android:background="@android:color/transparent"
        android:layout_marginTop="96dp"
        android:orientation="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</FrameLayout>

希望对你有帮助 (: 请告诉我是否有效!

英文:

Have you tried elevation? the problem might be that in the z axis the TextView is higher than the RecyclerView. try adding android:elevation=&quot;10dp&quot; or higher... like this:

&lt;FrameLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:id=&quot;@+id/layoutRoot&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/txtTitle&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:padding=&quot;@dimen/general_margin&quot;
        android:text=&quot;@string/activity_bible_order_explanation&quot;
        android:layout_gravity=&quot;start|top&quot; /&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:elevation=&quot;10dp&quot; (if it doesn&#39;t work try maybe higher elevation, 20dp etc.)
        android:background=&quot;@android:color/transparent&quot;
        android:layout_marginTop=&quot;96dp&quot;
        android:orientation=&quot;vertical&quot;
        app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot; /&gt;

&lt;/FrameLayout&gt;

Hope it works for you (: let me know!

答案3

得分: 0

这是什么的父布局?

因为您在RecyclerView中设置了match_parent,而列表中有很多内容,所以出现了这个问题。

请提供固定高度或使用相对布局,并将文本视图的位置应用于RecyclerView中的上方。

英文:

What is the parent layout of this?

Because you set match_parent in recyclerview and list have many content that's why this issue facing.

Please give fix height or use relative layout and apply position above of text view in recyclerview

答案4

得分: 0

以下是翻译好的内容:

问题与您的布局代码有关:

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

解决方案:

  • 您应该使用 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>
英文:

Problem with your layout code:

  1. You're using a FrameLayout
  2. Inside FrameLayout, you're also using top margin on your RecyclerView
    android:layout_marginTop=&quot;96dp&quot; &lt;-- This causing the issue.

Solution:
<br>

  • You should LinearLayout instead of FrameLayout. And, also set LinearLayout's orientation as vertical. <br>
    android:orientation=&quot;vertical&quot;
  • Also remove this line from your RecyclerView:-<br>
    android:layout_marginTop=&quot;96dp&quot;

Solution Code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:id=&quot;@+id/layoutRoot&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/txtTitle&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:padding=&quot;16dp&quot;
        android:text=&quot;Hello World&quot;/&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;@android:color/transparent&quot;
        app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot; /&gt;

&lt;/LinearLayout&gt;

答案5

得分: 0

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

要允许 RecyclerView 在其外部绘制,请为 RecyclerViewparent 设置以下属性:

android:clipChildren="false"

对于 RecyclerView,设置:

android:clipToPadding="false"

这应该解决问题。

英文:

The issue is not that the dragged view is going behind the TextView but, rather, the dragged view is not being drawn outside the bounds of the RecyclerView. The effect is the same.

To allow the RecyclerView to draw outside of itself, set the following for the parent of the RecyclerView:

android:clipChildren=&quot;false&quot;

For the RecyclerView set

android:clipToPadding=&quot;false&quot;

That should solve the problem.

答案6

得分: 0

RecyclerView在您的FrameLayout中位于文本视图之上。目前,似乎底层的TextView位于RecyclerView之上,因为您将RecyclerView的背景设置为:

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

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

英文:

The RecyclerView is above the text view in your FrameLayout. Currently, it appears that the underlying TextView is above the RecyclerView as you have given the background for the RecyclerView as:

android:background=&quot;@android:color/transparent&quot;

To make this work as normal, either remove this line or change the background of RecyclerView to any value other than transparent.

答案7

得分: 0

有1个问题。您需要先放置回收视图。尝试这样做:

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

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

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="Hello World"/>

</LinearLayout>
英文:

There is 1 more issue. You need to place the recycler view first. Try this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:id=&quot;@+id/layoutRoot&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:background=&quot;@android:color/transparent&quot;
        app:layoutManager=&quot;androidx.recyclerview.widget.LinearLayoutManager&quot; /&gt;

    &lt;TextView
        android:id=&quot;@+id/txtTitle&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:padding=&quot;16dp&quot;
        android:text=&quot;Hello World&quot;/&gt;

&lt;/LinearLayout&gt;

huangapple
  • 本文由 发表于 2020年1月3日 19:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定