如何在 RecyclerView 中获取特定位置视图? [英] How to get specific position view in RecyclerView?

查看:46
本文介绍了如何在 RecyclerView 中获取特定位置视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 recyclerView.recyclerView 项目之一具有 onClick 方法.在这个方法中,我setVisibility 将一个Item 设置为Visible(默认没有),同时获取位置并将其添加到arrayList.

I have a recyclerView. One of recyclerView items has an onClick method. In this method, I setVisibility for one Item to Visible(default is gone), also get the position and add it to an arrayList.

点击:

public void onClickImageView(ImageView imageView, int position) {
    imageView.setVisibility(View.VISIBLE);
    positionArrayList.add(position);
}

在我去另一个活动之前,我想将 ImageView 可见性重置为默认值(消失),所以我使用一个接口将 positionArrayList 发送到 recyclerView 活动以按位置获取视图并将 ImageViews 重置为默认值.

Before i go to another activity, I want to the ImageView visibility be reset to default (gone) so I use an interface to send positionArrayList to the recyclerView activity to get views by position and reset ImageViews to default.

界面:

public interface RecyclerViewMainActivityImp {
    void resetImageViewToDefault(ArrayList<Integer> positionArrayList);
}

我在适配器中使用接口方法的地方:

public void OnClickNextActivity() {
    Intent intent = new Intent();
    intent.setClass(context, ClassActivity.class);
    context.startActivity(intent);
    recyclerViewInterface.resetImageViewToDefault(positionArrayList);
}

活动:

@Override
public void resetImageViewToDefault(ArrayList<Integer> positionArrayList) {
    for (int position = 0; position < positionArrayList.size(); position++) {
        View row = binding.recyclerView.getLayoutManager().findViewByPosition(positionArrayList.get(position));
        ImageView imageView = row.findViewById(R.id.imageView);
        imageView.setVisibility(View.GONE);    
    }
}

当我滚动 recyclerView 时,如果某些项目不再可见,它们的视图将变为 null,因此 positionArrayList 中的某些位置不再有它们的视图.

When i scroll the recyclerView, and if some items are not in view anymore their views going to null so some positions in positionArrayList no longer have their view.

我的问题是滚动后某些视图为空,我不知道如何访问它们.

My problem is some views are null after scrolling and I don't know how to get access to them.

我在谷歌上搜索并找到了一些解决方案,但我无法解决我的问题

I searched on google and found some solutions but I could not solve my problem

注意:我使用了数据绑定

推荐答案

你可以尝试解决这个问题,

there's workaround you can try for this,

在您的活动中创建一个函数

create a function in your activity

function void updateRecyclerView(position){
   yourArrayListForVisibility.set(position,'hide');
   mAdapter.notifyDataSetChanged();
}


@Override
public void onResume(){
    super.onResume();
    mAdapter.notifyDataSetChanged();

}

在意图之前在您的适配器中调用此函数,并在您的适配器中检查此数组列表值以隐藏或显示内容,始终将每个位置的默认值设置为yourArrayListForVisibility"

call this function in your adapter before intent and in your adapter check for this arraylist value to hide or show content, always set default value to 'yourArrayListForVisibility' for every position

这篇关于如何在 RecyclerView 中获取特定位置视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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