0
我用recyclerView在我的應用程序,並希望在添加向上滾動到頂部的按鈕,我已經在裏面添加的按鈕,但也不能當我點擊按鈕滾動我的recyclerView時,我也有顛倒我的RecyclerView,以便我添加的新帖子出現在頂部而不是底部。請提前幫助我,非常感謝。我也使用getPosition
方法,但這並不適合我。滾動到recyclerView在Android的頂部用一個按鈕
我使用過Android Studio
滾動操作類文件代碼:
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);
}
此問題以前已被詢問過很多次。 –
@ H.Brooks你可以參考一些,我已經嘗試了很多,但他們沒有爲我工作,我無法得到正確的答案 –
https://stackoverflow.com/q/32159724/5550161 –