回收者查看位置

时间:2018-06-07 11:01:33

标签: android firebase firebase-realtime-database android-recyclerview adapter

我是Android开发的新手。我有一个带有Viewholder的recyclerview用于显示照片。我在我的应用程序中实现了喜欢功能,但我遇到的问题只是当我添加时喜欢在照片上喜欢没有显示在我喜欢的照片上,而是在另一张照片上显示喜欢,当我在firebase数据库中看到时它看起来很好,但在回收者视图中没有显示在正确的位置。

我认为这不是更新位置我该怎么解决这个问题?

这是我的适配器类

@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
// mHolder = holder;
photo = moviesList.get(position);
//  final VideoHolder viewHolder2 = (VideoHolder)holder;
int viewType = getItemViewType(holder.getAdapterPosition());
switch ( viewType ) {
    case IMAGE_TYPE:
        PhotoHolder photoview = (PhotoHolder) holder;
        mPhotoHolder = photoview;
        getCurrentUsername();
        getLikesPhotoString();
        final ImageLoader imageLoader =  ImageLoader.getInstance();
        imageLoader.displayImage(getItem(position).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;
}

这是照片添加到firebase

private void addNewPhotolike(TestAdapter.PhotoHolder holder, final 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).getUser_id())
            .child(getItem(position).getPhoto_id())
            .child(mContext.getString(R.string.field_likes))
            .child(newLikeID)
            .setValue(likes);
    holder.mHeartPhoto.setLiked(true);
    HashMap<String ,String> notificationData = new HashMap<>();
    notificationData.put("from",FirebaseAuth.getInstance().getCurrentUser().getUid());
    notificationData.put("type","likes");
    notificationData.put("photo_desc",getItem(position).getDescription());
    holder.mNotification.child(getItem(position).getUser_id()).push().setValue(notificationData).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
          //  getLikesString(mHolder);
            getLikesPhotoString(mPhotoHolder,position);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
          //  getLikesString(mHolder);
            getLikesPhotoString(mPhotoHolder,position);

        }
    });

}

这是我的代码,其中从strong fire中检索 like 并以文本显示。

private void getLikesPhotoString(final TestAdapter.PhotoHolder holder, final int postion){
Log.d(TAG, "getLikesString: getting likes string");
try{
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    reference.keepSynced(true);
    Query query = reference
            .child(mContext.getString(R.string.dbname_photos))
            .child(getItem(postion).getPhoto_id())
            .child(mContext.getString(R.string.field_likes));
    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            holder.usersPhoto = new StringBuilder();
            for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
                Query query = reference
                        .child(mContext.getString(R.string.dbname_users))
                        .orderByChild(mContext.getString(R.string.field_user_id))
                        .equalTo(singleSnapshot.getValue(Likes.class).getUser_id());
                query.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
                            Log.d(TAG, "onDataChange: found like: " +
                                    singleSnapshot.getValue(User.class).getUsername());
                            holder.usersPhoto.append(singleSnapshot.getValue(User.class).getUsername());
                            holder.usersPhoto.append(",");
                        }
                        String[] splitUsers =  holder.usersPhoto.toString().split(",");

                        if( holder.usersPhoto.toString().contains(currentUsername + ",")){
                            holder.likephotobycurrentUser = true;
                        }else{
                            holder.likephotobycurrentUser = false;
                        }

                        Log.d(TAG, "onDataChange: likes string: " +  holder.mLIkePhotoString);
                        //  setupwidjets();

                        int length = splitUsers.length;
                        if(length == 1){
                            holder.mLIkePhotoString = "Liked by " + splitUsers[0];
                        }
                        else if(length == 2){
                            holder.mLIkePhotoString = "Liked by " + splitUsers[0]
                                    + " and " + splitUsers[1];
                        }
                        else if(length == 3){
                            holder.mLIkePhotoString = "Liked by " + splitUsers[0]
                                    + ", " + splitUsers[1]
                                    + " and " + splitUsers[2];

                        }
                        else if(length == 4){
                            holder.mLIkePhotoString = "Liked by " + splitUsers[0]
                                    + ", " + splitUsers[1]
                                    + ", " + splitUsers[2]
                                    + " and " + splitUsers[3];
                        }
                        else if(length > 4){
                            holder.mLIkePhotoString = "Liked by " + splitUsers[0]
                                    + ", " + splitUsers[1]
                                    + ", " + splitUsers[2]
                                    + " and " + (splitUsers.length - 3) + " others";
                        }
                        Log.d(TAG, "onDataChange: likes string: " + holder.mLIkePhotoString);
                        //setup likes string
                     //   setupLikePhotostring(holder, holder.mLIkePhotoString);
                        holder.mHeartPhoto.setLiked(true);
                        if (holder.likephotobycurrentUser){
                            holder.mHeartPhoto.setLiked(true);
                        }else {
                            holder.mHeartPhoto.setLiked(false);
                        }
                        holder.likes.setText(holder.mLIkePhotoString);


                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
            if(!dataSnapshot.exists()){
                holder.mLIkePhotoString = "";
                holder.likephotobycurrentUser = false;
                //setupwidjets();
            //    setupLikePhotostring(holder,holder.mLIkePhotoString);
                if (holder.likephotobycurrentUser){
                    holder.mHeartPhoto.setLiked(true);
                }else {
                    holder.mHeartPhoto.setLiked(false);
                }
                holder.likes.setText(holder.mLIkePhotoString);

                holder.mHeartPhoto.setLiked(false);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}catch (NullPointerException e){
    Log.e(TAG, "removeStar: NullPointer"+e.getMessage());
    holder.mLIkePhotoString = "";
    holder.likephotobycurrentUser = false;
   // setupLikePhotostring(mPhotoHolder,holder.mLIkePhotoString);
    if (holder.likephotobycurrentUser){
        holder.mHeartPhoto.setLiked(true);
    }else {
        holder.mHeartPhoto.setLiked(false);
    }
     holder.likes.setText(holder.mLIkePhotoString);
}

}

1 个答案:

答案 0 :(得分:1)

更改此行

photo = moviesList.get(holder.getAdapterPosition());

photo = moviesList.get(position);

其中position变量与我们在下面的函数中得到的相同

public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position);

并在holder.getAdapterPosition()函数中将position更改为onBindViewHolder(...)