当recyclerview滚动时,findviewwithtag给出null [英] findviewwithtag gives null when recyclerview gets scrolled

查看:122
本文介绍了当recyclerview滚动时,findviewwithtag给出null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用recyclerView实现的自定义图片库,我想从其他片段中取消选择选定的项目.

I am having a custom gallery implemented with recyclerView, I want to deselect the selected items from other fragment.

为此,我将每个视图的标签设置为-

To do that I have set a tag to each view as -

@Override
    public void onBindViewHolder(GalleryAdapter.ViewHolder viewHolder, int i) {
        String gitem = galleryList.get(i);
        viewHolder.itemView.setTag(gitem); // Setting the tag
        ....
    }

要取消选择,我正在通过标签找到视图-

to deselect I am finding the view by tag as -

recyclerView_gallery.findViewWithTag(String.valueOf(showImageView.getTag())).performClick();

当图库recyclerview滚动在该图像上但在我向下滚动并转到该片段时,它正在工作- findViewWithTag 返回null,但是当我滚动到该图像并再次执行相同操作时,没有发现错误.

It is working when a gallery recyclerview scroll is on that Image but when I scroll down and go to that fragment - findViewWithTag is returning null, but when I scroll to that Image and again do same I found no error.

我已经尝试过-

recyclerView_gallery.getLayoutManager().scrollToPosition(position);
recyclerView_gallery.findViewWithTag(String.valueOf(showImageView.getTag())).performClick();

另外,我也尝试过-

recyclerView_gallery.findViewHolderForAdapterPosition(position);

但是结果是一样的,如果recyclerView滚动,它将无法正常工作.

but the result is the same, it not working if recyclerView gets scrolled.

推荐答案

Recycler视图仅比可见视图数多创建4或5个视图,并重新绑定/重用用户不可见的视图,因此称为RecyclerView.每次将视图与数据绑定时,将调用OnCreateView方法创建所需的视图,并调用onBindViewHolder.

Recycler view creates only 4 or 5 views more than the visible view count and rebinds/reuses the views that are not visible to user and hence it is called RecyclerView. OnCreateView method is called to create the required views and onBindViewHolder is called every time the view is bound with data.

public void onBindViewHolder(GalleryAdapter.ViewHolder viewHolder, int i) {
    String gitem = galleryList.get(i);
    viewHolder.itemView.setTag(gitem); // Setting the tag
    ....
}

当视图与数据"A"绑定时,,其标签为"A"并且当其被重新用于项目"B"时,其标签变为"B".因此,当您使用findViewByTag("A")时,它会返回null,因为标签已更改为"B".您可以通过将回收者视图包装在NestedRecyclerView中来禁用此功能,因为NestedRecyclerView会创建所需的所有视图,并且不会重复使用任何视图.(这很容易,但是我不推荐).

When the view is bound with data "A" , its tag is "A" and when it is reused for item "B", its tag becomes "B" and so when you uses findViewByTag("A"), it returns null because the tag has already changed into "B". You can disable this feature by wrapping the recycler view inside a NestedRecyclerView since NestedRecyclerView creates all the views required and there will not be no view reusing. (It is very easy, but I would not recommend it).

另一种方法是将所选项目/itemId保存在这样的位置.假设您的galleryList是一个字符串列表,

The other approach is to save the selected items/itemId somewhere like this. Suppose your galleryList is a String list,

private Set<String> selectedItemList = new HashSet<>();
private List<String> galleryList = new ArrayList<>();

public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String gItem = galleryList.get(position);
    holder.populateUi(gItem); //populate normal ui
    if (selectedItemList.contains(gItem)) {
        holder.markSelected(); //mark the ui as selected
    } else {
        holder.markUnSelected(); //mark the ui as unSelected
    }
}

我想您可以通过某种方式获得被单击项的位置,如果可以的话,可以按以下方式更新用户界面.

and I think you can get the position of the clicked item somehow, if so you may update the ui as follow.

public void notifySelected(int... positions) {
    for (int position:positions) {
        String gItem = galleryList.get(position);
        selectedItemList.add(gItem);
        notifyItemChanged(position);
    }
}

public void notifyUnSelected(int... positions) {
    for (int position:positions) {
        String gItem = galleryList.get(position);
        selectedItemList.remove(gItem);
        notifyItemChanged(position);
    }
}

如果您无法获得职位,而只能获得职位,则可以这样更改

If you can't get the position, but the item only, you may change like this

int index= galleryList.indexOf(item);
if(index>-1){
    selectedItemList.add(galleryList.get(index));
}

更好的方法是使用

The better approach is to use the itemChanged Method with payload as it is smoother. Sorry for code style, as I hadn't coded in Java for a long time.

这篇关于当recyclerview滚动时,findviewwithtag给出null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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