【问题标题】:RecyclerView scrolls to bottom whenever I load the data from Firebase每当我从 Firebase 加载数据时,RecyclerView 都会滚动到底部
【发布时间】:2019-12-07 21:01:00
【问题描述】:

基本上,每当我将数据加载到我的 recyclerView 时,它都会自动滚动到 recyclerView 的底部

我的函数如下所示:

mMessagesDBRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                final String key = getIntent().getStringExtra("key");
                final String currentUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
                if (dataSnapshot.child(key).child(currentUid).exists() && !dataSnapshot.child(currentUid).child(key).exists()) {

                    mMessagesDBRef.child(key).child(currentUid).limitToLast(TOTAL_ITEMS_TO_LOAD* mCurrentPage).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            mMessagesList.clear();
                            for (DataSnapshot snap : dataSnapshot.getChildren()) {
                                ChatMessage chatMessage = snap.getValue(ChatMessage.class);
                                mMessagesList.add(chatMessage);
                                populateMessagesRecyclerView();


                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

private void populateMessagesRecyclerView() {

    adapter = new messageAdapter(mMessagesList, this);
    mChatsRecyclerView.setAdapter(adapter);



}

这是我的一些 xml 文件:

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refreshLayout"
    android:layout_below="@id/imageView11"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_above="@id/imageView14">

    <android.support.v7.widget.RecyclerView
        android:background="#f9f9f9"
        android:id="@+id/messagesRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

有什么办法可以关闭自动滚动。谢谢

【问题讨论】:

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


【解决方案1】:

在你的 for 循环中,将你的 populateMessagesRecyclerView() 移出它

...
       for (DataSnapshot snap : dataSnapshot.getChildren()) {
                                        ChatMessage chatMessage = snap.getValue(ChatMessage.class);
                                        mMessagesList.add(chatMessage);

                                    }

          populateMessagesRecyclerView();

    }
...

如果你让这个方法在你的 for 循环中,它会创建适配器的 x 个实例,并且会多次完成这项工作,将其排除在外,你确信你只会用你的列表数据实例化适配器一次。

如果您来自 Firebase 的数据以错误的顺序出现(假设旧数据优先而不是新数据)

要解决此问题,您可以在 populateMessagesRecyclerView(); 中反转您的 RecyclerView

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setReverseLayout(true);
    mChatsRecyclerView.setLayoutManager(linearLayoutManager);

【讨论】:

  • 谢谢!它甚至通过将 populateMessagesRecyclerView() 放在外面使我的 recyclerView 更快。
  • 是的,它更快,因为只有在获取所有数据后才会调用它
猜你喜欢
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多