简体   繁体   English

为什么将RecyclerView适配器包装在另一个类中会导致不同的行为?

[英]Why does wrapping my RecyclerView adapter in another class cause different behaviour?

I have come across an issue when using RecyclerViews. 使用RecyclerViews时遇到一个问题。 Basically during the initial load of a RecyclerView (after I start an activity) there is a small delay before items appear. 基本上,在RecyclerView的初始加载期间(在我开始一项活动之后),项目出现之前会有一个小的延迟。

After experimenting for a while I found a way to remove this delay by wrapping my adapter in another class as follows: 经过一段时间的试验,我发现了一种方法,可以通过将适配器包装在另一个类中来消除这种延迟,如下所示:

public class AdapterWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private RecyclerView.Adapter<RecyclerView.ViewHolder> mAdapter;

    public AdapterWrapper(RecyclerView.Adapter<RecyclerView.ViewHolder> adapter) {
        mAdapter = adapter;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return mAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        mAdapter.onBindViewHolder(holder, position);
    }

    @Override
    public int getItemCount() {
        return mAdapter.getItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        return mAdapter.getItemViewType(position);
    }
}

Then in my activity I have this: 然后在我的活动中,我有这个:

protected void onCreate(Bundle savedInstanceState) {
    ...
    setUpRecyclerView();
    ...
}

public void setUpRecyclerView() {
    mAdapter = new MyAdapter(this, mCursor);
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    //mRecyclerView.setAdapter(mAdapter);                   // This causes a small delay.
    mRecyclerView.setAdapter(new AdapterWrapper(mAdapter)); // This doesn't
}

This seems really weird to me and I have no idea why the behaviour is different. 这对我来说真的很奇怪,我也不知道为什么行为不同。 Has anybody got any potential theory to explain this? 有没有人有任何潜在的理论来解释这一点?

Extra information: 额外的信息:
-I'm using a cursor loader to provide data to my adapter. -我正在使用游标加载器向适配器提供数据。
-My adapter is subclassed using a CursorRecyclerAdapter found at http://quanturium.github.io -我的适配器使用可在http://quanturium.github.io找到的CursorRecyclerAdapter进行子类化

You have to override every method, not just implement the abstract methods. 您必须重写每个方法,而不仅仅是实现抽象方法。

When you call your overriden methods on AdapterWrapper it's getting passed to the wrapped object. 当您在AdapterWrapper上调用重写的方法时,它将被传递给包装的对象。 Every other method is going to your wrapper and not getting passed on. 其他所有方法都将传递给您的包装器,并且不会继续传递。

If im right, your premise may be a red herring. 如果说得对,您的前提可能是鲱鱼。 Delays like this can be—and usually are—because you are performing updates to the ui from a background thread. 这样的延迟可能是(通常是)延迟,因为您正在从后台线程执行ui的更新。

We had a similar issue and finally tracked it dowm to that being rhe case. 我们遇到了类似的问题,并最终跟踪到该情况。 Moving the code back to the main thread, everything became instant again. 将代码移回主线程,一切再次变得瞬间。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM