如何正确处理RecyclerView位置?

时间:2019-07-03 18:21:09

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

我正在使用recyclerview,并且在其适配器内有另一个recyclerview。

如何在更新项目时防止项目更改和项目刷新。 这会造成视觉故障和物品的错误定位

我试图直接从数据库参考中读取持有人当前位置的信息。

结果:https://media.giphy.com/media/j6TpXZRE6e44cMZVIP/giphy.gif

public class CommentsAdapter extends RecyclerView.Adapter<CommentsAdapter.ViewHolder>{
private Context mContext;

    private List<Comments> mComments;
    private List<Replies> allReplies;

    public CommentsAdapter(Context mContext, List<Comments> mComments, List<Replies> allReplies )
    {
        this.mContext = mContext;
        this.mComments = mComments;
        this.allReplies = allReplies;
    }

    @NonNull
    @Override
    public CommentsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.all_comments_layout, viewGroup, false);
        return new CommentsAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position)
    {
        final Comments comments = mComments.get(position);


        holder.like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    CommentUps.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            {
if (dataSnapshot.child(PostKey).child(com_uid).hasChild(currentUserID)) {
                                    CommentUps.child(PostKey).child(com_uid).child(currentUserID).removeValue();
                                  CommentsRef.child(PostKey).child(com_uid).child("likes").setValue(comments.getLikes() - 1);


                                } else {
                                    CommentUps.child(PostKey).child(com_uid).child(currentUserID).setValue(true);
                                    CommentsRef.child(PostKey).child(com_uid).child("likes").setValue(comments.getLikes() + 1);



                                }
}
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }

            }
        });



        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Replies");

        //reference.orderByChild("pLikes")

        reference.orderByChild(com_uid).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                holder.repliesList.clear();
                for (DataSnapshot snapshot : dataSnapshot.child(PostKey).child(com_uid).getChildren())
                {

                    Replies replies = snapshot.getValue(Replies.class);
                    //final String com = comment.toString();
                    // Log.d(new String("Comments"), com);
                    //for (String id : followingList)

                        if (replies.getComment_uid().equals(com_uid))
                        {

                        holder.repliesList.add(replies);
                        }
                }
                holder.repliesAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }


    @Override
    public int getItemCount() {
        return mComments.size();
    }


public class ViewHolder extends RecyclerView.ViewHolder {

        ImageButton like
        List<Replies> repliesList;
        RepliesAdapter repliesAdapter;


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

            like = itemView.findViewById(R.id.comment_like_button);
            repliesRecycler = itemView.findViewById(R.id.all_replies_recycler);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            repliesRecycler.setLayoutManager(linearLayoutManager);
            repliesRecycler.setHasFixedSize(true);
            repliesList = new ArrayList<>();

            repliesAdapter = new RepliesAdapter(mContext, repliesList);
            repliesRecycler.setAdapter(repliesAdapter);
            repliesAdapter.notifyDataSetChanged();


        }


        // Second Method - Same Result
        private void displayReplies(final String com_uid ,final String PostKey, final boolean locked) {

            DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Replies");

            reference.child(PostKey).child(com_uid).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    repliesList.clear();

                    if (dataSnapshot.exists()) {
                        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                final Replies replies = snapshot.getValue(Replies.class);

                                if (replies.getComment_uid().equals(com_uid)) {
                                    repliesList.add(replies);
                                    repliesAdapter.notifyDataSetChanged();
                                }
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
}

我的回复适配器:

public class RepliesAdapter extends RecyclerView.Adapter<RepliesAdapter.ViewHolder>
{
    public Context mContext;
    public List<Replies> mReplies;

    public RepliesAdapter(Context mContext, List<Replies> mReplies)
    {
        this.mContext = mContext;
        this.mReplies = mReplies;


    }

    @NonNull
    @Override
    public RepliesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.all_replies_layout, viewGroup, false);

        mAuth = FirebaseAuth.getInstance();
        return new RepliesAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position)
    {
final Replies replies = mReplies.get(position);

        holder.myUserName.setText(replies.getUsername());
        holder.myComment.setText(replies.getReply());
        holder.myTime.setText(replies.getTime());
    }


    @Override
    public int getItemCount() {

        return mReplies.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView myUserName, myComment,myTime;


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

            myUserName = itemView.findViewById(R.id.reply_username);
            myComment = itemView.findViewById(R.id.reply_text);
            myTime = itemView.findViewById(R.id.reply_time);

        }


    }

0 个答案:

没有答案