《第一行代码Android》学习总结第三章 编写聊天界面实践

本文详细介绍如何使用Android和RecyclerView组件创建一个基本的即时聊天应用程序。从设置图片资源、添加依赖库、编写布局文件,到创建消息实体类、设计适配器及处理用户输入,全面覆盖了聊天应用开发的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、用通过Nine-Patch制作好的气泡图片作为图片资源。

2、在app/build.gradle中添加依赖库,使用RecyclerView。

compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'

3、编写activity_main.xml文件,放置用于显示聊天内容界面,EditText用于输入消息,Button用于输入消息。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8e0e8">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/msg_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/input_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Input SomeThing"
            android:maxLines="2"/>
        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"/>
    </LinearLayout>
</LinearLayout>

4、创建Msg消息实体类

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;
    }
}

5、编写RecyclerView子项布局msg_item.xml,让收到的消息左对齐,发出的消息右对齐。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">
    <LinearLayout
        android:id="@+id/left_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:background="@drawable/message_blue">
        <TextView
            android:id="@+id/left_mes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:textColor="#fff"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/right_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@drawable/message_grey">
        <TextView
            android:id="@+id/right_mes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
           />
    </LinearLayout>
</LinearLayout>

6、新建适配器类MsgAdapter类

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.left_mes);
            rightMsg = view.findViewById(R.id.right_mes);
        }
    }
    public MsgAdapter(List<Msg> mMsgList) {
        this.mMsgList = mMsgList;
    }
    @Override
    public ViewHolder onCreateViewHolder(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(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();
    }
}

7、编写MainActivity.java中代码

public class MainActivity extends AppCompatActivity {
    private List<Msg> msgList = new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initMsg();
        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send);
        msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter=new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = inputText.getText().toString();
//如果输入消息不为空,创建新的Msg对象加入到msgList中
                if(!"".equals(content)){
                    Msg msg=new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);
//当有新消息时,通知列表有新数据加入,刷新RecyclerView中的显示
                    adapter.notifyItemInserted(msgList.size()-1);
//将显示的数据定位到最后一行,保证一定能看到最后发出的一行数据
                    msgRecyclerView.scrollToPosition(msgList.size()-1);
                    inputText.setText("");
                }
            }
        });
    }
//创建消息数据
    private void initMsg() {
        Msg msg1 = new Msg("Hello Guys" ,Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Who is that" ,Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("This is Tom" ,Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值