RecyclerView点击更改图像(RecyclerView change image on click)

编程入门 行业动态 更新时间:2024-10-17 05:35:27
RecyclerView点击更改图像(RecyclerView change image on click)

我正在做汽车选择功能。 我有一个API,我从中获得车型,选择的图像链接和未选择的图像链接。 我正在使用带有文本和图像视图的recyclerview。 在创建recyclerview时,我选择第一个位置并显示hover-image以显示所选内容。 当我选择另一个图像时,它会更改为所选图像,但之前选择的图像不会更改为未选择的图像。 这意味着for循环不起作用。 但是当我打印未选择图像的url链接时,url链接显示正确。 那为什么它没有出现在毕加索。

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<Model> arrayList;
    private Context context;

    public MyAdapter(Context context, List<Model> arrayList) {
        this.arrayList = arrayList;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);

        return new MyViewHolder(itemView);
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView txt;
        ImageView image;
        LinearLayout LL_root;

        MyViewHolder(View view) {
            super(view);
            txt = (TextView) view.findViewById(R.id.txt);
            image = (ImageView) view.findViewById(R.id.image);
            LL_root = (LinearLayout) view.findViewById(R.id.LL_root);
            LL_root.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.LL_root:
                    int pos = getAdapterPosition();
                    System.out.println("sammy_onclick_position "+pos);



                    for(int i=0; i<arrayList.size(); i++){
                        if(pos!=i){
                            Picasso.with(context).load(arrayList.get(i).getImage()).into(image);
                            System.out.println("sammy_unselected_image "+arrayList.get(i).getImage());
                        }
                    }
                    Picasso.with(context).load(arrayList.get(pos).getSelectedImage()).into(image);
                    System.out.println("sammy_selected_image "+arrayList.get(pos).getSelectedImage());
                    break;
            }
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Model model = arrayList.get(position);
        holder.txt.setText(model.getTitle());
        if (!TextUtils.isEmpty(model.getImage())){
            if(position==0)
                Picasso.with(context).load(model.getSelectedImage()).into(holder.image);
            else
                Picasso.with(context).load(model.getImage()).into(holder.image);
        }

    }

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

I'm making ola like car selecting function. I've an API from which I'm getting car type, selected image link and unselected image link. I'm using recyclerview with text and image view for that. While creating the recyclerview I'm selecting first position and displaying hover-image to show selected. When I select another image it changes to selected image but the previously selected one doesn't changes to unselected image. This means that the for loop is not working. But when I printed the url link for the unselected images the url link shows correct. Then why its not showing in Picasso.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private List<Model> arrayList;
    private Context context;

    public MyAdapter(Context context, List<Model> arrayList) {
        this.arrayList = arrayList;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);

        return new MyViewHolder(itemView);
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView txt;
        ImageView image;
        LinearLayout LL_root;

        MyViewHolder(View view) {
            super(view);
            txt = (TextView) view.findViewById(R.id.txt);
            image = (ImageView) view.findViewById(R.id.image);
            LL_root = (LinearLayout) view.findViewById(R.id.LL_root);
            LL_root.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.LL_root:
                    int pos = getAdapterPosition();
                    System.out.println("sammy_onclick_position "+pos);



                    for(int i=0; i<arrayList.size(); i++){
                        if(pos!=i){
                            Picasso.with(context).load(arrayList.get(i).getImage()).into(image);
                            System.out.println("sammy_unselected_image "+arrayList.get(i).getImage());
                        }
                    }
                    Picasso.with(context).load(arrayList.get(pos).getSelectedImage()).into(image);
                    System.out.println("sammy_selected_image "+arrayList.get(pos).getSelectedImage());
                    break;
            }
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Model model = arrayList.get(position);
        holder.txt.setText(model.getTitle());
        if (!TextUtils.isEmpty(model.getImage())){
            if(position==0)
                Picasso.with(context).load(model.getSelectedImage()).into(holder.image);
            else
                Picasso.with(context).load(model.getImage()).into(holder.image);
        }

    }

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

最满意答案

您可以保留对适配器中所选位置的引用,然后检查是否在onBindViewHolder选择了当前项:

创建一个全局字段:

int selectedPos = 0;

在你onClick :

selectedPos = getAdapterPosition();

在你的onBindViewHolder中检查这种方式:

if(position == selectedPos) { Picasso.with(context).load(model.getSelectedImage()).into(holder.image); } else { Picasso.with(context).load(model.getImage()).into(holder.image); }

You can keep the reference to the selected position in your adapter and then check if the current item is selected in the onBindViewHolder :

Create a global field :

int selectedPos = 0;

In you onClick :

selectedPos = getAdapterPosition();

And in your onBindViewHolder do the check this way :

if(position == selectedPos) { Picasso.with(context).load(model.getSelectedImage()).into(holder.image); } else { Picasso.with(context).load(model.getImage()).into(holder.image); }

更多推荐

本文发布于:2023-08-07 01:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   RecyclerView   change   click   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!