gpt4 book ai didi

java - 如何让RecyclerView在每次将项目添加到列表时不滚动到底部

转载 作者:行者123 更新时间:2023-12-02 00:42:13 36 4
gpt4 key购买 nike

当我的聊天加载时,我希望我的 RecyclerView 滚动到底部,但是当用户手动向上滚动列表以阅读以前的消息时,它不应该在列表中添加新项目时再次向下滚动,除非消息(新项目)由用户自己发送/添加。

我尝试使用layoutManager.setStackFromEnd(true) 和recyclerView.scrollToPosition(list.size() - 1)。如果我手动向上滚动,这两种方法都会在收到新消息时自动向下滚动列表,除非我是发送消息的人,否则它不应再次向下滚动到底部。

mref.child(RoomName).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()) {
ChatMessage value = dataSnapshot1.getValue(ChatMessage.class);
ChatMessage fire = new ChatMessage();
String msgtxt = value.getMessageText();
String user=value.getMessageUser();
long msgtime=value.getMessageTime();
String prothumb=value.getProfuri();
String sentimguri=value.getSentimguri();
String type=value.getType();

fire.setMessageUser(user);
fire.setMessageText(msgtxt);
fire.setMessageTime(msgtime);
fire.setProfuri(prothumb);
fire.setSentimguri(sentimguri);
fire.setType(type);
list.add(fire);
}
adapter = new RecyclerViewAdapter(ChatRoom.this, list);
recyclerView.setAdapter(adapter);

//layoutManager.setStackFromEnd(true);
}

//Initializing RecyclerView
private void initRecyclerView() {
//Log.d(TAG, "initRecyclerView: init recyclerview.");
recyclerView =(RecyclerView) findViewById(R.id.list_of_messages);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
//recyclerView.scrollToPosition(list.size() - 1);
}
}

最佳答案

获取聊天列表后,您不必每次都创建新的适配器实例。而只需通知适配器有关更改即可。

mref.child(RoomName).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
list.clear();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()) {
ChatMessage value = dataSnapshot1.getValue(ChatMessage.class);
ChatMessage fire = new ChatMessage();
String msgtxt = value.getMessageText();
String user=value.getMessageUser();
long msgtime=value.getMessageTime();
String prothumb=value.getProfuri();
String sentimguri=value.getSentimguri();
String type=value.getType();

fire.setMessageUser(user);
fire.setMessageText(msgtxt);
fire.setMessageTime(msgtime);
fire.setProfuri(prothumb);
fire.setSentimguri(sentimguri);
fire.setType(type);
list.add(fire);
}

if(!(recyclerView.canScrollVertically(1)))
{
recyclerView.smoothScrollToPosition(adapter.getItemCount());
}

adapter.notifyDataSetChanged();
//layoutManager.setStackFromEnd(true);
}

//Initializing RecyclerView
private void initRecyclerView() {
//Log.d(TAG, "initRecyclerView: init recyclerview.");
recyclerView =(RecyclerView) findViewById(R.id.list_of_messages);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);

//initialse your adapter only once
adapter = new RecyclerViewAdapter(ChatRoom.this, list);
recyclerView.setAdapter(adapter);
layoutManager.setStackFromEnd(true);

//recyclerView.scrollToPosition(list.size() - 1);
}

}

这可能会解决您的问题。

关于java - 如何让RecyclerView在每次将项目添加到列表时不滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57926037/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com