活动开始使用Linearlayoutmanager时,将Recycleview滚动到顶部位置

时间:2019-01-12 13:06:53

标签: java android firebase-realtime-database android-recyclerview recycler-adapter

我正在将 Firebase RealTime数据库检索到我的 Recycleview适配器类。我想按日期对数据进行排序,并使用最新的帖子。我正在使用

llm.setReverseLayout(true);

起作用,但是现在当活动打开时,recycleview会自动滚动到底部。但是我希望它可以将其强制到顶部。

这是我的代码

         rv.setHasFixedSize(true);
         LinearLayoutManager llm = new LinearLayoutManager(this);
         llm.setReverseLayout(true);
         rv.setLayoutManager(llm);
         llm.scrollToPositionWithOffset(0, 0);
         rv.setAdapter(staggeredBooksAdapter);

我的整个代码是

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();
                }


   
}

我用过

/Solution one
mLayoutManager.scrollToPositionWithOffset(0, 0);
//Solution Two
rv.scrollToPosition(0)
//Solution Three
setStackFromEnd(false)
//Solution Four
Collections.reverse();

但是他们没有用。

有解决方案吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

不要使用任何设置,现在将false传递给llm.setReverseLayout(false);

当您从firebase中获取数据时,只需使用Collections.reverse(passyourlist);,该列表将就地反转。 之后,将其传递到您的适配器,它将可以正常工作。