android 实战 制作聊天界面

RecyclerView学了这么久,也应该来试一下实战了,这次来试一下做一个聊天界面,做聊天界面需要两张图片来分别做收到信息和发送信息的背景图,是需要制作Nine-Patch图片,做Nine-Patch图片也很简单,直接右键生成然后编辑就行,不过要注意的是,图片的后缀要是png才可以

我们先来看一下效果图
android 实战 制作聊天界面_第1张图片
看起来挺不错的,接下来来实现一下
因为我们会用到RecyclerView,所以首先添加一下依赖,打开app/build.gradle添加依赖

   dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        implementation 'com.android.support:recyclerview-v7:28.0.0'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }

接着来编写一下主页面,修改activity_main.xml




    

    



    
        

布局挺简单的,就不说了
接下来定义一下消息的实体类,新建Msg.java

package com.example.ninepatch;

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

用content表示消息的内容,type表示消息的类型,消息类型有两个值,0代表收到的信息,1代表发送的信息

接下来编写RecyclerView子项的布局,新建msg_item.xml




    

        

    


    




这里收到的消息居左对齐,发送的消息居右对齐,用我们之前制作的ninepatch图片作为背景
接着创建RecyclerView的适配器类,通过判断来隐藏消息布局,新建MsgAdapter.java

package com.example.ninepatch;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter {

    private List 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_msg);
            rightMsg = view.findViewById(R.id.right_msg);
        }
    }

public MsgAdapter(List msgList){
        mMsgList = msgList;
}
@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();
}

}

最后来初始化一下数据,并给按钮加入监听事件,修改MainActivity.java

package com.example.ninepatch;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List 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);

        initMsgs();
        inputText = (EditText)findViewById(R.id.input_txet);
        send = (Button) findViewById(R.id.send);
        msgRecyclerView = (RecyclerView)findViewById(R.id.msg_recycle_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 v) {
                String content = inputText.getText().toString();

                if(!"".equals(content)){
                    Msg msg = new Msg(content,Msg.TYPE_SENT);
                    msgList.add(msg);
                    //当有新消息,刷新RecyclerVeiw的显示
                    adapter.notifyItemInserted(msgList.size() - 1);
                    //将RecyclerView定位到最后一行
                    msgRecyclerView.scrollToPosition(msgList.size() - 1);
                    //清空输入框内容
                    inputText.setText("");

                }
            }
        });
    }
    private void initMsgs(){
        Msg msg1 = new Msg("你好哇,李银河",Msg.TYPE_RECEIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("Hi",Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("当我跨过沉沦到一切,向着永恒开战的时候,你是我的军旗",
        Msg.TYPE_RECEIVED);
        msgList.add(msg3);
    }
}

我们在 initMsgs()方法中先初始化了几条数据用于RecyclerView显示,然后在发送按钮的点击事件理获取了EditText中的内容,如果内容不为空字符串则创建出一个新的Msg对象,并把它添加到msgList列表中,又调用适配器的notifyItemInserted()方法,用于通知列表有新的数据插入,这样新的消息才能在RecycleView中显示,接着调用RecyclerView的scrollToPosition()方法将显示的数据定位到最后一行,这样一个简单的界面就做好了,以后可以继续优化

你可能感兴趣的:(Android,studio学习笔记)