0

I have used recyclerView in my application and want to add a scroll up to top button in that, I have added the button in it but could not be able to scroll my recyclerView on top when I clicks the button, also I have reversed my RecyclerView so that the new post I add in it comes on top not on bottom. please help me, much thanks in advance. I have also used getPosition method but that did not work for me.

I am using Android Studio

scroll action class file code:

public class HomePage extends AppCompatActivity {

private FirebaseAuth mAuth;
private RecyclerViewPager mrecyclerView;
private DatabaseReference mDatabaseReference;
private Button rollUp;
public static int recyclerPos = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);
    mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("post");
    mrecyclerView = (RecyclerViewPager) findViewById(R.id.list);
    final LinearLayoutManager layout = new LinearLayoutManager(HomePage.this, LinearLayoutManager.VERTICAL, false);
    layout.setReverseLayout(true);//<---- to get new posted data on top of list
    layout.setStackFromEnd(true);//<-----
    mrecyclerView.setLayoutManager(layout);
    rollUp = (Button) findViewById(R.id.roll_up);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mAuth = FirebaseAuth.getInstance();

    this.mrecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        int mLastFirstVisibleItem = 0;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            final int currentFirstVisibleItem = layout.findFirstVisibleItemPosition();

            if (currentFirstVisibleItem < this.mLastFirstVisibleItem) {
                HomePage.this.getSupportActionBar().hide();
            } else if (currentFirstVisibleItem > this.mLastFirstVisibleItem) {
                HomePage.this.getSupportActionBar().show();
            }
            this.mLastFirstVisibleItem = currentFirstVisibleItem;
        }
    });

}

    private void sentToStart() {
    Intent intent = new Intent(HomePage.this, LoginPage.class);
    startActivity(intent);
    finish();
}
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_home_page, menu);
    return true;
}
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    int id = item.getItemId();

    if (id == R.id.action_settings){
        Intent intentSettings = new Intent(HomePage.this,settings.class);
        startActivity(intentSettings);
        LinearLayoutManager layoutManager = ((LinearLayoutManager) mrecyclerView.getLayoutManager());
        recyclerPos = layoutManager.findFirstVisibleItemPosition();
    }
    if (id == R.id.roll_up){
        LinearLayoutManager layoutManagerroll = ((LinearLayoutManager) mrecyclerView.getLayoutManager());
        recyclerPos = layoutManagerroll.findFirstVisibleItemPosition();
        layoutManagerroll.scrollToPositionWithOffset(0,0);

    }
    return super.onOptionsItemSelected(item);
}
rohit b
  • 75
  • 13

1 Answers1

0

The problem is that your position is wrong, since you want to scroll to the top which is element at 0 position you have to Do this mRecyclerView.smoothScrollToPosition(0); Not this mrecyclerView.smoothScrollToPosition(recyclerPos.getItemCount()-1);.
as you are scrolling to last but one element. where the position is wrong

Also to add a element at a particular position use this addItemAtPosition() and don't callnotifyDataSetChanged() no need to inverse the whole list like you have mentioned.

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52