我正在开展聊天活动,使用FirebaseRecyclerAdapter在recyclerView中显示消息,我的问题是在发送或接收消息时滚动到底部,有时它滚动到底部但是大部分时间我都要滚动手动
我想我没有做出指示:recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount());
在正确的位置
你可以建议我一个解决方案吗?!这是我的适配器:
@Override
protected void onStart() {
super.onStart();
try {
recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount());
}catch (NullPointerException e){}
FirebaseRecyclerAdapter<Message,MessageViewHolder> FBRA1 = new FirebaseRecyclerAdapter<Message, MessageViewHolder>(
Message.class,
R.layout.singlemessagelayout,
MessageViewHolder.class,
refbase1
) {
@Override
protected void populateViewHolder(MessageViewHolder viewHolder, Message model, int position) {
try {
LinearLayout lin = viewHolder.l ;
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) lin.getLayoutParams();
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
if (model.getSender().username.equals(senderUsername)){
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lin.setBackground(getResources().getDrawable(R.drawable.rmimi));
}else {
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lin.setBackground(getResources().getDrawable(R.drawable.mm));
}
layoutParams.setMargins(10,10,10,10);
viewHolder.setContent(model.date, model.message);
recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount());
}catch (NullPointerException e){
}
}
} ;
recyclerView.setAdapter(FBRA1);
recyclerView.scrollToPosition(recyclerView.getAdapter().getItemCount());
}
这是我的viewHolder类:
public static class MessageViewHolder extends RecyclerView.ViewHolder {
public View v ;
public TextView t;
public LinearLayout l ;
public MessageViewHolder(View itemView) {
super(itemView);
v = itemView ;
l = (LinearLayout)v.findViewById(R.id.mylayout) ;
t = (TextView)v.findViewById(R.id.usernameid2) ;
}
public void setContent(String date, String content) {
TextView messageContent = (TextView)v.findViewById(R.id.emailid2) ;
TextView messagesender = (TextView)v.findViewById(R.id.usernameid2) ;
messageContent.setText(content);
messagesender.setText(date);
}
}