英文:
How to reverse recyclerView layout like a chat app with FirebaseUI FirestoreRecyclerAdapter?
问题
我正在使用FirestoreRecyclerAdapter在Java中制作聊天应用程序。在添加一条消息时,它的运行良好,但我希望消息显示在底部,而不是顶部。我尝试过:
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
现在它已经倒转了位置,这是很好的,但它没有自动滚动到底部位置。当我添加新消息时,我需要手动滚动才能看到它。
英文:
I am using FirestoreRecyclerAdapter to make a chat app in java. When adding a message it works fine but I want messages to show at bottom not on top. I tried
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
Now it reverse it's position which is good but it's not automatically scrolling to bottom position. And when I add a new message I need to scroll manually to see it.
答案1
得分: 0
最佳方法是在有新消息时使用以下代码片段。您可以使用快照监听器实现这一点。
y = (在这里,您需要尝试两件事情。由于我的编程笔记本不在身边,所以我无法测试并告诉您,但是 y 可以是 0,或者是 chatlist.size() - 1);
layoutManager.scrollToPosition(y);
或者
layoutManager.smoothScrollToPosition(y); // 对于聊天应用程序,我不建议使用此方法,因为如果在用户下方有大量文本,下滑将需要一些时间
英文:
The best way to do it is use the following piece snippet whenever there is a new message. You can use a snapshot listener for that
y = (Here you need to try 2 things. My coding laptop is not with me so I cannot test and tell but either 0 should work or the chatlist.size()-1);
layoutManager.scrollToPosition(y);
OR
layoutManager.smoothScrollToPosition(y);// I do not recommend this for chat apps since it will take time to go down if there are a lot of texts below the user
答案2
得分: 0
Recyclerview 在添加以下代码行后会自动滚动到底部:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(0);
}
});
}
}, 1000);
如果父布局是水平滚动,则上述解决方法无效。
另一种解决方法是:
recyclerView.scrollToPosition(0);
这两行代码都对我有效。您可以使用任何一种解决方法。
英文:
Recyclerview automatically scroll to bottom after adding below lines
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
recyclerView.smoothScrollToPosition(0);
}
});
}
}, 1000);
above solution will not work if parent layout is horizontal scroll.
Other solution is
recyclerView.scrollToPosition(0);
Both line of code work for me. you can use any of the solution
答案3
得分: 0
以下是翻译好的部分:
这是我使用的:
@Override
public void onStart() {
super.onStart();
adapter.startListening();
recyclerView.scrollToPosition(0);
}
还要在适配器中使用:
@Override
public void onDataChanged() {
super.onDataChanged();
recyclerView1.smoothScrollToPosition(0);
}
在适配器中我之前在 onCreate
方法中使用了:
recyclerView.scrollToPosition(0);
并且适配器在 onStart
中开始。现在它正常工作了。
英文:
This is what I used:
@Override
public void onStart() {
super.onStart ();
adapter.startListening();
recyclerView.scrollToPosition (0);
}
Also use
@Override
public void onDataChanged() {
super.onDataChanged ();
recyclerView1.smoothScrollToPosition (0);
}
in adapter
I was using
recyclerView.scrollToPosition (0);
in onCreate and adapter was starting in onStart. Now it's working
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论