我很新到Android / Java开发。我的工作,涉及到一个聊天活动项目。
我希望活动以自动滚动到聊天屏幕的底部(最近的聊天消息)。
我试着用吐司显示为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;
}
}
你好@Asif斌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名列榜首