【问题标题】:RecyclerView scrolls up when items changeRecyclerView 在项目更改时向上滚动
【发布时间】:2022-01-01 12:52:48
【问题描述】:

当适配器提交新列表时,屏幕会向上滚动。提交新列表时如何不滚动屏幕?

obs: setHasFixedSize(false)

    <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/design_default_color_on_primary">
    

                   ...


        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/matchesRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/button1"
            tools:itemCount="5"
            tools:listitem="@layout/item_match" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    请试试这个

    如果您使用ListView - 覆盖以下方法。

    @Override
     public int getViewTypeCount() {    
         return getCount();
     }
    
     @Override
     public int getItemViewType(int position) {    
         return position;
     }
    

    如果您使用 RecyclerView - 仅覆盖 getItemViewType() 方法。

     @Override
     public int getItemViewType(int position) {    
         return position;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      相关资源
      最近更新 更多