/ / Zmeniť pozadie vybranej položky RecyclerView - android, android-recyclerview

Zmeniť pozadie vybranej položky RecyclerView - Android, Android-recyclerview

Mám recylcerView, chcem zmeniťfarba pozadia vybraného textového zobrazenia, ak používateľ klikne na ďalší textový prehľad, predchádzajúci výber by mal byť odstránený a aktuálny by mal byť zvýraznený, zatiaľ som urobil iba to, že zvýrazní vybraný dátum a ponechá zvýraznený predchádzajúci

    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);

}
});
}

odpovede:

3 pre odpoveď č. 1

Vložte globálnu premennú int do triedy adaptéra, ako je int selectedPosition = 9999; Ďalšie informácie nájdete v komentároch

   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);

}
});
}

0 pre odpoveď č. 2

Myslím, že tvoje selectedDate nie je elegantný, môžete to zmeniť.

public class CustomDate {
public String date;

public boolean isSelected;
}

potom

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);

}
});
}