我对Android / Java开发非常陌生。我正在从事一个涉及聊天活动的项目。
我希望活动自动滚动到聊天屏幕(最新聊天消息)的底部。
我尝试使用Toast显示getItem计数-我能够验证是否可以正确拾取Recyclerview项目的int位置。但是,我正在努力使用ScrollToPosition或SmoothScroltoPosition来获得所需的结果。
关于如何自动滚动到最近的聊天项目的任何想法?
以下是我的聊天活动:
公共类ChatActivity扩展了AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mChatAdapter;
private RecyclerView.LayoutManager mChatLayoutManager;
private String currentUserID, matchId, chatId;
private EditText mSendEditText;
private Button mSend;
DatabaseReference mDatabaseChat;
DatabaseReference mDatabaseChatDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
matchId = getIntent().getExtras().getString("matchid");
currentUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabaseChat = FirebaseDatabase.getInstance().getReference().child("User").child(currentUserID).child("connections").child("matches").child(matchId).child("ChatId");
mDatabaseChatDB = FirebaseDatabase.getInstance().getReference().child("Chat");
getChatID();
mSend = (Button) findViewById(R.id.send);
mSendEditText = (EditText) findViewById(R.id.message);
mSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
private void sendMessage() {
String sendMessageText = mSendEditText.getText().toString();
if (!sendMessageText.isEmpty()){
DatabaseReference newMessageDb = mDatabaseChatDB.push();
Map newMessage = new HashMap<>();
newMessage.put("createdByUser",currentUserID);
newMessage.put("text", sendMessageText);
newMessageDb.setValue(newMessage);
}
mSendEditText.setText(null);
}
});
mRecyclerView = (RecyclerView) findViewById(R.id.chatview);
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(false);
mChatLayoutManager = new LinearLayoutManager(ChatActivity.this);
mRecyclerView.setLayoutManager(mChatLayoutManager);
mChatAdapter = new ChatAdapter(getDataSetChat(),ChatActivity.this);
mRecyclerView.setAdapter(mChatAdapter);
}
private void getChatID(){
mDatabaseChat.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
chatId = dataSnapshot.getValue().toString();
mDatabaseChatDB = mDatabaseChatDB.child(chatId);
getChatMessages();
}
}
private void getChatMessages() {
mDatabaseChatDB.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if (dataSnapshot.exists()){
String message = null;
String currentuser = null;
if (dataSnapshot.child("text").getValue() != null ){
message = dataSnapshot.child("text").getValue().toString();
}
if (dataSnapshot.child("createdByUser").getValue() != null ){
currentuser = dataSnapshot.child("createdByUser").getValue().toString();
}
if(message!= null && currentuser !=null){
Boolean currentUserBoolean = false;
if(currentuser.equals(currentUserID)){
currentUserBoolean = true;
}
ChatObject newMessage = new ChatObject(message,currentUserBoolean);
resultsChat.add(newMessage);
mChatAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private ArrayList<ChatObject> resultsChat = new ArrayList<ChatObject>();
private List<ChatObject> getDataSetChat() {
return resultsChat;
}
}
答案 0 :(得分:0)
您好@Asif Bin Zahit要将最新消息显示在顶部,您必须对代码进行如下更改
在数组列表中添加最新消息后 resultsChat.add(newMessage) 您必须按降序对数组列表进行排序,然后在调用notifyDataSetChanged
之后如果在设置适配器之前已经在列表中进行了记录,则必须按如下所述对列表进行排序,然后再将适配器设置为回收站视图
Collections.sort(getDataSetChat())
mChatAdapter = new ChatAdapter(getDataSetChat(),ChatActivity.this);
mRecyclerView.setAdapter(mChatAdapter);
将新消息添加到列表中
resultsChat.add(newMessage);
Collections.sort(resultsChat);
mChatAdapter.notifyDataSetChanged();
它将始终将您的最新消息添加到recyclerview列表的顶部