RecyclerView LinearLayout 总是出现在其他元素之上
RecyclerView LinearLayout always appearing on top of other elements
我有一个名为 Messages
的屏幕,它向用户显示最近消息的列表。在我的 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)。
我该如何解决这个问题?我希望我的消息按现在的样子显示,就在水平线下方。
这只是约束问题。
您还没有为 recyclerview 设置约束尝试再次添加约束和 运行
它会起作用
您可以使用 android:orientation="vertical" 将第一个视图和滚动视图包装在一个 LinearLayout 中,或者您可以添加约束(如果根视图是一个 ConstraintLayout)
改变这个:
<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
(也就是"match constraints")代替。
- 为所有四个边添加约束。 start/end 约束使视图水平填充屏幕,top/bottom 约束使视图填充从线下方到屏幕底部的所有内容。
我有一个名为 Messages
的屏幕,它向用户显示最近消息的列表。在我的 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)。
我该如何解决这个问题?我希望我的消息按现在的样子显示,就在水平线下方。
这只是约束问题。 您还没有为 recyclerview 设置约束尝试再次添加约束和 运行 它会起作用
您可以使用 android:orientation="vertical" 将第一个视图和滚动视图包装在一个 LinearLayout 中,或者您可以添加约束(如果根视图是一个 ConstraintLayout)
改变这个:
<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
(也就是"match constraints")代替。- 为所有四个边添加约束。 start/end 约束使视图水平填充屏幕,top/bottom 约束使视图填充从线下方到屏幕底部的所有内容。