更改RecyclerView所选项目的背景

时间:2016-12-09 06:20:57

标签: android android-recyclerview

我有一个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);

                    }
                });
        }

2 个答案:

答案 0 :(得分:3)

将全局int变量放入适配器类,如NSDictionary检查注释以获取更多信息

int selectedPosition = 9999;

答案 1 :(得分:0)

我认为你的selectedDate并不优雅,你可以改变它。

public class CustomDate {
    public String date;

    public boolean isSelected;
}

然后

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

    // TODO Custom some background drawable .

    if (monthDate.isSelected) {
        drawable.setColor(Color.parseColor("#006EB9"));
        holder.tvDate.setTextColor(getResources().getColor(R.color.white));
        holder.tvDate.setBackground(selectedDrawable);
    } else {
        drawable.setColor(Color.parseColor("#000000"));
        holder.tvDate.setTextColor(getResources().getColor(R.color.normal));
        holder.tvDate.setBackground(normalDrawable);
    }
    holder.tvDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            monthDate.isSelected = true;
           // notifyDataSetChanged();

           // You just need notify this item;
            notifyItemChanged(position);

        }
    });
}
相关问题