Android基础实战之聊天界面的制作 | 带实例

聊天界面的制作

  • 首先准备message_left & message_right
  • 定义主xml文件

主要包括聊天界面下方的输入和发送,以及展示界面

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/meg_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:paddingBottom="40dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/meg_recycler_view"
        app:layout_constraintVertical_bias="1.0"
        android:background="#fff">

        <EditText
            android:id="@+id/input_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/input_text"
            android:maxLines="4"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent" />

        <Button
            android:id="@+id/btn_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="sendMsg"
            android:text="@string/btn_send"
            android:layout_gravity="bottom"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="parent"/>

    </LinearLayout>
  • 定义实体类Msg

里面包含两个字段,content表示消息的内容,type表示消息的类型,其中类型有两种,TYPE_RECEIVED和TYPE_SENT

public class Msg {

    public static final int TYPE_RECEIVED = 0;
    public static final int TYPE_SENT = 1;

    private String content;

    private int type;

    public Msg(String content, int type) {
        this.content = content;
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public int getType() {
        return type;
    }
}
  • 编写RecyclerView子项的布局msg_item.xml

让收到的消息左对齐,发送的消息右对齐并且分别使用.9.png作为背景图

到时根据判断消息的类型,来选择对属性进行隐藏和显示

<LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/message_left"
        android:gravity="start"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.045"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <TextView
            android:id="@+id/text_left_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/message_right"
        android:gravity="end"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.954"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <TextView
            android:id="@+id/text_right_msg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp" />

    </LinearLayout>
  • 创建RcyclerView适配器类MsgAdapter

在onBindViewHolder()方法中增加了对消息类型的判断

public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {

    private List<Msg> mMsgList;

    static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout leftLayout;
        LinearLayout rightLayout;

        TextView leftMsg;
        TextView rightMsg;

        public ViewHolder(View view) {
            super(view);

            leftLayout = view.findViewById(R.id.left_layout);
            rightLayout = view.findViewById(R.id.right_layout);

            leftMsg = view.findViewById(R.id.text_left_msg);
            rightMsg = view.findViewById(R.id.text_right_msg);

        }
    }

    public MsgAdapter(List<Msg> msgList) {
        mMsgList = msgList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Msg msg = mMsgList.get(position);

        if (msg.getType() == Msg.TYPE_RECEIVED) {

            //如果是收到的消息,则显示左边的消息布局,将右边的消息布局隐藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());
        } else if (msg.getType() == Msg.TYPE_SENT) {

            //如果是收到的消息,则显示左边的消息布局,将右边的消息布局隐藏
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
}
  • 修改MainActivity内的代码,初始化一些数据,并增加按钮事件

在initMsg()中初始化了几条数据在RecyclerView中显示,获取EditText中的内容,若不为空则创建一个新的Msg对象,并将之添加至msgList中

调用了适配器的notifyItemInserted()方法,用于通知列表有新的数据插入,这样新增的消息才能够在recyclerView中显示

调用RecyclerView的scrollToPosition()方法将显示的数据定位到最后一行,以保证一定可以看到最后发出的消息

最后调用setText将数据清空

public class MainActivity extends AppCompatActivity {

    private List<Msg> mMsgList = new ArrayList<>();

    private EditText inputText;

    private RecyclerView mRecyclerView;

    private MsgAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化数据
        initMsg();
        inputText = findViewById(R.id.input_text);
        mRecyclerView = findViewById(R.id.meg_recycler_view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new MsgAdapter(mMsgList);
        mRecyclerView.setAdapter(mAdapter);

        // 将RecyclerView定位到最后一行
        mRecyclerView.scrollToPosition(mMsgList.size() - 1);


    }

    private void initMsg() {
        Msg msg1 = new Msg("Hello guys",Msg.TYPE_RECEIVED);
        mMsgList.add(msg1);
        Msg msg2 = new Msg("Hi,who is that?",Msg.TYPE_SENT);
        mMsgList.add(msg2);
        Msg msg3 = new Msg("This is Tom,Nice to meet you.",Msg.TYPE_RECEIVED);
        mMsgList.add(msg3);

        mMsgList.add(new Msg("Oh.me too",Msg.TYPE_SENT));
        mMsgList.add(new Msg("emmm,can you tell me your name?",Msg.TYPE_RECEIVED));
        mMsgList.add(new Msg("Oh,sorry,i just forgot to introduce myself,i am qricis",Msg.TYPE_SENT));
        mMsgList.add(new Msg("that's ok,what a beautiful name and i like it",Msg.TYPE_RECEIVED));
        mMsgList.add(new Msg("Oh,thanks for your like",Msg.TYPE_SENT));
        mMsgList.add(new Msg("By the way,how about go lunch together?",Msg.TYPE_RECEIVED));
        mMsgList.add(new Msg("Sure,i am just so hungry now",Msg.TYPE_SENT));

    }

    public void sendMsg(View view) {
        String content = inputText.getText().toString();

        if(!"".equals(content)) {
            Msg msg = new Msg(content,Msg.TYPE_SENT);
            mMsgList.add(msg);

            //有新消息时,刷新RecyclerView中的显示
            mAdapter.notifyItemInserted(mMsgList.size() - 1);

            //将RecyclerView定位到最后一行
            mRecyclerView.scrollToPosition(mMsgList.size() - 1);

            //清空输入框中的内容
            inputText.setText("");

        }
    }

}
  • 下载地址

https://github.com/qricis/DoSomeAndroidTest/tree/main/ChatView

 

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值