• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Recyclerview单品选择器

Android 来源:ANDROID_ROOKIE 4次浏览

我为我的RecyclerView商品添加了选择器背景,但我一次只能选择单件商品。即如果选择了一个项目,则不能选择其余的项目。我怎样才能做到这一点?Recyclerview单品选择器

这里是我的适配器类

public class VisaTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    Context context; 
    private List<VisaType> visaTypeList; 

    int selected_position; 


    public VisaTypeAdapter(Context context, List<VisaType> visaTypeList) { 
     this.context = context; 
     this.visaTypeList = visaTypeList; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.visa_type_row, parent, false); 
     return new jp.workjapan.adapters.VisaTypeAdapter.ViewHolder(view); 

    } 

    @Override 
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { 
     final VisaType a = visaTypeList.get(position); 
     final jp.workjapan.adapters.VisaTypeAdapter.ViewHolder viewHolder = (jp.workjapan.adapters.VisaTypeAdapter.ViewHolder) holder; 
     viewHolder.visa_eng.setText(a.getName_en()); 
     viewHolder.visa_jp.setText(a.getName_jp()); 


     final long id = a.getId(); 
     if (id == AppSingleTon.visaType) { 
      viewHolder.visa_layout.setSelected(true); 
     } else { 
      viewHolder.visa_layout.setSelected(false); 
     } 


     viewHolder.visa_layout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if (viewHolder.visa_layout.isSelected()) { 
        viewHolder.visa_layout.setSelected(false); 


       } else { 

        viewHolder.visa_layout.setSelected(true); 
        AppSingleTon.visaType = id; 
        selected_position = position; 


       } 


      } 
     }); 

    } 

} 


===========解决方案如下:

这是我的样品溶液:

public class VisaTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    Context context; 
    private List<VisaType> visaTypeList; 

    int selected_position; 


    public VisaTypeAdapter(Context context, List<VisaType> visaTypeList) { 
     this.context = context; 
     this.visaTypeList = visaTypeList; 
    } 

    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.visa_type_row, parent, false); 
     return new jp.workjapan.adapters.VisaTypeAdapter.ViewHolder(view); 

    } 

    @Override 
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { 
     final VisaType a = visaTypeList.get(position); 
     final jp.workjapan.adapters.VisaTypeAdapter.ViewHolder viewHolder = (jp.workjapan.adapters.VisaTypeAdapter.ViewHolder) holder; 
     viewHolder.visa_eng.setText(a.getName_en()); 
     viewHolder.visa_jp.setText(a.getName_jp()); 


     final long id = a.getId(); 
     if (id == AppSingleTon.visaType) { 
      viewHolder.visa_layout.setSelected(true); 
     } else { 
      viewHolder.visa_layout.setSelected(false); 
     } 

     if (position == selected_position) { 
      viewHolder.visa_layout.setSelected(true); 
     } else { 
      viewHolder.visa_layout.setSelected(false); 
     } 

     viewHolder.visa_layout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       selected_position = getAdapterPosition(); 
       notifyDataSetChanged(); 
      } 
     }); 

    } 

} 

希望它可以帮助你…… 感谢


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)