【问题标题】:Laggy RecyclerViewLaggy RecyclerView
【发布时间】:2018-02-01 19:47:43
【问题描述】:

我的 recyclerview 滚动很慢。当我通过触摸 Recyclerview 开始滚动时它会滞后,但从它上方的视图开始时不会滞后。 我还在 recyclerview 上禁用了嵌套滚动。

这是我的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:overScrollMode="never">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="HAHHAHA"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/mRecyclerViewMain"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:background="@android:color/transparent">

            </android.support.v7.widget.RecyclerView>


        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>


</FrameLayout>

这是延迟的video。这就像滚动正在跳过一些布局。

【问题讨论】:

  • setNestedScrollView = false 在回收站视图中
  • @PriteshVadhiya 我在 OnCreate() 方法中将嵌套滚动设置为 false。 mRecyclerViewMain.setNestedScrollingEnabled(false);
  • 你试过this solution
  • 使用 .setHasFixedSize(true) 设置 recyclerview 可以稍微提高性能。谢谢

标签: android android-recyclerview nestedscrollview


【解决方案1】:

像这样修改你的xml:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto">


                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/mRecyclerViewMain"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        app:layout_behavior="@string/appbar_scrolling_view_behavior"
                        android:background="@android:color/transparent">

                    </android.support.v7.widget.RecyclerView>

        </FrameLayout>

并在 recyclerView 的标题中设置您的 textView,它将解决您的问题

<TextView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:text="HAHHAHA"/>

在适配器类中为标题和项目列表设置 viewType

@Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (layoutInflater == null)
            layoutInflater = LayoutInflater.from(parent.getContext());

        if (viewType == 0) {
            // bind your headerview
        }
        if (viewType == 1) {
            //bind your recyclerview
        } 
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderHolder)
            // viewHolder for header view
        if (holder instanceof ListHolder) {
            // viewholder for list
        } 
    }

使用这个你可以传递 viewType

@Override
    public int getItemViewType(int position) {

        if (position == 0)
            return 0;
        else if (ArrayList.size() == 0)
            return 1;

    }

【讨论】:

  • 通过在 recyclerview 中设置 .setHasFixedSize(true),性能会好一点,无论如何谢谢
猜你喜欢
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
相关资源
最近更新 更多