skip to Main Content

I am setting up recyclerview from Firebase with the below code. This fetches the whole data every time when new child added to firebase realtime database and recyclerview scrolled to the bottom automatically. I need to stop autoscroll recyclerview when user is in the middle of the screen.

public void setUpRecyclerview() {
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                modelList.clear();
                for (DataSnapshot ds:dataSnapshot.getChildren()) {
                    String data = (String) ds.child("data").getValue();
                    long time = (long) ds.child("time").getValue();
                    String type = (String) ds.child("type").getValue();
                    DealsModel model = new DealsModel(data, time, type);
                    modelList.add(model);
                    count++;
                }
                dealAdapter = new DealAdapter(modelList);
                LinearLayoutManager layoutManager = new LinearLayoutManager(DealsActivity.this);
                layoutManager.setStackFromEnd(true);
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(dealAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

I need to perform this functionality only when a new child inserted, so that the user screen doesn’t auto scroll to bottom and a counter show on Float Button that a new message appears. Like Whatsapp/Telegram.


RecyclerView.LayoutManager linearLayoutManager = recyclerView.getLayoutManager();
                int lastVisiblePosition = ( (LinearLayoutManager) linearLayoutManager ).findLastCompletelyVisibleItemPosition();
                int position1 = ( (LinearLayoutManager) linearLayoutManager ).findLastVisibleItemPosition();

if(position1 != lastVisiblePosition ) {
                    if (positionStart >= (totalItem - 1)) {
                        countMsg = countMsg + 1;
                        Log.e("Countmsg","."+countMsg);
                    }
                }
                else {
                    recyclerView.scrollToPosition(totalItem - 1);
                }

I need value of positionStart i.e. the value of newly instered child. How to get this.

2

Answers


  1. If you want more granular control over the changes to the child nodes, use a ChildEventListener:

    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
            String data = dataSnapshot.child("data").getValue(String.class);
            long time = dataSnapshot.child("time").getValue(Long.class);
            String type = dataSnapshot.child("type").getValue(String.class);
            DealsModel model = new DealsModel(data, time, type);
            modelList.add(model);
            count++;
        }
    
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
            Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
        }
    
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
            Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.w(TAG, "onCancelled", databaseError.toException());
        }
    };
    

    The onChildAdded method here gets called initially for each child node in databaseReference, and subsequently once for each child node that is added. As you can see, you no need to loop over ds.getChildren() as we now get called for each child node individually.

    Since there are more type of changes than just additions, you’ll also want to implement onChildChanged, onChildRemoved and onChildMoved to make the corresponding update to your data structure, and then call adapter.notifyDataSetChanged() to tell it about the change.

    Login or Signup to reply.
  2. Remove this piece of code or make it false like this.

    layoutManager.setStackFromEnd(false);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search