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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With