Sni*_*fer 0 android android-cardview android-recyclerview
我点击RecylerView项目时,我试图更改RecyclerView CardView背景的颜色(绿色),当我点击RecyclerView的下一个项目时,之前的项目必须更改/来到其原始颜色(粉红色),并选择项目颜色将变化,那就是绿色.有人能为我提供适当的解决方案.
我的课-:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是关于使用模型类进行项目选择管理的全部内容:
MyModel.class:这是用于显示回收器视图的数据列表的类.添加一个布尔变量以进行选择和取消选择.
private boolean isSelected;
public void setSelected(boolean selection){
this.isSelected = selection;
}
public boolean isSelected(){
return isSelected;
}
Run Code Online (Sandbox Code Playgroud)
现在点击你的适配器中的回收站视图:
myModel = list.get(position);
myModel.isSelected = !myModel.isSelected;
notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)
在适配器的onBindViewHolder方法中
myModel = list.get(position);
if(myModel.isSelected){
itemView.setBackground(Color.Green);
}else{
itemView.setBackground(Color.Pink);
}
Run Code Online (Sandbox Code Playgroud)
使用此逻辑并检查,如果您发现任何困难,请告诉我.
您更新的代码,因为您没有使用模型类列表,因此您无法使用模型变量选择进行管理,请查看以下内容:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4487 次 |
| 最近记录: |