循环查看多个项目上发生的项目按钮动画



所以我有一个非常简单的回收器视图。还有一个应用按钮。按下按钮后,文本变为橙色。但当我向下滚动时,我看到还有其他项目的应用按钮颜色也发生了变化。

有人知道发生了什么事吗?以下是一些代码片段。

public void apply(View view){
Button b = (Button)view;
b.setTextColor(ColorEx.TUTOR_BEAR_LOGO_ORANGE);
}

这是在主活动中执行的方法。这是不同布局中的按钮xml。(项目布局(

<Button
android:id="@+id/btn_apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="APPLY"
android:onClick="apply"
/>

这是我的适配器类

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
Context context;
ArrayList<String> title;
public Adapter(Context context, ArrayList<String> title) {
this.context = context;
this.title = title;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_sample,parent,false);
MyViewHolder  holder = new MyViewHolder(view);
return  holder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
switch (position){
case 0:
case 3:
case 6:
holder.imageView.setImageResource(R.drawable.x);
break;
case 1:
case 4:
case 7:
holder.imageView.setImageResource( R.drawable.y);
break;
case 2:
case 5:
holder.imageView.setImageResource(R.drawable.z);
break;
case 8:
holder.imageView.setImageResource(R.drawable.z);
break;
}
}
@Override
public int getItemCount() {
return title.size();
}

class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
}
}

}

我用屏幕记录了这一事件,并将其上传为.gif,以便更好地参考。这是下面的链接。https://media.giphy.com/media/jOagjkf9dmW5Kvrqyn/giphy.gif

您必须保存点击的项目位置

public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
Context context;
ArrayList<String> title;
Set<Integer> itemClicked;
public Adapter(Context context, ArrayList<String> title) {
this.context = context;
this.title = title;
this.itemClicked = new HashSet<>();
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_sample, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
switch (position) {
case 0:
case 3:
case 6:
holder.imageView.setImageResource(R.drawable.x);
break;
case 1:
case 4:
case 7:
holder.imageView.setImageResource(R.drawable.y);
break;
case 2:
case 5:
holder.imageView.setImageResource(R.drawable.z);
break;
case 8:
holder.imageView.setImageResource(R.drawable.z);
break;
}
if (itemClicked.contains(position)) {
holder.button.setTextColor(ColorEx.TUTOR_BEAR_LOGO_ORANGE);
} else {
holder.button.setTextColor(ColorEx.BLACK);
}
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (itemClicked.contains(position)) {
itemClicked.remove(position);
} else {
itemClicked.add(position);
}
notifyItemChanged(position);
}
});
}
@Override
public int getItemCount() {
return title.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
Button button;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
button = itemView.findViewById(R.id.btn_apply);
}
}
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
final Context context;
final ArrayList<String> title;
final List<Boolean> buttonPositionList;//this tells weather the button is selected or not

public Adapter(Context context, ArrayList<String> title,List<Boolean> buttonPositionList) {
this.context = context;
this.buttonPositionList = buttonPositionList;
this.title = title;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item_sample,parent,false);
MyViewHolder  holder = new MyViewHolder(view);
holder.btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonPositionList.set(holder.getAdapterPosition(),!buttonPositionList.get(holder.getAdapterPosition()));
notifyDataSetChanged();// or notify Item set changed
}
});
return  holder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
switch (position){
case 0:
case 3:
case 6:
holder.imageView.setImageResource(R.drawable.x);
break;
case 1:
case 4:
case 7:
holder.imageView.setImageResource( R.drawable.y);
break;
case 2:
case 5:
holder.imageView.setImageResource(R.drawable.z);
break;
case 8:
holder.imageView.setImageResource(R.drawable.z);
break;
}
// Button Position is selected
if(buttonPositionList.get(position)){
holder.btnApply.setTextColor(ColorEx.TUTOR_BEAR_LOGO_ORANGE);
}else{
//The color you want when not selected
}
}
@Override
public int getItemCount() {
return title.size();
}
protected class MyViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
Button btnApply;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
btnApply = itemView.findViewById(R.id.btn_apply);
}
}

}

相关内容

  • 没有找到相关文章

最新更新