RecyclerView Cardview 项目背景颜色在单击其项目时发生变化 [英] RecyclerView Cardview item background color change When Click on Its Item
问题描述
我试图在单击 RecylerView 项目时更改 RecyclerView CardView 背景的颜色(绿色),当我单击 RecyclerView 的下一个项目时,则必须更改上一个项目/变为其原始颜色(粉红色),并选中项目颜色会改变,即绿色.有人可以为此提供适当的解决方案.
I am trying to change the color (Green)of RecyclerView CardView background when clicks on RecylerView item , when I click on next item of RecyclerView, then previous item must be change / comes to its original color(Pink), and selected item color would be change, that is Green. Can someone give me the proper solution for this.
我的班级-:
public class RecylerAdapter extends RecyclerView.Adapter<RecylerAdapter.ViewHolder>
{ private boolean isSelected;
private final static int FADE_DURATION = 500;// milliseconds
private int lastPosition = -1;
Context cont;
private String[] strname;
private int[] icon;
public RecylerAdapter(Context con, String[] androidNames, int[] androidIcon)
{ cont=con;
strname=androidNames;
icon=androidIcon;
}
class ViewHolder extends RecyclerView.ViewHolder
{ private ImageView imgView;
private TextView txtView;
private CardView cardView;
private SparseBooleanArray selectedItems = new SparseBooleanArray();
public ViewHolder(final View itemView)
{
super(itemView);
imgView = (ImageView) itemView.findViewById(R.id.imageView);
txtView = (TextView) itemView.findViewById(R.id.txt);
cardView = (CardView) itemView.findViewById(R.id.cv12);
itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
cardView.isSelected = !cardView.isSelected;
notifyDataSetChanged();
}
});
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout,parent,false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(ViewHolder holder, int i)
{
if(ViewHolder.isSelected)
{
holder.cardView.setBackground(Color.Green);
}
else{
holder.cardView.setBackground(Color.Pink);
}
holder.txtView.setText(strname[i]);
holder.imgView.setImageResource(icon[i]);
setAnimation(holder.cardView, i);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setAnimation(View viewToAnimate, int position)
{
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition)
{
//animation 1
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(FADE_DURATION);
viewToAnimate.startAnimation(anim);
//animation 2
Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
else
{
Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
@Override
public int getItemCount()
{
return strname.length;
}
public void setSelected(boolean selection){
this.isSelected = selection;
}
public boolean isSelected(){
return isSelected;
}
}
推荐答案
这完全是关于您使用模型类进行项目选择管理:
It's all about your item selection manage using model class:
MyModel.class:这是您用于向回收者视图显示数据列表的类.添加一个布尔变量以进行选择和取消选择.
MyModel.class: This is the class which you are using for showing list of data to recycler view. Add a boolean variable to make selection and deselection.
private boolean isSelected;
public void setSelected(boolean selection){
this.isSelected = selection;
}
public boolean isSelected(){
return isSelected;
}
现在在您的适配器中点击回收器视图的项目:
Now on Item click of recycler view in your adapter:
myModel = list.get(position);
myModel.isSelected = !myModel.isSelected;
notifyDataSetChanged();
在适配器的 onBindViewHolder 方法中
In onBindViewHolder method of adapter
myModel = list.get(position);
if(myModel.isSelected){
itemView.setBackground(Color.Green);
}else{
itemView.setBackground(Color.Pink);
}
使用此逻辑并检查,如果您发现任何困难,请告诉我.
Use this logic and check, if you found any difficulty let me know.
您更新的代码,因为您没有使用模型类列表,因此您无法使用模型变量选择进行管理,请检查以下内容:
Your updated code as you are not using list of model class so you can't manage with Model variable selection, check with below:
public class RecylerAdapter extends RecyclerView.Adapter<RecylerAdapter.ViewHolder> {
private boolean isSelected;
private final static int FADE_DURATION = 500;// milliseconds
private int lastPosition = -1;
Context cont;
private String[] strname;
private int[] icon;
private int selectedPosition = -1;
public RecylerAdapter(Context con, String[] androidNames, int[] androidIcon) {
cont = con;
strname = androidNames;
icon = androidIcon;
}
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView imgView;
private TextView txtView;
private CardView cardView;
private SparseBooleanArray selectedItems = new SparseBooleanArray();
public ViewHolder(final View itemView) {
super(itemView);
imgView = (ImageView) itemView.findViewById(R.id.imageView);
txtView = (TextView) itemView.findViewById(R.id.txt);
cardView = (CardView) itemView.findViewById(R.id.cv12);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectedPosition = getAdapterPosition();
notifyDataSetChanged();
}
});
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(ViewHolder holder, int i) {
if (selectedPosition == i) {
holder.cardView.setBackground(Color.Green);
} else {
holder.cardView.setBackground(Color.Pink);
}
holder.txtView.setText(strname[i]);
holder.imgView.setImageResource(icon[i]);
setAnimation(holder.cardView, i);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setAnimation(View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition) {
//animation 1
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(FADE_DURATION);
viewToAnimate.startAnimation(anim);
//animation 2
Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
} else {
Animation animation = AnimationUtils.loadAnimation(cont, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
@Override
public int getItemCount() {
return strname.length;
}
}
这篇关于RecyclerView Cardview 项目背景颜色在单击其项目时发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!