更新可绘制对象会更新所有在线设备上的所有可绘制对象

时间:2019-06-17 15:22:06

标签: android android-recyclerview android-drawable recycler-adapter

我有一个喜欢的按钮,当有人单击它时,它的可绘制更改。 问题在于它还会更新所有设备上的所有可绘制对象以及 增加了颤抖的效果。 有人可以帮我解决这个问题吗?

setUpStatus正在根据可绘制对象的喜好更新其状态

示例图片:https://im4.ezgif.com/tmp/ezgif-4-d3eff3e829a3.gif

适配器:

minimalmodbus

ViewHolder:

public MainAdapter(Context mContext, List<Posts> mPost)
    {
        Log.d(TAG, "Main Adapter Start");
        this.mContext = mContext;
        this.mPost = mPost;
 }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.all_posts_layout, viewGroup, false);
            return new MainAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

        mAuth = FirebaseAuth.getInstance();
        currentUserID = mAuth.getCurrentUser().getUid();
final Posts post = mPost.get(position);
        final String Postkey = post.getPost_uid();
        final String likes = Integer.toString(post.getLikes());

        holder.setUpStatus(Postkey);

        LikesRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                holder.LikepostButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {

                        LikeChecker = true;

                        LikesRef.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot)
                            {
                                if (LikeChecker.equals(true))
                                {

                                    if (dataSnapshot.child(Postkey).hasChild(currentUserID)) {
                                        LikesRef.child(Postkey).child(currentUserID).removeValue();

                                        PostsRef.child(Postkey).child("likes").setValue(post.getLikes()-1);
                                        LikeChecker = false;

                                    } else {

                                        LikesRef.child(Postkey).child(currentUserID).setValue(true);

                                        DisikesRef.child(Postkey).child(currentUserID).removeValue();
                                        StoreToHistory(Postkey);

                                        DisikesRef.addListenerForSingleValueEvent(new ValueEventListener() {
                                            @Override
                                            public void onDataChange(DataSnapshot dataSnapshot) {
                                                if (dataSnapshot.child(currentUserID).exists())
                                                {
                                                    PostsRef.child(Postkey).child("likes").setValue(post.getLikes()+2);
                                                }
                                                else {
                                                    PostsRef.child(Postkey).child("likes").setValue(post.getLikes()+1);
                                                }
                                            }

                                            @Override
                                            public void onCancelled(DatabaseError databaseError) {

                                            }
                                        });
                                        LikeChecker = false;
                                    }
                                }
                            }
                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                    }});
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });
    }
@Override
    public int getItemCount()
    {


        return mPost.size();
    }

0 个答案:

没有答案