我有一个recylcerView,我想更改所选textview的背景颜色,如果用户单击下一个textview,则应删除先前的选择,并应突出显示当前内容,到目前为止,我所做的是突出显示了所选日期并保留了上一个日期突出显示

    public void onBindViewHolder(final DateHolder holder, final int position) {
        final String monthDate = arrayListDate.get(position);
        final GradientDrawable gd = new GradientDrawable();
        gd.setShape(GradientDrawable.OVAL);
        final GradientDrawable drawable=new GradientDrawable();
        drawable.setShape(GradientDrawable.OVAL);
        if(selectedDate.contains(monthDate)){

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                drawable.setColor(Color.parseColor("#006EB9"));
                drawable.setSize(width,height);
                holder.tvDate.setTextColor(getResources().getColor(R.color.white));
                holder.tvDate.setBackground(drawable);
             }
        }
            ViewTreeObserver viewTreeObserver = relativeLayout.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {                            holder.tvDate.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        width = holder.tvDate.getMeasuredWidth();
                        height = holder.tvDate.getLineHeight();
                    } else {
                        //noinspection deprecation
                        holder.tvDate.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        width = holder.tvDate.getMeasuredWidth();
                        height= holder.tvDate.getLineHeight();
                    }

                }
            });

                holder.tvDate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        selectedDate.clear();
                        selectedDate.add(monthDate);
                        notifyItemChanged(position);

                    }
                });
        }

最佳答案

将全局int变量放入适配器类(如int selectedPosition = 9999;)中,查看注释以获取更多信息

   public void onBindViewHolder(final DateHolder holder, final int position
  {
    final String monthDate = arrayListDate.get(position);
    final GradientDrawable gd = new GradientDrawable();
    gd.setShape(GradientDrawable.OVAL);
    final GradientDrawable drawable=new GradientDrawable();
    drawable.setShape(GradientDrawable.OVAL);
    // if its selected position change background;
    if(selectedPosition == position){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            drawable.setColor(Color.parseColor("#006EB9"));
            drawable.setSize(width,height);
            holder.tvDate.setTextColor(getResources().getColor(R.color.white));
            holder.tvDate.setBackground(drawable);
         }
    }
        ViewTreeObserver viewTreeObserver = relativeLayout.getViewTreeObserver();
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {                            holder.tvDate.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    width = holder.tvDate.getMeasuredWidth();
                    height = holder.tvDate.getLineHeight();
                } else {
                    //noinspection deprecation
                    holder.tvDate.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    width = holder.tvDate.getMeasuredWidth();
                    height= holder.tvDate.getLineHeight();
                }

            }
        });

            holder.tvDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     //notify position if there was previous position
                    if(selectedPosition != 9999){
                      int temp= selectedPosition;
                      selectedPosition = position;
                      notifyItemChanged(temp);
                    }
                    else{
                       selectedPosition= position;
                    }
                    // put new selected position

                   //notify new position
                    notifyItemChanged(position);

                }
            });
    }

关于android - 更改RecyclerView所选项目的背景,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41054268/

10-11 15:34