簡體   English   中英

Android Recycler 視圖滾動到底部

[英]Android Recycler view scroll to bottom

我試圖將回收器視圖滾動到底部,但問題是如果我的行項目高度大於屏幕高度,滾動停止在項目頂部。 是否可以手動滾動到回收站視圖的最底部?

recyclerView.scrollToPosition(adapter.getItemCount()-1); // does not work

它會為你工作。 在您的LayoutManager 中使用setReverseLayout=true並將其設置為您的recylcerView

final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    linearLayoutManager.setReverseLayout(true);

    recyclerView.setLayoutManager(linearLayoutManager);

此解決方案將有效,

添加一個NestedScrollView作為父對象,

即在您的Xml中

<android.support.v4.widget.NestedScrollView
 android:id="@+id/nsView"
 android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

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

現在在您的Java類中,將數據添加到recyclerview之后,使NestedScrollView滾動到底部。

nsView.fullScroll(View.FOCUS_DOWN);

希望對您有幫助。

在java中實際上列表是基於零的。 例如,列表 [0,1,2] 的大小為 3,但最后一個位置是 2,因為它從 0 開始。

recyclerView.scrollToPosition(items.size() - 1);

暫無
暫無

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

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