SwipeRefreshLayout和firebaserecycleradapter

时间:2018-11-06 12:57:43

标签: java android firebase firebase-realtime-database

private static final int TOTAL_ITEMS_TO_LOAD = 10;
private int mCurrentPage = 1;

private int itemPos = 0;
private String mLastKey = "";
private String mPrevKey = "";
    final DatabaseReference mFetchingMessages = FirebaseDatabase.getInstance().getReference().child("Messages").child(MessageSenderId).child(MessageRecieverId);

    FirebaseRecyclerOptions<MessagesHelper> options =
            new FirebaseRecyclerOptions.Builder<MessagesHelper>()
                    .setQuery(mFetchingMessages.limitToLast(mCurrentPage * TOTAL_ITEMS_TO_LOAD), MessagesHelper.class)
                    .build();
    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<MessagesHelper, Chat.MessagesViewHolder>(options) {

        @Override
        protected void onBindViewHolder(@NonNull final Chat.MessagesViewHolder holder, final int position, @NonNull final MessagesHelper model) {
            holder.setMessage(model.getMessage());


            final String userId = getRef(position).getKey();
            final DatabaseReference mTimeReference = FirebaseDatabase.getInstance().getReference().child("Messages").child(MessageSenderId).child(MessageRecieverId);
            mFetchingMessages.limitToLast(mCurrentPage * TOTAL_ITEMS_TO_LOAD).addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    final String from_user = model.getFrom();
                    final boolean Seen = model.isSeen();
                        MessagesHelper message = dataSnapshot.getValue(MessagesHelper.class);

                    itemPos++;

                    if(itemPos == 1){

                        String messageKey = dataSnapshot.getKey();

                        mLastKey = messageKey;
                        mPrevKey = messageKey;

                    }


                        if (from_user != null && from_user.equals(MessageSenderId)) {
                            holder.mDisplayMessage.setBackgroundColor(Color.BLUE);
                            holder.mDisplayMessage.setBackgroundColor(Color.WHITE);

                            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mDisplayMessage.getLayoutParams();
                            params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
                            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                            holder.mDisplayMessage.setLayoutParams(params);
                            mTimeReference.keepSynced(true);


                            if (Seen == true) {
                                holder.mDisplayMessage.setBackgroundColor(Color.CYAN);
                                mTimeReference.keepSynced(true);

                            } else {
                                holder.mDisplayMessage.setBackgroundColor(Color.BLUE);
                                mTimeReference.keepSynced(true);

                            }
                        } else {
                            holder.mDisplayMessage.setBackgroundColor(Color.GREEN);
                            holder.mDisplayMessage.setTextColor(data);
                            mTimeReference.keepSynced(true);

                            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mDisplayMessage.getLayoutParams();
                            params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                            holder.mDisplayMessage.setLayoutParams(params);
                        }


                        messagesList.add(message);

                        messageList.scrollToPosition(messagesList.size() - 1);

                    }



                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {
                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });
        }
        @Override
        public Chat.MessagesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.custom_activity_chat, parent, false);
            return new Chat.MessagesViewHolder(view);
        }
    };
    messageList.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();

onFreshListener

 mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mCurrentPage++;
            itemPos = 0;
            fetchMoreMessages();
        }
    });

方法

public void fetchMoreMessages(){
    DatabaseReference messageRef = mDatabaseReference.child("Messages").child(MessageSenderId).child(MessageRecieverId);

    Query messageQuery = messageRef.orderByKey().endAt(mLastKey).limitToLast(10);

    messageQuery.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            MessagesHelper message = dataSnapshot.getValue(MessagesHelper.class);
            String messageKey = dataSnapshot.getKey();
            if(!mPrevKey.equals(messageKey)){
                messagesList.add(itemPos++, message);
            } else {mPrevKey = mLastKey;
            }
            if(itemPos == 1) {
                mLastKey = messageKey;
            }
            Log.d("TOTALKEYS", "Last Key : " + mLastKey + " | Prev Key : " + mPrevKey + " | Message Key : " + messageKey);
            firebaseRecyclerAdapter.notifyDataSetChanged();
            mRefreshLayout.setRefreshing(false);
            linearLayoutManager.scrollToPositionWithOffset(10, 0);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

lastkey是正确的,因为它显示在Logcat中。但是,当我刷新时,将加载相同的消息。就像它应该从mLastKey及更高版本加载一样,但它只是从底部本身加载。有人可以帮我吗?我非常确定该逻辑是正确的,因为在我使用普通适配器之前,它曾经可以正常工作,然后我切换到firebaserecycler适配器,此后它一直不起作用...

0 个答案:

没有答案