簡體   English   中英

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

我該如何解決? 我希望我的消息以它們現在的樣子出現在水平線的下方。

更改此:

 <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 (這意味着“匹配約束”)。
  • 為所有四個方面添加約束。 開始/結束約束使視圖水平填充屏幕,頂部/底部約束使視圖填充屏幕下方和底部的所有內容。

這只是約束問題。 您尚未為recyclerview設置約束,嘗試添加約束並再次運行它將起作用

您可以使用android:orientation =“ vertical”將兩個視圖的第一個視圖和滾動視圖都包裝在LinearLayout中,或者可以添加約束(如果根視圖是ConstraintLayout)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM