RecyclerView.Adapter的onCreateViewHolder被调用两次或更多次,多次

时间:2015-09-21 21:27:20

标签: android android-recyclerview android-viewholder

我目前正在开发一个使用RecyclerView的应用程序。

在审核LogCat时,我注意到 onCreateViewHolder 在实例化后被调用了两次。

09-22 05:22:55.209 V/Adapter﹕ Construct
09-22 05:22:55.213 V/Adapter﹕ onCreateViewHolder
09-22 05:22:55.224 V/Adapter﹕ onBindViewHolder
09-22 05:22:55.240 V/Adapter﹕ onCreateViewHolder
09-22 05:22:55.247 V/Adapter﹕ onBindViewHolder

同样onBindViewHolder被调用了两次,虽然我知道每当项目被回收时都会被调用。

但我认为onCreateViewHolder只需调用一次即可。

这是不正常的行为吗?如果是这样,怎么解决?

3 个答案:

答案 0 :(得分:19)

这不是异常,而是非常正常的行为。你不必担心。

ViewHolder确实会被回收,不会再次重新创建。

但是,在应用程序的屏幕上显示多个项目需要多个ViewHolders 。因此会创建一定数量的ViewHolders,因此onCreateViewHolder会被调用一段时间。

只要数据重置为ViewHolders之一,就会无限调用onBindViewHolder。

我用下面的测试应用程序检查了这个事实:

MainActivity:

public class MainActivity extends AppCompatActivity {
    private static final String[] DATASET = new String[]{
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
            "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
    };

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

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setHasFixedSize(true);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        MyAdapter myAdapter = new MyAdapter(DATASET);
        recyclerView.setAdapter(myAdapter);
    }
}

MyAdapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private static final String LOG_TAG = "RecyclerViewAdapter";
    private String[] dataset;

    private int counterOnCreateViewHolder = 0;
    private int counterOnBindViewHolder = 0;

    public MyAdapter(String[] dataset) {
        Log.d(LOG_TAG, "Construct");
        this.dataset = dataset;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public ViewHolder(TextView textView) {
            super(textView);
            this.textView = textView;
        }
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Log.d(LOG_TAG, "onCreateViewHolder (" + ++counterOnCreateViewHolder + ")");
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_textview, parent, false);

        ViewHolder viewHolder = new ViewHolder((TextView) view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.d(LOG_TAG, "onBindViewHolder (" + ++counterOnBindViewHolder + ")");
        holder.textView.setText(dataset[position]);
    }

    @Override
    public int getItemCount() {
        // Log.d(LOG_TAG, "getItemCount");
        return dataset.length;
    }
}

布局/ activity_main.xml中:

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:id="@+id/recycler_view" />

布局/ adapter_textview.xml:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/adapter_textview"
    android:textSize="30sp" />

结果:

09-22 06:03:04.166 D/RecyclerViewAdapter﹕ Construct
09-22 06:03:05.179 D/RecyclerViewAdapter﹕ onCreateViewHolder (1)
09-22 06:03:05.183 D/RecyclerViewAdapter﹕ onBindViewHolder (1)
09-22 06:03:05.190 D/RecyclerViewAdapter﹕ onCreateViewHolder (2)
09-22 06:03:05.192 D/RecyclerViewAdapter﹕ onBindViewHolder (2)
09-22 06:03:05.192 D/RecyclerViewAdapter﹕ onCreateViewHolder (3)
09-22 06:03:05.194 D/RecyclerViewAdapter﹕ onBindViewHolder (3)
09-22 06:03:05.195 D/RecyclerViewAdapter﹕ onCreateViewHolder (4)
09-22 06:03:05.197 D/RecyclerViewAdapter﹕ onBindViewHolder (4)
09-22 06:03:05.198 D/RecyclerViewAdapter﹕ onCreateViewHolder (5)
09-22 06:03:05.199 D/RecyclerViewAdapter﹕ onBindViewHolder (5)
09-22 06:03:05.200 D/RecyclerViewAdapter﹕ onCreateViewHolder (6)
09-22 06:03:05.202 D/RecyclerViewAdapter﹕ onBindViewHolder (6)
09-22 06:03:05.203 D/RecyclerViewAdapter﹕ onCreateViewHolder (7)
09-22 06:03:05.204 D/RecyclerViewAdapter﹕ onBindViewHolder (7)
09-22 06:03:05.206 D/RecyclerViewAdapter﹕ onCreateViewHolder (8)
09-22 06:03:05.207 D/RecyclerViewAdapter﹕ onBindViewHolder (8)
09-22 06:03:05.209 D/RecyclerViewAdapter﹕ onCreateViewHolder (9)
09-22 06:03:05.211 D/RecyclerViewAdapter﹕ onBindViewHolder (9)
09-22 06:03:05.212 D/RecyclerViewAdapter﹕ onCreateViewHolder (10)
09-22 06:03:05.213 D/RecyclerViewAdapter﹕ onBindViewHolder (10)
09-22 06:03:05.215 D/RecyclerViewAdapter﹕ onCreateViewHolder (11)
09-22 06:03:05.217 D/RecyclerViewAdapter﹕ onBindViewHolder (11)
09-22 06:03:05.218 D/RecyclerViewAdapter﹕ onCreateViewHolder (12)
09-22 06:03:05.220 D/RecyclerViewAdapter﹕ onBindViewHolder (12)
09-22 06:03:55.048 D/RecyclerViewAdapter﹕ onCreateViewHolder (13)
09-22 06:03:55.050 D/RecyclerViewAdapter﹕ onBindViewHolder (13)
09-22 06:03:55.228 D/RecyclerViewAdapter﹕ onCreateViewHolder (14)
09-22 06:03:55.229 D/RecyclerViewAdapter﹕ onBindViewHolder (14)
09-22 06:03:55.230 D/RecyclerViewAdapter﹕ onCreateViewHolder (15)
09-22 06:03:55.231 D/RecyclerViewAdapter﹕ onBindViewHolder (15)
09-22 06:03:55.232 D/RecyclerViewAdapter﹕ onBindViewHolder (16)
09-22 06:03:55.232 D/RecyclerViewAdapter﹕ onBindViewHolder (17)
09-22 06:03:55.260 D/RecyclerViewAdapter﹕ onBindViewHolder (18)
09-22 06:03:55.276 D/RecyclerViewAdapter﹕ onBindViewHolder (19)
09-22 06:03:55.296 D/RecyclerViewAdapter﹕ onBindViewHolder (20)
09-22 06:03:55.310 D/RecyclerViewAdapter﹕ onBindViewHolder (21)

如您所见,仅在onCreateViewHolder (15)之后调用onBindViewHolder。

答案 1 :(得分:4)

以下是针对RecyclerView稳定视图的稳定答案

下面的这些行有助于垂直地在recyclerview中保持列表数据的一致性           在我的垂直列表中,只有10个我不想再次绑定的项目。           每行都有无限的水平RecyclerView列表。

  1. mRecyclerview.getRecycledViewPool()。setMaxRecycledViews(int viewtype,int itemsCount)

  2. mRecyclerview.setItemViewCacheSize(int itemsCount)

    这两种方法都有助于不回收垂直视图。       但是如果list太大,则会导致OOM错误,因此请谨慎实施。

    Here 0 is viewType in Recyclerview adapter and 10 are the items count.
    
    mRecyclerview.getRecycledViewPool().setMaxRecycledViews(0, 10);
    mRecyclerview.setItemViewCacheSize(10);
    

答案 2 :(得分:0)

您可以通过此代码增加RecycledViewPool计数。默认情况下它有5个。

你可以根据需要增加。

  

recyclerView.getRecycledViewPool()。setMaxRecycledViews(0,50); // 0 - 您的视图类型