简体   繁体   English

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

[英]Scroll Recycleview to Top position when activity starts using Linearlayoutmanager

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 我正在将Firebase RealTime数据库检索到我的Recycleview Adapter类中 。我想按日期对数据进行排序,最上面的帖子在前。我正在使用

llm.setReverseLayout(true);

which works but now when activity opens then recycleview automatically scrolls to bottom itself. 可以,但是现在当活动打开时, recycleview自动滚动到底部。 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); 不要使用开箱即用的设置,现在将false传递给llm.setReverseLayout(false);

When you get the data from firebase just use Collections.reverse(passyourlist); 当您从Collections.reverse(passyourlist);获取数据时,只需使用Collections.reverse(passyourlist); the list would be reversed inplace. 该列表将就地反转。 After that pass it to your adapter and it would work fine. 之后,将其传递给您的适配器,即可正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将 Recycleview position 传递给另一个活动 - Unable to pass Recycleview position to another activity 滚动上的 Recycleview 在最后一个项目位置抛出 java.lang.IndexOutOfBoundsException - Recycleview on scroll throws java.lang.IndexOutOfBoundsException on last item position 从Activity的startOnActivityResult更新RecycleView特定位置 - Update RecycleView particular position from Activity's startOnActivityResult 滚动RecycleView时,为什么我的TextView文本大小会发生变化? (使用AutoResizeTextView) - Why is my TextView text size changing when I scroll RecycleView? (using AutoResizeTextView) 在ScrollView(ANDROID)内的无限RecycleView中保留滚动位置 - Retain scroll position in infinite RecycleView inside ScrollView (ANDROID) 使用自定义LinearLayoutManager - Using custom LinearLayoutManager 刷新 RecyclerView 中的数据并保持其滚动 position 将用户带到活动的顶部 - Refreshing data in RecyclerView and keeping its scroll position take user to the top of the activity 应用程序在活动开始时崩溃 - application crashes when an activity starts 活动开始时加载片段 - load fragment when activity starts 从另一个活动返回活动时,RecyclerView 的滚动位置丢失 - Scroll position of RecyclerView lost when returning back to activity from another activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM