Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain scroll position of FirebaseRecyclerAdapter

I am trying to maintain the scroll position of my FirebaseRecyclerAdapter when I am returning to the same activity from another activity.

Here is my function which sets the FirebaseRecyclerAdapter

public void setTheScreen(){
   FirebaseRecyclerAdapter<Post,PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post,PostViewHolder>(
           Post.class,
           R.layout.post_display_blueprint,
           PostViewHolder.class,
           postsNode
   ) {
       @Override
       public void onDataChanged() {
           if(dialog != null && dialog.isShowing()){
               dialog.dismiss();
           }
       }

       @Override
       protected void populateViewHolder(final PostViewHolder viewHolder, Post model, int position) {
           final String key = getRef(position).getKey();
           mRefPosts = FirebaseDatabase.getInstance().getReference().child("posts").child(key);
           mLoveNode = mRef.child("loves");
           mRefLoves = mRef.child("loves").child(key);
           viewHolder.setTitle(model.getTitle());
           viewHolder.setLoves(mRefLoves);
           viewHolder.setLoveButton(Uid,key);
           viewHolder.setImage(getApplicationContext(),model.getImageUrl());


           mRefPosts.addValueEventListener(new ValueEventListener() {
               @Override
               public void onDataChange(DataSnapshot dataSnapshot) {

                   String isAnonymous = dataSnapshot.child("anonymous").getValue(String.class);
                   if (isAnonymous != null && isAnonymous.equals("true")) {
                       viewHolder.anonymousButton.setVisibility(View.VISIBLE);
                   }

               }
               @Override
               public void onCancelled(DatabaseError databaseError) {

               }
           });


            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(MainActivity.this,PostDisplayPageActivity.class);
                    intent.putExtra("Key",key);
                    startActivity(intent);

                }
            });
       }
   };
   mRecyclerView.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.notifyDataSetChanged();

}

By clicking on any view created by the adapter I am going to a new activity named PostDisplayPageActivity from my MainActivity and on back pressing I am finishing the PostDisplayPageActivity and coming back to MainActivity.

When I come back to my MainActivity the list starts from the 0 position

I used the following code in my onPause and onResume methods, but it is not working.

 @Override
protected void onPause() {
    super.onPause();

    currentVisiblePosition = ((LinearLayoutManager)mRecyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
    Toast.makeText(MainActivity.this,String.valueOf(currentVisiblePosition),Toast.LENGTH_SHORT).show();

}

@Override
protected void onResume() {
    super.onResume();

    setTheScreen();
    mRecyclerView.getLayoutManager().scrollToPosition(currentVisiblePosition);
    Toast.makeText(MainActivity.this,String.valueOf(currentVisiblePosition),Toast.LENGTH_SHORT).show();
    currentVisiblePosition = 0;


}

From those 2 Toasts I am getting the correct positions but the view always starts from position 0.

for my RecyclerView I am using LinearLayoutManager as mRecyclerView.setLayoutManager(mLinearLayoutManager);

Someone please tell me what am I doing wrong or suggest me a different way to do this.

like image 494
RAHUL PANDEY Avatar asked Oct 29 '22 04:10

RAHUL PANDEY


1 Answers

I solved my problem with a simpler solution. I just called adapter.startListening(); in onViewCreated() instead of onStart and called adapter.stopListening(); in onDestroyView() instead of onStop() That prevented the entire list from regenerating while coming back from next activity and thus retained the scroll position where it was previously.

like image 141
Sanju Avatar answered Nov 09 '22 10:11

Sanju