RecyclerView,显示我在recyclerView上发送的最后一条消息

时间:2018-06-05 17:54:58

标签: java android android-layout android-recyclerview

我遇到了这个代码的问题,我创建了一个个人聊天,但是当我发送消息时,RecyclerView没有自动显示最后发送的消息但我必须向下滚动,我该如何确保自动显示最后一条消息?

公共类ChatListAdapter扩展了RecyclerView.Adapter {

private Activity mActivity;
private DatabaseReference mDataBaseReference;
private String mDisplayName;
private ArrayList<DataSnapshot> mDataSnapshot;


private ChildEventListener mListener = new ChildEventListener () {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot,String s) {

        mDataSnapshot.add (dataSnapshot);
        notifyDataSetChanged ();

    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot,String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot,String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
};




public ChatListAdapter(Activity activity, DatabaseReference ref, String name){

    mActivity = activity;
    mDataBaseReference = ref.child ("messaggi");
    mDisplayName = name;
    mDataSnapshot = new ArrayList <> ();

    mDataBaseReference.addChildEventListener (mListener);
}

public class ChatViewHolder extends RecyclerView.ViewHolder{

    TextView autore;
    TextView messaggio;
    LinearLayout.LayoutParams params;


    public ChatViewHolder(View itemView) {
        super (itemView);

        autore = (TextView)itemView.findViewById (R.id.tv_autore);
        messaggio = (TextView)itemView.findViewById (R.id.tv_messaggio);
        params = (LinearLayout.LayoutParams) autore.getLayoutParams ();
    }
}


@NonNull
@Override
public ChatViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType) {

    LayoutInflater inflater = (LayoutInflater)mActivity.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate (R.layout.chat_msg_row, parent, false);
    ChatViewHolder vh = new ChatViewHolder (v);

    return vh;
}

@Override
public void onBindViewHolder(@NonNull ChatViewHolder holder,int position) {

    DataSnapshot snapshot = mDataSnapshot.get (position);

    Messaggio msg = snapshot.getValue (Messaggio.class);

    holder.autore.setText (msg.getAutore ());
    holder.messaggio.setText (msg.getMessaggio ());

    boolean sonoIo = msg.getAutore ().equals (mDisplayName);
    setChatItemStyle(sonoIo, holder);


}

private void setChatItemStyle(boolean sonoIo, ChatViewHolder holder){
    if(sonoIo){

        holder.params.gravity = Gravity.END;
        holder.autore.setTextColor (Color.GREEN);
        holder.messaggio.setBackgroundResource(R.drawable.in_msg_bg);


    }else{

        holder.params.gravity = Gravity.START;
        holder.autore.setTextColor (Color.CYAN);
        holder.messaggio.setBackgroundResource(R.drawable.out_msg_bg);

    }

    holder.autore.setLayoutParams (holder.params);
    holder.messaggio.setLayoutParams (holder.params);


}


@Override
public int getItemCount() {
    return mDataSnapshot.size ();
}

public void clean(){
    mDataBaseReference.removeEventListener (mListener);
}

}

2 个答案:

答案 0 :(得分:0)

您可以在stackFromEnd上将LinearLayoutManager设为真。如果您的聊天消息最近会回来,您也可以在经理而不是数据中撤消。然后,要自动滚动更改的数据集,只需将android:transcriptMode设置为normal(仅在底部滚动)或alwaysScroll(在更改后始终滚动到底部)在RecyclerView xml上。< / p>

boolean reverseLayout = true; // Or false if your data is already reversed
LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, reverseLayout);
manager.setStackFromEnd(true);
yourRecyclerView.setLayoutManager(manager);

答案 1 :(得分:0)

我假设您正在使用回收站视图来填充邮件。

所以这里是

变量声明

private RecyclerView rvChat;

onCreate

rvChat = findViewById(R.id.a_individual_chat_rv_chat);

onChildAdded(在notifyDataSetChanged之后)

rvChat.smoothScrollToPosition(rvChat.getAdapter().getItemCount());