RecyclerView获取按位置查看 [英] RecyclerView get View by position

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

问题描述

我有一个RecyclerView,每个CardView包含一个TextView和一个ImageView.每当我单击一个项目时,我都希望将图像可见性设置为VISIBLE,并将上次单击的项目图像的可见性设置为INVISIBLE.

I have a RecyclerView and each CardView contains a TextView and an ImageView. Whenever I click an item, I want to set the image visibility to VISIBLE and to set the previous clicked item image's visibility to INVISIBLE.

这是我的适配器类:

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder>{
private Context context;
private List<Category> lista;
private LayoutInflater layoutInflater;
private IncomeCategoryActivity activity;

private static final int CATEGORY_REQUEST=6;
private static final int ITEM_EDIT=1;
private static final int ITEM_DELETE=2;
private static final int EDIT_REQUEST=7;

private int current_pos=-1;


public CategoryAdapter(List<Category> lista, Context context, IncomeCategoryActivity activity) {
    this.context = context;
    this.lista = lista;
    this.activity=activity;
    layoutInflater=LayoutInflater.from(context);
}

@Override
public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=layoutInflater.inflate(R.layout.category_layout, parent, false);
    ViewHolder viewHolder=new ViewHolder(view, activity);

    return viewHolder;
}

@Override
public void onBindViewHolder(CategoryAdapter.ViewHolder holder, int position) {
    holder.imageView.setImageURI(lista.get(position).getUri());
    holder.textView.setText(lista.get(position).getCategory());
    holder.position = position;
    holder.category=lista.get(position);

    if(holder.category.isChecked()==true){
        holder.imageViewCheck.setVisibility(View.VISIBLE);
        current_pos=position;
    } else {
        holder.imageViewCheck.setVisibility(View.INVISIBLE);
    }
}

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

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener{
    public ImageView imageView;
    public TextView textView;
    public ImageView imageViewCheck;
    public int position;
    public Category category;
    public IncomeCategoryActivity activity;

    public ViewHolder(View itemView, IncomeCategoryActivity activity) {
        super(itemView);
        this.activity=activity;

        imageView=itemView.findViewById(R.id.customCategoryImageView);
        textView=itemView.findViewById(R.id.customCategoryTextView);
        imageViewCheck=itemView.findViewById(R.id.customCheckImageView);

        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);

    }

    @Override
    public void onClick(View v) {
        String aux=textView.getText().toString();
        if(aux=="CATEGORIE NOUĂ"){
            Intent intent=new Intent(context, CustomIncomeActivity.class);
            activity.startActivityForResult(intent, CATEGORY_REQUEST);
        }
        else{
            imageViewCheck.setVisibility(View.VISIBLE);
            int pozitie_check=getLayoutPosition();
            Intent intent=new Intent(context, AddIncomeActivity.class);
            intent.putExtra("categorie_venit", aux);
            intent.putExtra("position_check", pozitie_check);
            activity.setResult(Activity.RESULT_OK, intent);
            activity.finish();
        }

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Selectează acțiunea");
        MenuItem edit=menu.add(0, ITEM_EDIT, 0, "Modifică");
        MenuItem delete=menu.add(0, ITEM_DELETE, 0, "Șterge");

        edit.setOnMenuItemClickListener(this);
        delete.setOnMenuItemClickListener(this);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        int position=getLayoutPosition();
        if (item.getGroupId() == 0) {
            if(item.getItemId()==ITEM_EDIT){
                Category category=lista.get(position);
                Intent intent=new Intent(activity, CustomIncomeActivity.class);
                intent.putExtra("edit_icon", category.getUri());
                intent.putExtra("edit_category", category.getCategory());
                intent.putExtra("list_position", position);
                activity.startActivityForResult(intent, EDIT_REQUEST);
            }
            else if(item.getItemId()==ITEM_DELETE){
                lista.remove(position);
                notifyDataSetChanged();
            }
    }
    return true;

}

此刻,每当我单击一个项目时,RecyclerView上都有两个可见的图像:被单击项目的图像和上一个被单击项目的图像.我认为我需要按其位置获取上一个视图,并将可见性手动设置为不可见".

At this moment, whenever I click an item, there are two images VISIBLE on the RecyclerView: the clicked item's image and the previous clicked item's image. I think I need to get the previous View by its position and to manually set the visibility to INVISIBLE.

推荐答案

recycleView.getChildCount()recycleView_parent.getChildAt()仅提供适配器项,该项仅显示在屏幕上. 这意味着,如果您的列表中有200个项目,并且屏幕上仅显示5个项目,那么借助于 recycleView

recycleView.getChildCount() and recycleView_parent.getChildAt() only gives the Adapter items which is shows only screen . that means if your list has 200 items and the only 5 items shows on screen so we can find only 5 item with the help of recycleView

我正在使用一个简单的技巧解决问题.

i am using one simple trick to solve the issue.

  1. 您可以定义保存您的 holder对象

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    ArrayList<Model> dataList;
    Context context;
    HashMap<Integer,ViewHolder> holderlist;


public MyAdapter(ArrayList<Model> dataList, Context context) {
    this.context = context;
    this.dataList = dataList;
    holderlist = new HashMap<>();
}

  • ,然后将其保存在Hashmap中

  • and after that to save the holder in Hashmap

    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if(!holderlist.containsKey(position)){
            holderlist.put(position,holder);
        }
    

  • 在适配器中创建方法.

  • create a method in Adapter.

    public MyListAdapter.ViewHolder getViewByPosition(int position){ 返回holderlist.get(位置); }

    public MyListAdapter.ViewHolder getViewByPosition(int position) { return holderlist.get(position); }

    在您的活动中或需要时调用此方法.

    call this method from your Activity or whenever you want.

    for (int i = 0; i < datalList.size(); i++) {
                MyAdapter.ViewHolder holder = ((MyAdapter)recycleView.getAdapter()).getViewByPosition(i);
                View view = holder.itemView;
                TextView tv = view.findViewById(R.id.tv);
    
    }
    

  • 这篇关于RecyclerView获取按位置查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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