即使RecyclerView.findViewHolderForAdapterPosition()在该位置返回null,也不会在该位置的视图上调用onBindViewHolder() [英] onBindViewHolder() is never called on view at position even though RecyclerView.findViewHolderForAdapterPosition() returns null at that position

查看:174
本文介绍了即使RecyclerView.findViewHolderForAdapterPosition()在该位置返回null,也不会在该位置的视图上调用onBindViewHolder()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含13个项目的列表(尽管可以添加或删除项目),位置为0-12.当第一次显示包含RecyclerView的片段时,用户只能看到位置0到7(位置7只能看到一半).在我的适配器中,每次绑定/绑定视图持有人时(如果此处应用语法,则为)并记录其位置.

I have a list with 13 items (although items may be added or removed), positions 0-12. When the fragment containing the RecyclerView is first shown, only positions 0 through 7 are visible to the user (position 7 being only half visible). In my adapter I Log every time a view holder is binded/bound (idk if grammar applies here) and record its position.

适配器

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    Log.d(TAG, "onBindViewHolder() position: " + position);
    ...
}

从我的Log中,我看到位置0-7被绑定:

From my Log I see that positions 0-7 are bound:

我有一个selectAll()方法,该方法通过适配器位置获取每个ViewHolder.如果返回的holder不是null,则使用返回的holder更新视图以显示它已被选中.如果返回的持有者是null,则我调用selectOnBind(),该方法会标记该位置更新处的视图,以使其在绑定时(而不是实时)显示为选中状态,因为当前未显示:

I have a selectAll() method that gets each ViewHolder by adapter position. If the returned holder is NOT null I use the returned holder to update the view to show it's selected. If the returned holder IS null I call selectOnBind() a method that flags the view at that position update to show it's selected when it's binded rather than in real time since it's not currently shown:

public void selectAll() {
    for (int i = 0; i < numberOfItemsInList; i++) {
        MyAdapter.ViewHolder holder = (MyAdapter.ViewHolder)
                mRecyclerView.findViewHolderForAdapterPosition(i);

        Log.d(TAG, "holder at position " + i + " is " + holder);

        if (holder != null) {
            select(holder);
        } else {
            selectOnBind(i);
        }
    }
}

在此方法中,我Log holder及其位置:

In this method I Log the holder along with its position:

到目前为止,一切似乎都很正常.我们有0-7位显示,根据Log,这些位置是绑定的.当我按selectAll()而不更改可见视图(滚动)时,我看到位置0-7被定义,而8-12是null.到目前为止一切顺利.

So up to this point everything seems normal. We have positions 0-7 showing, and according to the Log these are the positions bound. When I hit selectAll() without changing the visible views (scrolling) I see that positions 0-7 are defined and 8-12 are null. So far so good.

在这里变得很有趣.如果在调用selectAll()之后我进一步向下滚动,列表位置8和9不会显示它们已被选中.

Here's where it gets interesting. If after calling selectAll() I scroll further down the list positions 8 and 9 do not show they are selected.

在检查Log时,我发现这是因为即使它们被报告为null,也从未绑定它们:

When checking the Log I see that it's because they are never bound even though they were reported to be null:

更令人困惑的是,这种情况并非每次都会发生.如果我首先启动该应用程序并对其进行测试,则可能会起作用.但是事后似乎确实会发生.我猜这与回收的视图有关,但是即使如此,它们是否也不必绑定?

Even more confusing is that this does not happen every time. If I first launch the app and test this it may work. But it seems to happen without fail afterwards. I'm guessing it has something to do with the views being recycled, but even so wouldn't they have to be bound?

编辑(6-29-16)
更新AndroidStudio后,我似乎无法重现该错误.它按我的预期工作,绑定了空视图.如果这个问题应该浮出水面,我将返回这篇文章.

EDIT (6-29-16)
After an AndroidStudio update I cannot seem to reproduce the bug. It works as I expected it to, binding the null views. If this problem should resurface, I will return to this post.

推荐答案

发生这种情况的原因是:

This is happening because:

  • 视图未添加到recyclerview中(getChildAt将不起作用,并且将在该位置返回null)
  • 它们也被缓存(不会调用onBind)
  • The views are not added to the recyclerview (getChildAt will not work and will return null for that position)
  • They are cached also (onBind will not be called)

呼叫recyclerView.setItemViewCacheSize(0)将解决此问题".

由于默认值为2(RecyclerView.Recycler中的private static final int DEFAULT_CACHE_SIZE = 2;),您将始终获得2个不会调用onBind但未添加到回收站的视图

Because the default value is 2 (private static final int DEFAULT_CACHE_SIZE = 2; in RecyclerView.Recycler), you'll always get 2 views that will not call onBind but that aren't added to the recycler

这篇关于即使RecyclerView.findViewHolderForAdapterPosition()在该位置返回null,也不会在该位置的视图上调用onBindViewHolder()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆