简体   繁体   English

RecyclerView LinearLayout总是出现在其他元素的顶部

[英]RecyclerView LinearLayout always appearing on top of other elements

I have this screen called Messages that displays a list of recent messages to the user. 我有一个名为“ Messages屏幕,该屏幕显示给用户的最近消息列表。 In my activity_messages.xml file I have this bit of code: 在我的activity_messages.xml文件中,我有以下代码:

<View
    android:id="@+id/horizontal_line"
    android:layout_width="351dp"
    android:layout_height="1dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:background="@android:color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.47"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/Game_Button"
    app:layout_constraintVertical_bias="0.216" />

Basically, this block of code creates a horizontal line. 基本上,这段代码创建一条水平线。 Below that horizontal, I want to display the user's recent messages. 在该水平下方,我想显示用户的最近消息。

Below that horizontal line block of code, I have this code: 在该水平线代码块下方,我有以下代码:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="64dp"
    tools:layout_editor_absoluteY="168dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical">
    </android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>

This takes care of displaying the user's recent messages. 这样可以显示用户的最近消息。

Now in my Messages.java file I have this bit of code: 现在在我的Messages.java文件中,我有以下代码:

mMessagesLayoutManager = new LinearLayoutManager(Messages.this);
mRecyclerView.setLayoutManager(mMessagesLayoutManager);
mMessagesAdapter = new MessagesAdapter(getDataSetMessages(), Messages.this);
mRecyclerView.setAdapter(mMessagesAdapter);

for(int i = 0; i < 100; i++) {
    MessagesObject obj = new MessagesObject(Integer.toString(i));
    resultsMessages.add(obj);
}
mMessagesAdapter.notifyDataSetChanged();

This code is for testing purposes, but it works. 该代码用于测试目的,但是可以工作。 It shows all the user's messages a linear order and I am able to scroll through them. 它以线性顺序显示所有用户的消息,我可以滚动查看它们。 My only problem is when I run the program, the recent messages don't start below the horizontal line. 我唯一的问题是,当我运行该程序时,最近的消息不会在水平线以下开始。 Some of the messages are above the horizontal line and on top of the other elements. 一些消息在水平线上方,在其他元素上方。 I think the first message jumps to position (0,0). 我认为第一个消息跳到位置(0,0)。

How do I fix this? 我该如何解决? I would like my messages to appear the way they are right now, just below the horizontal line. 我希望我的消息以它们现在的样子出现在水平线的下方。

Change this: 更改此:

 <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" tools:layout_editor_absoluteX="64dp" tools:layout_editor_absoluteY="168dp"> 

to this instead: 改为:

<android.support.v4.widget.NestedScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/horizontal_line"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">

Two major changes going on here: 这里发生了两个主要变化:

  • ConstraintLayout doesn't fully support match_parent , so we use 0dp (which means "match constraints") instead. ConstraintLayout不完全支持match_parent ,因此我们改用0dp (这意味着“匹配约束”)。
  • Add constraints for all four sides. 为所有四个方面添加约束。 The start/end constraints make the view fill the screen horizontally, and the top/bottom constraints make the view fill everything from below the line to the bottom of the screen. 开始/结束约束使视图水平填充屏幕,顶部/底部约束使视图填充屏幕下方和底部的所有内容。

it is just Constrain issue. 这只是约束问题。 You haven't set constraint for recyclerview try to add constrain and run again it will work 您尚未为recyclerview设置约束,尝试添加约束并再次运行它将起作用

您可以使用android:orientation =“ vertical”将两个视图的第一个视图和滚动视图都包装在LinearLayout中,或者可以添加约束(如果根视图是ConstraintLayout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM