RecyclerView获取按位置查看

时间:2018-07-12 11:56:48

标签: android android-recyclerview

我有一个RecyclerView,每个CardView包含一个TextView和一个ImageView。每当我单击一个项目时,我都希望将图像可见性设置为VISIBLE,并将上一次单击的项目图像的可见性设置为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上都有两个可见的图像:被单击项目的图像和上一个被单击项目的图像。我想我需要按其位置获取上一个视图,并手动将可见性设置为“不可见”。

4 个答案:

答案 0 :(得分:1)

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

,我们只能找到5个项目

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

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

    的Hashmap。
    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<>();
    }
    
  2. ,然后将其保存在Hashmap中

    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if(!holderlist.containsKey(position)){
            holderlist.put(position,holder);
        }
    
  3. 在适配器中创建方法。

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

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

    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);
    
    }
    

答案 1 :(得分:0)

我已经创建了演示,您可以参考该演示并实现单选

 recyclerView.setAdapter(new RecyclerView.Adapter() {
        int selected_position = -1;

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new RecyclerView.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.row_color_list,parent,false)) {
                @Override
                public String toString() {
                    return super.toString();
                }
            };
        }

        @Override
        public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
            ImageView imageView = holder.itemView.findViewById(R.id.image);

            if(selected_position == position){
                imageView.setVisibility(View.VISIBLE);
            }else {
                imageView.setVisibility(View.GONE);
            }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selected_position != holder.getAdapterPosition()){
                        selected_position = holder.getAdapterPosition();
                        notifyDataSetChanged();
                    }
                }
            });

        }

        @Override
        public int getItemCount() {
            return 20;
        }
    });

答案 2 :(得分:0)

您可以执行以下操作:

1)声明一个全局变量:

EVNTMST_ID

2)设置所选位置private int selectedPos = -100; // Put any irrelevant number you want

onClick()

3)检查选中的位置并在@Override public void onClick(View v) { selectedPos = getAdapterPosition(); } 内分配可见性:

onBindViewHolder()

答案 3 :(得分:0)

尝试此代码。 将此代码添加到适配器类中以处理click事件。

 OnItemClick onItemClick;

public void setOnItemClick(OnItemClick onItemClick) {
    this.onItemClick = onItemClick;
}

public interface OnItemClick {
    void getPosition(String data); //pass any data to shared it.
}

绑定方法之后。.

@Override
public void onBindViewHolder(final ItemViewHolder holder, final int position) {
    // below code handle click event on recycler view item.
    final String str=mStringList.get(position); //  here your boject
    if(holder.category.isChecked()==true){
        holder.imageViewCheck.setVisibility(View.VISIBLE);
        current_pos=position;
    } else {
        holder.imageViewCheck.setVisibility(View.INVISIBLE);
    }
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClick.getPosition(str); // pass your data.
        }
    });
}

将适配器绑定到回收器视图后,这意味着适配器不为null,然后在下面的代码中调用。

        adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
        @Override
        public void getPosition(String data) {
            // hear update your value for check into if condition.
            data="sdfsdf";

            adpater.notifyDataSetChanged();
        }
    });

并阅读注释,还尝试创建custon类并访问该对象值并在单击它后进行更新。 这段代码仅介绍了如何处理进入回收站视图的click事件。