3-18.编写聊天界面

制作Nine-Patch图片

Nine-Patch图片,被特殊处理过的png图片,能够指定哪些区域可以被拉伸、哪些区域不可以。

将这上面的图片设置为一个LinearLayout的背景图片,修改 activity_main.xml 中的代码, 如下所示。

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/message_left">

</LinearLayout>

运行效果如下。

Google因为draw9patch热门的原因,把draw9patch.bat集成在Android Studio里面了。

在AS中,右键需要编辑的图片,选择Create 9-Patch file进行编辑,会自动生成.9格式的图片,打开该格式的图片可进行编辑。

在边框部分绘制黑点表示图片需要拉伸时就拉伸黑点标记的部分。

运行效果如下。

编写聊天界面

添加依赖库

在app/build.gradle中添加RecyclerView的依赖库。

1
compile 'com.android.support:recyclerview-v7:24.2.1'

编写主界面

修改activity_main.xml文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/input_text"
android:layout_weight="1"
android:hint="Type something here"
android:maxLines="2"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/send"
android:text="Send"/>

</LinearLayout>

</LinearLayout>

定义消息的实体类

新建消息的实体类Msg。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class  {
public static final int TYPE_RECEIVED=0;
public static final int TYPE_SENT=1;
private String content;
private int type;

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

public String getContent(){
return content;
}

public int getType(){
return type;
}
}

编写RecyclerView的子项

新建msg_item.xml。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_layout"
android:layout_gravity="left"
android:background="@drawable/message_left">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" 大专栏  3-18.编写聊天界面an>
android:id="@+id/left_msg"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff"/>

</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_layout"
android:layout_gravity="right"
android:background="@drawable/message_left">

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

</LinearLayout>

</LinearLayout>

创建RecyclerView的适配器

新建MsgAdapter类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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=(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<Msg> 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);
}


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


public int getItemCount() {
return mMsgList.size();
}
}

为RecyclerView加入初始化数据&为发送按钮注册监听事件

修改MainActivity。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class MainActivity extends AppCompatActivity {

private List<Msg> msgList=new ArrayList<>();
private EditText inputText;
private Button send;
private RecyclerView msgRecyclerView;
private MsgAdapter adapter;


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() {

public void onClick(View view) {
String content=inputText.getText().toString();
if(!"".equals(content)){
Msg msg=new Msg(content,Msg.TYPE_SENT);
msgList.add(msg);
adapter.notifyItemInserted(msgList.size()-1);
msgRecyclerView.scrollToPosition(msgList.size()-1);//将ListView定位到最后一行
inputText.setText("");//清空输入框中的内容
}
}
});
}

private void initMsg(){
Msg msg1=new Msg("1",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2=new Msg("2",Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3=new Msg("2",Msg.TYPE_RECEIVED);
msgList.add(msg3);
}
}

运行效果如下。

猜你喜欢

转载自www.cnblogs.com/dajunjun/p/11711067.html