感谢您的帮助。我试图在我的回收站视图中实现“添加到收藏夹”功能,除一件事情外,其他所有东西都工作正常。我无法在回收者视图中提交更改的图像按钮,每次按该按钮时,帖子都会添加到收藏夹中,并且图像按钮变为黄色,但是一旦我移到其他片段,黄色按钮就会消失回到初始阶段。你们中的任何人可以帮助我如何使我的按钮提交到回收者视图中的更改吗?下面是我的相关代码。
支架类中按钮的初始化:
class UsersViewHolder1 extends RecyclerView.ViewHolder {
View mView;
private ImageButton mFavouritesBlack, mFavouritesYellow;
private GestureDetector mGestureDetector;
private Heart mHeart;
public UsersViewHolder1(View itemView) {
super(itemView);
mView = itemView;
mFavouritesBlack = mView.findViewById(R.id.ad_start_fav);
mFavouritesYellow = mView.findViewById(R.id.ad_start_fav1);
}
}
OnBindViewHOlder类(我省略了不相关的代码):
protected void onBindViewHolder(@NonNull final UsersViewHolder1 Holder, final int position, @NonNull
Ad ad) {
Holder.setTitle(ad.getTitle());
Holder.setPrice(ad.getPrice());
Holder.setCategory(ad.getCategory());
Holder.setImage(ad.getImage(), getContext());
Holder.setTime(ad.getTime());
String user_id = getRef(position).getKey();
final String kk = user_id.toString();
Holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent mew = new Intent(getActivity(), ExpandActivity.class);
mew.putExtra("user_id", kk);
startActivity(mew);
}
});
Holder.mFavouritesBlack.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).child("fav_status").setValue("Added as fav").addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(),"Added to Fav",Toast.LENGTH_SHORT).show();
if (Holder.mFavouritesBlack.isShown())
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}
});
return true;
}
});
Holder.mFavouritesYellow.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(),"Removed from Fav",Toast.LENGTH_SHORT).show();
if (Holder.mFavouritesYellow.isShown())
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
});
return true;
}
});
}
答案 0 :(得分:4)
您要实现的是在需要保存状态的recyclerview中进行选择,也就是说,您必须将喜欢的项保存在SparseBooleanArray中,以用于保存喜欢的适配器位置,然后在onBindViewHolder中检查是否喜欢数组包含适配器位置和切换imageview(无需使用两个图像,而是可以更改drawable的颜色),请参阅this artcle
答案 1 :(得分:2)
这是因为单击时会保存标志,但是重新绘制视图时不会相应地重置图像。在onBindViewHolder内部,添加以下代码。
//Replace the if condition statement with the state value check(Abstract code shown below)
if(mFav.child(puid).child(kk).child("fav_status").getValue(),equals("Added as fav")){
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}else{
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
答案 2 :(得分:1)
经过两天的搜索和工作,我终于消除了我的问题,那就是回收者视图在更改后无法单独保存状态,这就是为什么我们需要提供一种存储状态的方法(或以我的情况为例),以便它在每次回收视图时都能记住它,但是我添加了直接语句,通过该语句,它可以检查数据库中是否存在收藏夹节点,并基于按钮的行为进行检查。下面是代码。
mFav.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(puid).hasChild(kk)){
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}else{
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Holder.mFavouritesBlack.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).child("fav_status").setValue("Added as fav").addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Added to Fav", Toast.LENGTH_SHORT).show();
if (!itemStateArray.get(adapterPosition, false))
Holder.mFavouritesBlack.setVisibility(View.GONE);
Holder.mFavouritesYellow.setVisibility(View.VISIBLE);
}
});
return true;
}
});
Holder.mFavouritesYellow.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
mFav.child(puid).child(kk).removeValue().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getContext(), "Removed from Fav", Toast.LENGTH_SHORT).show();
if (itemStateArray.get(adapterPosition, false))
Holder.mFavouritesBlack.setVisibility(View.VISIBLE);
Holder.mFavouritesYellow.setVisibility(View.GONE);
}
});
return true;
}
});
但是,非常感谢所有尝试提供帮助的人。