更改项目的用户界面时,RecyclerView复制项目

时间:2019-08-15 14:11:46

标签: android android-recyclerview recycler-adapter

我试图在单击时更改ImageView(如按钮)的背景。但是,单击“赞”图标后背景会立即更改,然后立即刷新recyclerview并复制项目(使用默认图标)

我从类似问题的答案中尝试了以下方法:

1-向onBindViewHolder添加一些条件以识别喜欢的商品

2- postAdapter.setHasStableIds(true)

3-使用postAdapter.notifyItemChanged(position)

4-覆盖适配器类中的以下方法:

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}


@Override
public void setHasStableIds(boolean hasStableIds) {
    super.setHasStableIds(hasStableIds);
}

我分别尝试了上述每种方法,然后一次全部尝试,但是没有任何效果,问题仍然存在:/

代码很复杂,所以它们是相关的部分:

1-适配器类:

请注意,它具有一个接口和一个接口实例。该实例在内部ViewHolder类的likeImage.setOnClickListener中使用,该类也设置了clicked和clickedPositions;

public class PostRecyclerAdapter extends RecyclerView.Adapter<PostRecyclerAdapter.ViewHolder> {

public interface ClickListener{
    public void click(View view, int position);
}

private ArrayList<Post> postList;
private Context context;
private ClickListener clickListener;
private int clickedItemPosition = -1;
private boolean clicked = false;



public PostRecyclerAdapter(Context context, ArrayList<Post> postList){
    this.context = context;
    this.postList = postList;
}

public void onClickListener(ClickListener clickListener){
    this.clickListener = clickListener;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.post_item, parent, false));
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    if(clicked){
        if(clickedItemPosition == position){
            holder.likeImage.setBackgroundResource(R.drawable.like_filled);
            clicked = false;
            return;
        }
    }

    if(!clicked) {

        Post post = postList.get(position);

        holder.username.setText(post.getUsername());
        holder.date.setText(post.getDateAdded().toString());
        holder.description.setText(post.getDescription());
        holder.likeNum.setText(String.valueOf(post.getLikeNum()));
        holder.commentNum.setText(String.valueOf(post.getCommentNum()));

        Picasso.get()
                .load(Uri.parse(post.getImageUri()))
                .placeholder(R.drawable.download)
                .into(holder.postImage);

        //        holder.userImage.setText(post.getDateAdded().toString());
        //        holder.postImage.setText(post.getDateAdded().toString());

    }


}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
    return position;
}


@Override
public void setHasStableIds(boolean hasStableIds) {
    super.setHasStableIds(hasStableIds);
}

@Override
public int getItemCount() {
   if(postList != null)
       return postList.size();
   return 0;
}


// Inner View class
class ViewHolder extends RecyclerView.ViewHolder{

    ImageView userImage, postImage, commentImage, likeImage ;
    TextView username, date, description, commentNum, likeNum;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        userImage = itemView.findViewById(R.id.post_item_userImage);
        username = itemView.findViewById(R.id.post_item_username);
        date = itemView.findViewById(R.id.post_item_date);
        postImage = itemView.findViewById(R.id.post_item_postImage);
        likeNum= itemView.findViewById(R.id.post_item_likesNum);
        commentNum = itemView.findViewById(R.id.post_item_commentsNum);
        description = itemView.findViewById(R.id.post_item_description);
        commentImage = itemView.findViewById(R.id.post_item_comments);
        likeImage = itemView.findViewById(R.id.post_item_likes);

        commentImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context, "commentClicked", Toast.LENGTH_SHORT).show();
                if(clickListener != null) {
                    clickedItemPosition = getAdapterPosition();
                    clickListener.click(view, getAdapterPosition());

                }
            }
        });

        likeImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "LikeClicked", Toast.LENGTH_SHORT).show();
                if(clickListener != null) {
                    clicked = true;
                    clickedItemPosition = getAdapterPosition();
                    clickListener.click(view, getAdapterPosition());

                }

            }
        });
    }
}
}

2-实现了onClickListener接口方法的地方:

    @Override
public void click(View view, int position) {

    switch(view.getId()){

        case R.id.post_item_likes:
            likeUnlike(view, position);
            break;

        case R.id.post_item_comments:
            break;


    }

3-在likeUnlike方法中,我尝试更改图标:

 view.setBackgroundResource(R.drawable.like_filled);
 postAdapter.notifyItemChanged(position);
  • 这是完整的likeUnlike方法:

    postCommentReference.whereEqualTo("userID", user.getUid())
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
    
                    // within the comment sub-collection of the clicked post
    
                    if(e != null){
                        Toast.makeText(getContext(), "Exception "+e.toString() , Toast.LENGTH_SHORT).show();
                    }else{
    
    
                        if(queryDocumentSnapshots.isEmpty()){
                            // create a DOCUMENT for the comment:
                            DocumentReference likeDocReference = postCommentReference.document(user.getUid());
    
                            // create new like
                            Like like = new Like();
                            like.setPostID(post.getPostID());
                            like.setUserID(user.getUid());
                            like.setUsername(UserUtils.getInstance().username);
                            like.setLikeID(likeDocReference.getId());
    
                            // add like to db
                            likeDocReference.set(like);
    
                            //increment LikeNum
                            post.setLikeNum(post.getLikeNum()+1);
                            postReference.document(post.getPostID()).update("likeNum", post.getLikeNum())
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if(task.isSuccessful()){
                                                Log.d("home_fragment_likeNum", "onComplete: likeNum is updated");
                                            }else{
                                                Log.d("home_fragment_likeNum", "onComplete:  Exception: "+task.getException());
                                            }
                                        }
                                    });
    
                            // change icon
                            view.setBackgroundResource(R.drawable.like_filled);
                            postAdapter.notifyItemChanged(position);
                            return;
                        }else{
    
    
                        }
                    }
                }
            });}
    

0 个答案:

没有答案