recyclerview只突出显示一个项目

时间:2016-10-16 09:59:29

标签: android android-recyclerview

我有回收者视图,当我点击它时我突出显示该项目,问题是我可以突出显示许多项目,我不会只有一个项目,当我点击项目突出显示时,当我点击另一个项目时突出显示< / p>

这是MyAdapter

public class ScreenRecyclerAdapter  extends RecyclerView.Adapter<ScreenRecyclerAdapter.ViewHolder> {
Context context;
int image_list[];
ImageView image_view_screen_item;

public ScreenRecyclerAdapter(int[] image_list, Context context){
    super();
    this.image_list = image_list;
    this.context = context;}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.screen_items, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Picasso.with(context).load(image_list[position]).into(holder.image_view_screen_item);
     holder.list_row.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {

         }
     });
   }

@Override
public int getItemCount() {
    return image_list.length;
}
class ViewHolder extends RecyclerView.ViewHolder{
    ImageView image_view_screen_item , back;
    RelativeLayout list_row;
    public ViewHolder(View itemView) {
        super(itemView);
        image_view_screen_item = (ImageView) itemView.findViewById(R.id.plantImageView);
        list_row = (RelativeLayout) itemView.findViewById(R.id.list_row);
        list_row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    list_row.setBackgroundColor(Color.parseColor("#d5d5d5"));
                    Intent i = new Intent(context, ImagePager.class);
                    context.startActivity(i);
            }
        });
    }}}

3 个答案:

答案 0 :(得分:2)

首先为适配器定义RelativeLayouts列表,其中包含每一行:

List<RelativeLayout> items;

然后在你的onBindViewHolder方法中添加:

items.add(holder.list_row);

现在向您的适配器添加一个方法,如下所示:

private void makeAllWhite() {
    for(RelativeLayout item : items) {
        item.setBackgroundColor(Color.parseColor("#ffffff"));
    }
}

最后在这一行之前:

list_row.setBackgroundColor(Color.parseColor("#d5d5d5"));

呼叫:

makeAllWhite();

你的最终代码应该是这样的:

public class ScreenRecyclerAdapter  extends RecyclerView.Adapter<ScreenRecyclerAdapter.ViewHolder> {
    Context context;
    int image_list[];
    ImageView image_view_screen_item;
    List<RelativeLayout> items;

    public ScreenRecyclerAdapter(int[] image_list, Context context){
        super();
        this.image_list = image_list;
        this.context = context;
        this.items = new ArrayList<>();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.screen_items, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;}
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        items.add(holder.list_row);
        Picasso.with(context).load(image_list[position]).into(holder.image_view_screen_item);
    }

    @Override
    public int getItemCount() {
        return image_list.length;
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView image_view_screen_item , back;
        RelativeLayout list_row;
        public ViewHolder(View itemView) {
            super(itemView);
            image_view_screen_item = (ImageView) itemView.findViewById(R.id.plantImageView);
            list_row = (RelativeLayout) itemView.findViewById(R.id.list_row);
            list_row.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        makeAllWhite();
                        list_row.setBackgroundColor(Color.parseColor("#d5d5d5"));
                        Intent i = new Intent(context, ImagePager.class);
                        context.startActivity(i);
                }
            });
        }
    }
    private void makeAllWhite() {
        for(RelativeLayout item : items) {
            item.setBackgroundColor(Color.parseColor("#ffffff")));
        }
    }
}

答案 1 :(得分:0)

我在这里做了同样的事情。它有多种解决方案,但我发现最方便的是存储视图的副本(或视图,两者都工作),并在每个onClick事件检查视图(或视图)是否相同,如果不是,则更改背景之前选择的视图并为所选项目设置新背景,否则请使用aPlanes

答案 2 :(得分:0)

在适配器中,您为OnClickListener设置了ViewHolder,它基本上代表了每个项目。 (数据,设计,事件等)的绑定根据数据集中的位置在onBindViewHolder中发生。因此,只需将您的事件实现从ViewHolder的构造函数移动到onBindViewHolder

这可能无关紧要,因为您的数据集似乎未被事件修改,但如果确实如此,请不要忘记在修改后调用notifyDataSetChanged()