【问题标题】:Why Recyclerview start scrolling up/down from half of the list?为什么 Recyclerview 从列表的一半开始向上/向下滚动?
【发布时间】:2019-08-13 12:45:49
【问题描述】:

实际上,我正在根据过滤数据列表更新“RecylerView”列表适配器。当我成功更新适配器数据但滚动从适配器列表的一半开始时。 有没有人遇到过这样的问题?

我已经尝试过的事情 - 将 Recyclerview 放入 RelativeLayout - 将 Recyclerview 高度设置为 wrap_content - 使用 requestLayout 方法

    <?xml version="1.0" encoding="utf-8"?>
    <layout 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">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/spinnerLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp45"
        android:background="@color/black_color"
        android:orientation="horizontal"
        android:paddingStart="@dimen/dp10"
        android:paddingEnd="@dimen/dp10"
        android:weightSum="2">

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="start|center"
            android:text="@string/statement_type"
            android:textAllCaps="true"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/subtitle"
            android:textStyle="bold"
            app:fontFamily="@string/fontBold" />

        <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/filterItemSpinner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </androidx.appcompat.widget.LinearLayoutCompat>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp45"
        android:layout_below="@+id/spinnerLayout"
        android:background="@color/colorAccent"
        android:orientation="horizontal"
        android:padding="@dimen/dp10"
        android:weightSum="3">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/txtdate"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.8"
            android:gravity="start|center"
            android:text="@string/lbl_date_particular"
            android:textAllCaps="true"
            android:textColor="@color/black_color"
            android:textSize="@dimen/bodyText"
            android:textStyle="bold" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/txtdr_cr"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.2"
            android:gravity="center"
            android:text="@string/lbl_dr_cr"
            android:textAllCaps="true"
            android:textColor="@color/black_color"
            android:textSize="@dimen/bodyText"
            android:textStyle="bold" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/txtbalance"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center|end"
            android:text="@string/balance"
            android:textAllCaps="true"
            android:textColor="@color/black_color"
            android:textSize="@dimen/bodyText"
            android:textStyle="bold" />
    </androidx.appcompat.widget.LinearLayoutCompat>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/contentLayout"
        tools:listitem="@layout/item_account_statement" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/txtEmptyView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/no_acc_statement_available"
        android:textAlignment="center"
        android:textColor="@color/colorPrimary"
        android:textSize="@dimen/sp18"
        android:visibility="gone" />

    <include
        android:id="@+id/includeLoaderLine"
        layout="@layout/custom_loaderview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    </RelativeLayout>
    </layout>

【问题讨论】:

  • 可能你的视图持有者的大小会随着它们变得可见而改变

标签: android android-recyclerview linearlayoutmanager


【解决方案1】:

您可以收听适配器更新并在检测到更改时滚动您的RecyclerView 到起始位置。

如果您使用的是 Java:

//Initializing your adapter 
MyAdapter myAdapter = new MyAdapter(...);

//Listening to adapter updates
myAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
    @Override
    public void onChanged() {

        //Scrolling to starting position
        recyclerView.getLayoutManager().scrollToPosition(0);
    }
});

//Setting your adapter on your RecyclerView
myRecyclerView.setAdapter(myAdapter);

...

如果您使用的是 Kotlin:

//Initializing your adapter 
var myAdapter = MyAdapter(...)

//Listening to adapter updates
myAdapter.registerAdapterDataObserver(object : 
        RecyclerView.AdapterDataObserver() {
    override fun onChanged() {

        //Scrolling to starting position
        myRecyclerView.layoutManager.scrollToPosition(0)
    }
})

//Setting your adapter on your RecyclerView
myRecyclerView.adapter = myAdapter

...

希望对您有所帮助! =)

【讨论】:

    猜你喜欢
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多