如何将 alpha 设置为 recyclerview 项目,除了点击一个
How to set alpha to recyclerview item except clicked one
我有一个带有 imageview 的 recyclerview 并且 textview.I 用它填充图像并且 text.I 已经使用 customadapter recyclerview.Everything 工作正常但现在我想要
1) 当我点击一张图片时,所有其他图片的 alpha 将设置为 0.5f
这是我的代码,它本身只是 alpha
public class CatAdapter extends RecyclerView.Adapter<CatAdapter.HeroViewHolder> {
Context mCtx;
List<CatModel> adsList;
int selectedPosition=-1;
public CatAdapter(Context mCtx, List<CatModel> heroList) {
this.mCtx = mCtx;
this.adsList = heroList;
}
@NonNull
@Override
public HeroViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.cat_recycler_layout, parent, false);
return new HeroViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final HeroViewHolder holder, int position) {
final CatModel hero = adsList.get(position);
Glide.with(mCtx)
.load(hero.getImageurl())
.into(holder.imageView);
holder.textView.setText(hero.getName());
holder.imageView.setOnClickListener(new View.OnClickListener() {
int button01pos = 0;
@Override
public void onClick(View view) {
if (button01pos == 0) {
holder.cross.setVisibility(View.VISIBLE);
holder.imageView.setAlpha(1f);
button01pos = 1;
} else if (button01pos == 1) {
holder.cross.setVisibility(View.GONE);
holder.imageView.setAlpha(1f);
button01pos = 0;
}
}
});
}
@Override
public int getItemCount() {
return adsList.size();
}
class HeroViewHolder extends RecyclerView.ViewHolder {
ImageView cross;
CircleImageView imageView;
TextView textView;
public HeroViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
cross = itemView.findViewById(R.id.cross);
textView = itemView.findViewById(R.id.textView);
}
}
}
试试这个方法。
public class CatAdapter extends RecyclerView.Adapter<CatAdapter.HeroViewHolder> {
Context mCtx;
List<CatModel> adsList;
int selectedPosition=-1;
CatModel selectedItem = null;
@Override
public void onBindViewHolder(@NonNull final HeroViewHolder holder, int position) {
...
holder.imageView.setAlpha(selectedItem == null || hero.equals(selectedItem) ? 1f : 0.5f);
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedItem = hero.equals(selectedItem) ? null : hero;
...
notifyDataSetChanged();
}
});
}
...
试试这个方法
全球
selectedItem =-1
override fun onBindViewHolder(@NonNull final HeroViewHolder holder, int position) {
if(position == selectedItem)holder.itemView.setAlpha(0.5f)
holder.itemview.setOnClickListener{
selectedItem = position
notifyDataSetChanged()
}
}
我有一个带有 imageview 的 recyclerview 并且 textview.I 用它填充图像并且 text.I 已经使用 customadapter recyclerview.Everything 工作正常但现在我想要
1) 当我点击一张图片时,所有其他图片的 alpha 将设置为 0.5f
这是我的代码,它本身只是 alpha
public class CatAdapter extends RecyclerView.Adapter<CatAdapter.HeroViewHolder> {
Context mCtx;
List<CatModel> adsList;
int selectedPosition=-1;
public CatAdapter(Context mCtx, List<CatModel> heroList) {
this.mCtx = mCtx;
this.adsList = heroList;
}
@NonNull
@Override
public HeroViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.cat_recycler_layout, parent, false);
return new HeroViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final HeroViewHolder holder, int position) {
final CatModel hero = adsList.get(position);
Glide.with(mCtx)
.load(hero.getImageurl())
.into(holder.imageView);
holder.textView.setText(hero.getName());
holder.imageView.setOnClickListener(new View.OnClickListener() {
int button01pos = 0;
@Override
public void onClick(View view) {
if (button01pos == 0) {
holder.cross.setVisibility(View.VISIBLE);
holder.imageView.setAlpha(1f);
button01pos = 1;
} else if (button01pos == 1) {
holder.cross.setVisibility(View.GONE);
holder.imageView.setAlpha(1f);
button01pos = 0;
}
}
});
}
@Override
public int getItemCount() {
return adsList.size();
}
class HeroViewHolder extends RecyclerView.ViewHolder {
ImageView cross;
CircleImageView imageView;
TextView textView;
public HeroViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
cross = itemView.findViewById(R.id.cross);
textView = itemView.findViewById(R.id.textView);
}
}
}
试试这个方法。
public class CatAdapter extends RecyclerView.Adapter<CatAdapter.HeroViewHolder> {
Context mCtx;
List<CatModel> adsList;
int selectedPosition=-1;
CatModel selectedItem = null;
@Override
public void onBindViewHolder(@NonNull final HeroViewHolder holder, int position) {
...
holder.imageView.setAlpha(selectedItem == null || hero.equals(selectedItem) ? 1f : 0.5f);
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedItem = hero.equals(selectedItem) ? null : hero;
...
notifyDataSetChanged();
}
});
}
...
试试这个方法
全球
selectedItem =-1
override fun onBindViewHolder(@NonNull final HeroViewHolder holder, int position) {
if(position == selectedItem)holder.itemView.setAlpha(0.5f)
holder.itemview.setOnClickListener{
selectedItem = position
notifyDataSetChanged()
}
}