1.先设置背景聊天文件的图片
资源管理器中找到安装文件下的tools->draw9patch.bat的文件,双击可以运行,在里面打开将背景图片加入编辑,并保存替换原来的图片
2.设置依赖库
在app/build.gradle中添加依赖库(目的为了使用recyclerview这个控件)
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' compile 'com.android.support:recyclerview-v7:24.2.1' testCompile 'junit:junit:4.12' } 并同步 3.修改主界面的布局内容 设置背景图片,加入RecyclerView的控件(显示消息),EditText(编辑发送内容),Button(点击发送)控件
4.建立消息的类(包含两个字段,content是内容,type是类型表示接受的消息还是发送的消息)
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子项的布局(新建布局)
6.建立RecyclerView的适配器类(MsgAdapter) //添加背景图片表示左边的消息 //放在左边的消息 //添加背景图片表示右边的消息 //放在右边的消息
public class MsgAdapter extends RecyclerView.Adapter{///继承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=(LinearLayout)view.findViewById(R.id.left_layout);///找到左边的背景布局图片 rightLayout=(LinearLayout)view.findViewById(R.id.right_layout); leftMsg=(TextView) view.findViewById(R.id.left_msg);//找到左边的信息 rightMsg=(TextView) view.findViewById(R.id.right_msg); } } public MsgAdapter(List msgList)//构造函数从外部传入初始化过的消息 { mMsgList=msgList; } 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.完成主活动
public class MainActivity extends AppCompatActivity { private ListmsgList=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_text);//将定义的编辑框从布局中找到初始化 send=(Button)findViewById(R.id.send);//将定义的发送按钮Congo布局中找到初始化 msgRecyclerView=(RecyclerView)findViewById(R.id.msg_reccler_view);//将定义的RecyclerView控件从布局中找到初始化 LinearLayoutManager layoutManager=new LinearLayoutManager(this);///线性管理器 msgRecyclerView.setLayoutManager(layoutManager);///RecyclerView设置线性管理其 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);//编辑不空,则将内容和类型打包为Msg,并添加到列表 adapter.notifyItemInserted(msgList.size()-1); msgRecyclerView.scrollToPosition(msgList.size()-1); inputText.setText("");//编辑框设置为空 } } }); } private void initMsgs()//初识化消息将三个消息放入消息列表中 { Msg msg1=new Msg("Hello guy.",Msg.TYPE_RECEIVED); msgList.add(msg1); Msg msg2=new Msg("Hello.Who is that?",Msg.TYPE_SENT); msgList.add(msg2); Msg msg3=new Msg("This is Tom.Nice talking",Msg.TYPE_RECEIVED); msgList.add(msg3); } }