I"m retrieving Firebase RealTime Database to my Recycleview Adapter Class. I want to sort the data by date with latest post at top. I'm using
llm.setReverseLayout(true);
which works but now when activity opens then recycleview
automatically scrolls to bottom itself. But I want it to force it to Top.
Here is my Code
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setReverseLayout(true);
rv.setLayoutManager(llm);
llm.scrollToPositionWithOffset(0, 0);
rv.setAdapter(staggeredBooksAdapter);
My whole code is
public class SubjectBooks extends AppCompatActivity {
FirebaseAuth fauth;
String user;
DatabaseReference dbreference,dbref;
RecyclerView rv;
String subject;
int child_count=0;
ArrayList<Books> list;
SubjectBooksAdapter staggeredBooksAdapter;
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_subject_books);
getSupportActionBar().setTitle(getIntent().getStringExtra("subject")+" Area");
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#8abe50")));
// this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// this.getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_share);
fauth = FirebaseAuth.getInstance();
subject = getIntent().getStringExtra("subject").trim();
dbreference = FirebaseDatabase.getInstance().getReference("books").child(subject);
dbreference.keepSynced(true);
user = fauth.getCurrentUser().getEmail();
list = new ArrayList<>();
staggeredBooksAdapter = new SubjectBooksAdapter(list);
rv = (RecyclerView) findViewById(R.id.recyclerView);
final ProgressDialog progressDialog = ProgressDialog.show(this, "Loading...", "Please wait...", true);
progressDialog.setMessage("Please wait ...");
progressDialog.setCanceledOnTouchOutside(true);
progressDialog.show();
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
rv.setAdapter(staggeredBooksAdapter);
dbreference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
final Books b1 = dataSnapshot.getValue(Books.class);
// Log.e("Value is ",dataSnapshot.getKey()+" "+b1.getBauthor());
//Log.e("Book"," received");
if(!user.equals(b1.getSelleremail()) || (user.equals(b1.getSelleremail())) ) {
child_count++;
list.add(b1);
staggeredBooksAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}
I used
/Solution one
mLayoutManager.scrollToPositionWithOffset(0, 0);
//Solution Two
rv.scrollToPosition(0)
//Solution Three
setStackFromEnd(false)
//Solution Four
Collections.reverse();
But they didn't work.
Any Solution? Thanks in advance.
Don't use set anything out of the box now pass false to
llm.setReverseLayout(false);
When you get the data from firebase just use
Collections.reverse(passyourlist);
the list would be reversed inplace. After that pass it to your adapter and it would work fine.