Java Android Recycler视图仅选择一个图像,并在所选图像上显示勾号

Java Android Recycler视图仅选择一个图像,并在所选图像上显示勾号,java,android,Java,Android,我需要一个解决方案,如何做以下事情使用Android回收视图 我有多个图像,我只需要选择一个图像,就像单选按钮的工作方式一样。现在我可以选择我拥有的所有图像,但我需要限制当前工作行为,使其像单选按钮一样工作。(例如,如果选择了一个,则不应选择我拥有的其他图像。 我尝试了下面的代码,但没有运气。任何人都可以纠正错误,使代码根据我的需要可行 public class StarCountAdapter extends RecyclerView.Adapter<StarCountAdapter.S

我需要一个解决方案,如何做以下事情使用Android回收视图

我有多个图像,我只需要选择一个图像,就像单选按钮的工作方式一样。现在我可以选择我拥有的所有图像,但我需要限制当前工作行为,使其像单选按钮一样工作。(例如,如果选择了一个,则不应选择我拥有的其他图像。

我尝试了下面的代码,但没有运气。任何人都可以纠正错误,使代码根据我的需要可行

public class StarCountAdapter extends RecyclerView.Adapter<StarCountAdapter.StarCountHolder> {
    Context context;
    LayoutInflater inflater;
    List<StarCount> starCounts = new ArrayList<>();

    public StarCountAdapter(Context context, List<StarCount> starCounts) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.starCounts = starCounts;
    }

    @Override
    public StarCountHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.star_count_row,parent,false);
        return new StarCountHolder(view);
    }

    @Override
    public void onBindViewHolder(StarCountHolder holder, int position) {
        StarCount model = starCounts.get(position);
        Picasso.with(context)
                .load("http://"+model.getImagePath())
                .into(holder.starImage);
        holder.actorName.setText(model.getName());
        holder.counts.setText(""+model.getCount());
    }

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

    public class StarCountHolder extends RecyclerView.ViewHolder {
        ImageView starImage;
        TextView actorName,counts;
        StarCount modelCount;
        public StarCountHolder(View itemView) {
            super(itemView);
            starImage = (ImageView) itemView.findViewById(R.id.starCountIv);
            actorName = (TextView) itemView.findViewById(R.id.acterName);
            counts = (TextView) itemView.findViewById(R.id.counts);
        }
    }
}
公共类StarCountAdapter扩展了RecyclerView.Adapter{
语境;
充气机;
List starCounts=new ArrayList();
公共StarCountAdapter(上下文上下文,列出starCounts){
this.context=上下文;
this.inflater=layoutiner.from(上下文);
this.starCounts=starCounts;
}
@凌驾
public StarCountHolder onCreateViewHolder(视图组父级,int-viewType){
视图=充气机。充气(右布局。星形计数行,父级,false);
返回新的StarCountHolder(视图);
}
@凌驾
BindViewHolder上的公共无效(StarCountHolder,int位置){
StarCount model=starCounts.get(位置);
毕加索。与(上下文)
.load(“http://”+model.getImagePath())
.进入(支架.星像);
holder.actorName.setText(model.getName());
holder.counts.setText(“+model.getCount());
}
@凌驾
public int getItemCount(){
返回starCounts.size();
}
公共类StarCountHolder扩展了RecyclerView.ViewHolder{
ImageView星图;
TextView actorName,计数;
星计数模型计数;
公共StarCountHolder(查看项目视图){
超级(项目视图);
starImage=(ImageView)itemView.findViewById(R.id.starCountIv);
actorName=(TextView)itemView.findViewById(R.id.acterName);
counts=(TextView)itemView.findViewById(R.id.counts);
}
}
}
因为我已经工作了一天多,所以需要帮助来解决这个问题。分享想法,纠正我在代码中的错误,并纠正我的错误

public int selectedPosition  = -1   
public class StarCountHolder extends RecyclerView.ViewHolder {
    ImageView starImage;
    TextView actorName,counts;
    StarCount modelCount;
    public StarCountHolder(View itemView) {
        super(itemView);
        starImage = (ImageView) itemView.findViewById(R.id.starCountIv);
        actorName = (TextView) itemView.findViewById(R.id.acterName);
        counts = (TextView) itemView.findViewById(R.id.counts);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedPosition = getLayoutPosition());
                notifyDatasetChanged();
            }
        });
    }
}
现在您已完成选定项目的位置,请在活页夹中更改它

@Override
public void onBindViewHolder(StarCountHolder holder, int position) {
    StarCount model = starCounts.get(position);
    Picasso.with(context)
            .load("http://"+model.getImagePath())
            .into(holder.starImage);
    holder.actorName.setText(model.getName());
    holder.counts.setText(""+model.getCount());
    if(selectedPosition == position){
       // do whatever you want to do to make it selected.
    }
}
现在要在活动中获取所选项目,您可以执行以下操作

内部活动


希望它有帮助

您刚刚获得单选按钮位置值,如true或false。
StartCount startC = starCounts.get(adapter.selectedPosition);