java - RecyclerView LinearLayout 始终出现在其他元素之上

标签 java android android-recyclerview android-linearlayout

我有一个名为消息的屏幕,它向用户显示最近消息的列表。在我的 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" />

基本上,这段代码创建了一条水平线。在该水平线下方,我想显示用户最近的消息。

在水平线代码块下方,我有以下代码:

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

这负责显示用户最近的消息。

现在在我的 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();

此代码用于测试目的,但它可以工作。它以线性顺序显示所有用户的消息,我可以滚动浏览它们。我唯一的问题是当我运行程序时,最近的消息不会在水平线以下开始。有些消息位于水平线上方和其他元素的顶部。我认为第一条消息跳转到位置 (0,0)。

我该如何解决这个问题?我希望我的消息以现在的方式显示,就在水平线下方。

最佳答案

更改此:

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

这里发生了两个重大变化:

  • ConstraintLayout 不完全支持 match_parent,因此我们使用 0dp(表示“匹配约束”)。
  • 为所有四个边添加约束。开始/结束约束使 View 水平填充屏幕,顶部/底部约束使 View 填充从线下方到屏幕底部的所有内容。

关于java - RecyclerView LinearLayout 始终出现在其他元素之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54001366/

相关文章:

Java - 如何使用 CompareTo 方法将大写字母排在小写字母之前

java - GPS追踪比较(Android)

android - 需要帮助在 Android Nougat 中调试 SSLHandshakeException

android-recyclerview - 改造 2 : wait for response before executing next task in recyclerview

java - 从 Java FileInputStream 读取字节时如何区分 -1 和 0xff?

java - GXT 如何在 XTemplate 中显示树对象?

java - 安卓 : get all JSON values froma a certain category

android - 构建 Android 应用程序时出错(cordova-plugin-crosswalk-webview)

java - RecyclerViewAdapter OnBindViewHolder 的 ID 为空

android - 如何修复 RecyclerView requireFadingEdge 和 ClipToOutline 故障