简体   繁体   中英

Change Item Position in Firebase Recycler Adapter

I am using Firebase Recycler Adapter in my app to display items from my database, but I want a situation whereby newer items will be at the top of the fragment so i tried to use orderByChild() like this:

Query conversationQuery = mConvDatabase.orderByChild("timestamp");

but that didn't solve the problem.

What i really want to do is to update chat fragment anytime a user receives a new message to move the new conversation to the top just like every other messenger app.

this is my ChatFragment

@Override
public void onStart() {
    super.onStart();

    Query conversationQuery = mConvDatabase.orderByChild("timestamp");


    FirebaseRecyclerAdapter<Conv, ConvViewHolder> firebaseConvAdapter = new FirebaseRecyclerAdapter<Conv, ConvViewHolder>(
            Conv.class,
            R.layout.users_layout,
            ConvViewHolder.class,
            conversationQuery
    ) {
        @Override
        protected void populateViewHolder(final ConvViewHolder convViewHolder, final Conv conv, int i) {


            final String list_user_id = getRef(i).getKey();

           // Query MessageQuery = mMessageDatabase.child(list_user_id);


            Query lastMessageQuery = mMessageDatabase.child(list_user_id).limitToLast(1);

            lastMessageQuery.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                    String data = dataSnapshot.child("message").getValue().toString();
                    String type_image = dataSnapshot.child("type").getValue().toString();

                    boolean seen = Boolean.parseBoolean(dataSnapshot.getKey());


                    if (type_image.equals("image")){
                         convViewHolder.setMessage("image",conv.isSeen());
                    }else {

                        convViewHolder.setMessage(data, conv.isSeen());

                }}

                @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) {

                }
            });

            mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    final String userName = dataSnapshot.child("name").getValue().toString();
                    String userThumb = dataSnapshot.child("thumb_image").getValue().toString();

                    if(dataSnapshot.hasChild("online")) {

                        String userOnline = dataSnapshot.child("online").getValue().toString();
                        convViewHolder.setUserOnline(userOnline, getActivity());

                    }

                    mConvList.scrollToPosition(0);

                    convViewHolder.setName(userName);
                    convViewHolder.setUserImage(userThumb, getContext());

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


                            Intent chatIntent = new Intent(getContext(), ChatActivity.class);
                            chatIntent.putExtra("user_id", list_user_id);
                            chatIntent.putExtra("user_name", userName);
                            startActivity(chatIntent);

                        }
                    });

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    };


    mConvList.setAdapter(firebaseConvAdapter);
    firebaseConvAdapter.notifyDataSetChanged();
}

Is there a way i can manipulate the time stamp sent to database so it can be later retrieved in an appropriate manner? or can i just change the arrangement locally in my code as data is been loaded?

You can do something like this.

        //Display
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        linearLayoutManager.setStackFromEnd(true);
        linearLayoutManager.setReverseLayout(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(firebaseRecyclerAdapter);
        firebaseRecyclerAdapter.startListening();
        firebaseRecyclerAdapter.notifyDataSetChanged();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM