我是Android开发新手...我有1个回收器,视图2 Viewholder 1)用于视频2)用于照片..我正在为我的照片或视频实现类似功能......问题是我我不喜欢照片我喜欢其他它在下面的另一张照片上显示喜欢我检查firebase但它显示了相同的东西它添加像在数据库中的另一张照片
当我不向下滚动时它完美无缺...我认为它不是更新照片的位置我该怎么解决这个问题请帮助
public class TestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int VIDEO_TYPE=0;
public static final int IMAGE_TYPE=1;
private Context mContext;
private List<Photo> moviesList;
private DatabaseReference mReference;
private int mLayoutResource;
private LayoutInflater mInflater;
private Photo photo;
private VideoHolder mVideoHolder;
private PhotoHolder mPhotoHolder;
class PhotoHolder extends RecyclerView.ViewHolder {
private SquareImageView image;
private LikeButton mHeart,Star;
public PhotoHolder(View view){
super(view);
mHeart = (LikeButton) view.findViewById(R.id.main_heart);
}
}
class VideoHolder extends RecyclerView.ViewHolder{
public VideoHolder(View view) {
super(view);
}
public TestAdapter(List<Photo> moviesList) {
this.moviesList = moviesList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//findViewById...
mContext = parent.getContext();
switch (viewType) {
case VIDEO_TYPE:
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_type, parent, false);
mContext = parent.getContext();
return new VideoHolder(v);
case IMAGE_TYPE:
View i = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_list, parent, false);
mContext = parent.getContext();
return new PhotoHolder(i);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
// mHolder = holder;
photo = moviesList.get(holder.getAdapterPosition());
// final VideoHolder viewHolder2 = (VideoHolder)holder;
int viewType = getItemViewType(holder.getAdapterPosition());
switch ( viewType ) {
case IMAGE_TYPE:
PhotoHolder photoview = (PhotoHolder) holder;
mPhotoHolder = photoview;
getCurrentUsername();
final ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(getItem(holder.getAdapterPosition()).getImage_path(),photoview.image);
photoview.mHeart.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
addNewPhotolike(mPhotoHolder);
}
@Override
public void unLiked(LikeButton likeButton) {
removePhotolike(mPhotoHolder);
}
});
photoview.Star.setOnLikeListener(new OnLikeListener() {
@Override
public void liked(LikeButton likeButton) {
}
@Override
public void unLiked(LikeButton likeButton) {
}
});
break;
case VIDEO_TYPE:
final VideoHolder viewHolder2 = (VideoHolder)holder;
mVideoHolder = viewHolder2;
break;
}
}
@Override
public int getItemCount() {
return moviesList.size();
}
public Photo getItem(int position) {
return moviesList.get(position);
}
@Override
public int getItemViewType ( int position ) {
int viewType;
if (moviesList.get(position).getType_post().contains("Photo")) {
viewType = IMAGE_TYPE;
} else{
viewType = VIDEO_TYPE;
}
return viewType;
}
这是添加喜欢照片的方法,如果有问题
private void addNewPhotolike(final TestAdapter.PhotoHolder holder){
Log.d(TAG, "addNewlike: adding new like ");
String newLikeID = mReference.push().getKey();
Likes likes = new Likes();
likes.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.mHeart.setLiked(true);
});
}
private void removePhotolike(final TestAdapter.PhotoHolder holder){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.keepSynced(true);
Query query = reference
.child(mContext.getString(R.string.dbname_photos))
.child(photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singlesnapshot : dataSnapshot.getChildren()) {
String keyID = singlesnapshot.getKey();
if (holder.likeByCurrentUSer&& singlesnapshot.getValue(Likes.class).getUser_id().equals(FirebaseAuth.getInstance().getCurrentUser().getUid())) {
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(photo.getUser_id())
.child(photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
mReference.child(mContext.getString(R.string.dbname_photos))
.child(photo.getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(keyID)
.removeValue();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:0)
Recyclerview重复使用其行。这是正常的。您没有选择是否选择了喜欢按钮。您正在选择一个,而View holder会保持选中状态。您可以在ObjectIncome对象中添加一个布尔变量,并保持项目的选择状态。
答案 1 :(得分:0)
在onbindviewhodler中使用position而不是holder.getAdapterPosition(),它位于onbindviewholder方法的参数中。
答案 2 :(得分:0)
它没有更新位置 **添加另一个变量(int position)用LikeItem(position)替换照片在like listener中添加addNewPhotolike(ViewHolder,position)**
private void addNewPhotolike(final TestAdapter.PhotoHolder holder, int position){
Log.d(TAG, "addNewlike: adding new like ");
String newLikeID = mReference.push().getKey();
Likes likes = new Likes();
likes.setUser_id(FirebaseAuth.getInstance().getCurrentUser().getUid());
mReference.child(mContext.getString(R.string.dbname_photos))
.child(getItem(position).getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
mReference.child(mContext.getString(R.string.dbname_user_photos))
.child(getItem(position).getPhoto_id())
.child(mContext.getString(R.string.field_likes))
.child(newLikeID)
.setValue(likes);
holder.mHeart.setLiked(true);
});
}