首页 > 解决方案 > Android/Java:当 RecyclerView 可见性从 GONE 变为 VISIBLE 时如何禁用自动滚动

问题描述

RecyclerView正如您在代码中看到的那样,我在 XML 文件中设置为已消失。

从服务器获取更新后,我想在设置RecyclerView可见性时显示它们GONEVISIBLE 它将向下滚动所有页面以达到此RecyclerView

我怎样才能解决这个问题或者这个有什么更好的解决方案可以使用ShimmerLayout和 ``RecyclerView``` 而不会发生这种情况。

这是我的代码的一部分:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        // Some rows are here
        // ...

        <LinearLayout
            android:id="@+id/news_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical">

            // Another Row

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center"
                android:orientation="horizontal">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/latest_news_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="5dp"
                    android:nestedScrollingEnabled="false"
                    android:visibility="gone" />

                <LinearLayout
                    android:id="@+id/news_shimmer_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical"
                    android:visibility="visible">

                    <include layout="@layout/shimmer_news_row" />

                    <include layout="@layout/shimmer_news_row" />

                    <include layout="@layout/shimmer_news_row" />

                    <include layout="@layout/shimmer_news_row" />

                    <include layout="@layout/shimmer_news_row" />

                    <include layout="@layout/shimmer_news_row" />

                </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</ScrollView>

标签: androidandroid-recyclerview

解决方案


启动活动时不要设置GONE使其默认为VISBILE。然后在回收站视图上加载数据,它不会回收


推荐阅读