Scroll Recycleview to Top position when activity starts using Linearlayoutmanager

21
January 12, 2019, at 10:00 PM

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.

READ ALSO
Is there a way to visualize how a fragment would look like when it&#39;s loaded in adnroid studio inspector?

Is there a way to visualize how a fragment would look like when it's loaded in adnroid studio inspector?

I'm making an android app that has a couple of fragments in itI was wondering if you could look at how your fragments would look like when they are loaded in the layout inspector

40
xamarin android resource management

xamarin android resource management

I had a newly created Xamarin Android native app and was trying to display a three dots icon for popup menu (⋮)Using this xml it worked

47
Volley non-responsive after 200 records being returned?

Volley non-responsive after 200 records being returned?

I have an android app that queries an online database displays records in a listviewIt works fine until 200 records, after which there isn't any response or error

44
change visibility of Recyclerview.ViewHolder items

change visibility of Recyclerview.ViewHolder items

I have a custom recyclerview adapterBased on some conditions I want to change the visibility of ViewHolder items

44