我想使用Recyclerviews中的按钮之一来更改Recyclerview中的一个对象,即将Imageview设置为透明。
因此,我发现在这种情况下需要使用notifyItemChanged(int,object,payload)来查看此线程:(Example NotifyItemChanged)。遗憾的是,此示例没有涉及如何使用“对象值”更改图像的值(ImageView.setImageAlpha(70))。
请求:
能给我一个例子,说明如何使用notifyItemChanged在Recycleradapter中更改ImageView的setImageAlpha值 one 。
我的框架
我将发布我的代码,以便您可以进行相应的编辑。但是我也可以尝试自己做。
public class deckbuilder_RViewAdapter_Card extends RecyclerView.Adapter<deckbuilder_RViewAdapter_Card.ViewHolder > {
private List<Cards> mData;
private List<Heros> mHero, mHeroAdd;
private List<Spells> mSpell, mSpellSig;
private List<Items> mItem;
private List<Improvements> mImpr, mImprSig;
private List<Creeps> mCreep, mCreepSig;
private List<Decklist> mDecklist;
private List<Decklist_Heros> mDecklistHeros;
DBHelper dbHelper;
private CardView introCardview;
private Context mContext;
private RecyclerView mrvList,mrvHeros;
private deckbuilder_RViewAdapter_List mRViewAdapterList;
private deckbuilder_RViewAdapter_Heros mRViewAdapterHeros;
private int questionmarkNo;
public deckbuilder_RViewAdapter_Card (FragmentActivity mContext, List<Cards> mData, deckbuilder_RViewAdapter_List mRViewAdapterList, List<Decklist> mDecklist, RecyclerView rvList, deckbuilder_RViewAdapter_Heros mRViewAdapterHeros, List<Decklist_Heros> mDecklistHeros, RecyclerView rvHeros) {
this.mData = mData;
this.mContext = mContext;
this.mRViewAdapterList = mRViewAdapterList;
this.mDecklist = mDecklist;
this.mrvList = rvList;
this.mRViewAdapterHeros = mRViewAdapterHeros;
this.mDecklistHeros = mDecklistHeros;
this.mrvHeros= rvHeros;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
dbHelper = new DBHelper(mContext.getApplicationContext());
dbHelper.createDataBase();// used for clickable
//Todo change inflated layout later on <-????
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_cardview_deckbuilder_cards, viewGroup, false);
return new deckbuilder_RViewAdapter_Card.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int position) {
viewHolder.ivCardImage.setImageResource(mData.get(position).getCardImage());
viewHolder.tvCardName.setText(mData.get(position).getCardName());
viewHolder.ivCardImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cardClickable(position);
}
});
viewHolder.tvCardName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cardClickable(position);
}
});
/* Init the Add and Remove button */
viewHolder.civ_deckbuilder_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mData.get(position).getCardType().equals("hero")){
addHero(viewHolder, position);
}else {
addOtherCards(viewHolder,position);
}
}
});
viewHolder.civ_deckbuilder_remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mData.get(position).getCardType().equals("hero")){
removeHero(position);
**viewHolder.ivCardImage.setImageAlpha(70);**
}else {
removeOtherCard(position);
**viewHolder.ivCardImage.setImageAlpha(70);**
}
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
CircleImageView civ_deckbuilder_add, civ_deckbuilder_remove, civ_deckbuilder_number;
ImageView ivCardImage;
TextView tvCardName;
public ViewHolder(@NonNull View introListView) {
super(introListView);
ivCardImage = introListView.findViewById(R.id.ivDeckbuilder_img);
tvCardName = introListView.findViewById(R.id.tvDeckbuilder_title);
civ_deckbuilder_add = introListView.findViewById(R.id.civ_deckbuilder_add);
civ_deckbuilder_remove = introListView.findViewById(R.id.civ_deckbuilder_remove);
}
}
//[...] used methods should not be relevant.
}
感谢您可能为我提供的帮助!
答案 0 :(得分:1)
首先,您的 Card 数据类应具有 alpha 属性,该属性的初始值应为255,以显示完全可见的图像。
在onBindViewHolder中使用
viewHolder.ivCardImage.setImageAlpha(mData.get(position).getAlpha());
然后在您的点击事件中
@Override
public void onClick(View view) {
....
mData.get(position).setAlpha(70);
notifyItemChanged(position);
}
}
答案 1 :(得分:1)
您的按钮是否位于RecyclerView之外?
假设确实如此,请在适配器中公开一个方法,该方法采用位置值和要更改为的值。您还需要一种方法来修改ViewHolder中所需的任何项目。
类似的事情应该起作用...
在适配器中:
UPDATE User u
INNER JOIN Other o
ON u.id = o.id
SET name = 'XXXXXXX';
您认为持有人:
public void updateItem(int position, Object object) {
mData.set(object, position);
notifyItemChanged(position);
}
在您的clickListener中执行操作:
public void modifyAlpha(int alpha) {
imageView.setAlpha(alpha);
}
希望这会有所帮助!