RecyclerView嵌套在NestedScrollView中,每个项目都会调用onCreateViewHolder方法。

5

我有一个情况,需要在NestedScrollView中嵌套RecyclerView。问题是在初始化方法onCreateViewHolderonBindViewHolder被调用时,对列表中的每个项目(例如100个项目)进行了调用。

因此,屏幕上没有视图回收,在更复杂的情况下,性能存在很大问题。

我的Activity代码:

public class MyActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(new MyListAdapter());
}
}

我的.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/refresh"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="false"/>

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

我的适配器的代码:

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.Holder>{

@Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
    Log.d("TESTING", "onCreate: " + view.hashCode());
    return new Holder(view);
}

@Override
public void onBindViewHolder(Holder holder, int position) {
    Log.d("TESTING", "onBind position: " + position + ", view: " + holder.view.hashCode());
    holder.setContent(position % 2 == 0 ? R.color.colorPrimary : R.color.colorAccent);
}

@Override
public int getItemCount() {
    return 100;
}

class Holder extends RecyclerView.ViewHolder {

    private View view;

    public Holder(View itemView) {
        super(itemView);
        view = itemView;
    }

    public void setContent(int colorRes) {
        view.setBackgroundResource(colorRes);
    }
}
}

在初始化期间,我有以下日志:

    onCreate: 147045034
    onBind position: 0, view: 147045034
    onCreate: 235006520
    onBind position: 1, view: 235006520
    onCreate: 16439158
    onBind position: 2, view: 16439158
    onCreate: 84373988
    onBind position: 3, view: 84373988
    onCreate: 146076930
    onBind position: 4, view: 146076930
    onCreate: 12306512
    onBind position: 5, view: 12306512
    onCreate: 167862094
    onBind position: 6, view: 167862094
    onCreate: 220010876

    ... logs for items 7 - 94

    onCreate: 189894540
    onBind position: 95, view: 189894540
    onCreate: 121777898
    onBind position: 96, view: 121777898
    onCreate: 38672504
    onBind position: 97, view: 38672504
    onCreate: 210522038
    onBind position: 98, view: 210522038
    onCreate: 243811364
    onBind position: 99, view: 243811364

请查看https://dev59.com/XFoU5IYBdhLWcg3wk3l2 - ADM
@ADM 我尝试按照这个答案中所说的更改属性 android:layout_height,但是没有帮助。 - Pablo
2个回答

5
我之前遇到了这个问题,从我的研究结果来看,如果要设置这样的布局,那么RecyclerView实际上是无法循环利用视图的。RecyclerView通过询问其父视图的大小来计算需要显示多少视图。
ScrollView要求其子项填充所有视图,以便计算自己的高度,并查看应该能够滚动多少。这会导致冲突,意味着RecyclerView将填充所有子视图,这基本上使RecyclerView没用了。我的解决方法是将所有元素作为RecyclerView的一部分,并为不同的视图类型设置不同的视图。从而消除了ScrollView。

1
不幸的是,我有一个案例,无法摆脱ScrollView :( - Pablo

0

我通过在RecyclerView中包装多个额外的布局来解决了这个问题。在我的情况下,使用NestedScrollView的原因是为了支持AppbarLayout和嵌套滚动行为。我将一个片段加载到NestedScrollView中,该片段通常包含一个RecyclerView。

关键似乎是ConstraintLayout与FrameLayout的结合。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent_nested_scroll"
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:id="@+id/fragmentHolder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent">

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

            </FrameLayout>

        </android.support.constraint.ConstraintLayout>

    </LinearLayout>

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接