Android ListView仿微信聊天界面
作者:luinsist 发布时间:2023-10-15 04:59:46
标签:Android,微信,聊天
Android ListView仿聊天界面效果图的具体代码,供大家参考,具体内容如下
1.首先页面总布局(ListView + LinearLayout(TextView+Button))
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/msg_list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="#000000"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/input_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:maxLines="2"/>
<Button
android:id="@+id/send"
android:text="发送"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
2.为ListView定制Adapter
public class MsgAdapter extends ArrayAdapter<Msg>{
private int resourceID;
public MsgAdapter(Context context, int resource, List<Msg> objects) {
super(context, resource, objects);
resourceID = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Msg msg = getItem(position);
View view;
ViewHolder viewHolder;
if(convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceID, null);
viewHolder = new ViewHolder();
viewHolder.leftLayout = (LinearLayout)view.findViewById(R.id.left_layout);
viewHolder.rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);
viewHolder.leftMsg = (TextView) view.findViewById(R.id.left_msg);
viewHolder.rightMsg = (TextView) view.findViewById(R.id.right_msg);
view.setTag(viewHolder);
}else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
if(msg.getType() == Msg.MSG_RECEIVE) {
viewHolder.leftLayout.setVisibility(View.VISIBLE);
viewHolder.rightLayout.setVisibility(View.GONE);
viewHolder.leftMsg.setText(msg.getMessage());
}else {
viewHolder.rightLayout.setVisibility(View.VISIBLE);
viewHolder.leftLayout.setVisibility(View.GONE);
viewHolder.rightMsg.setText(msg.getMessage());
}
return view;
}
class ViewHolder {
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
}
}
public class Msg {
public static final int MSG_RECEIVE = 0;
public static final int MSG_SEND = 1;
private int type;
private String content;
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public String getMessage() {
return content;
}
public int getType() {
return type;
}
}
3.ListView单个view布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/left_layout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:gravity="center"
>
<ImageView
android:id="@+id/left_image"
android:src="@drawable/yan"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/msg">
<TextView
android:id="@+id/left_msg"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="end"
android:gravity="center"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/msg">
<TextView
android:id="@+id/right_msg"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
<ImageView
android:id="@+id/right_image"
android:src="@drawable/meng"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
</LinearLayout>
4.ListView加载Adapter
public class MainActivity extends Activity {
private ListView listView;
private MsgAdapter msgAdapter;
private List<Msg> msgList = new ArrayList<Msg>();
private EditText input;
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.msg_list_view);
initMsg();
msgAdapter = new MsgAdapter(this, R.layout.msg_item, msgList);
listView.setAdapter(msgAdapter);
input = (EditText) findViewById(R.id.input_text);
send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String message = input.getText().toString();
if(!"".equals(message)) {
Msg msg = new Msg(message, Msg.MSG_SEND);
msgList.add(msg);
msgAdapter.notifyDataSetChanged();//当有新消息时刷新
listView.setSelection(msgList.size());
}else {
Toast.makeText(MainActivity.this, "input can't be empty", Toast.LENGTH_SHORT).show();
}
input.setText("");
}
});
}
private void initMsg() {
Msg msg;
msg = new Msg("Hi, boy", Msg.MSG_RECEIVE);
msgList.add(msg);
msg = new Msg("Hi, girl", Msg.MSG_SEND);
msgList.add(msg);
msg = new Msg("what's up", Msg.MSG_RECEIVE);
msgList.add(msg);
}
}
来源:https://blog.csdn.net/liao_x/article/details/8259604
0
投稿
猜你喜欢
- 如果使用了反向代理软件,将http://192.168.1.110:2046/ 的URL反向代理为http://www.xxx.com/ 的
- 最近项目上要实现语音搜索功能,界面样式要模仿一下UC浏览器的样式,UC浏览器中有一个控件,会随着声音大小浮动,然后寻思偷个懒,百
- 1. 线程转储简介线程转储(Thread Dump)就是JVM中所有线程状态信息的一次快照。线程转储一般使用文本格式, 可以将其保存到文本文
- C# 利用代理爬虫网页实现代码:// yanggang@mimvp.com// http://proxy.mimvp.com// 2015-
- 进行数据源或者 FTP 服务器等资源配置时,我们可以将这些配置信息放到一个独立的外部属性文件中,并在 Spring 配置文件中通过形如 ${
- 整理文档,搜刮出一个Java实现身份证号码验证源码示例代码,稍微整理精简一下做下分享。package xxx;/** * Created b
- HashMap相同key累加valueimport java.util.HashMap;import java.util.Map;publi
- mybatis if test判断入参的值1.第一种判断方式<if test=' requisition != null an
- 本文实例讲述了Java模拟计算机的整数乘积计算功能。分享给大家供大家参考,具体如下:计算机计算整数乘积的原理:实现代码:package ma
- 一. 关键字Java中的关键字是由特定的单词组成,单词全为小写字母,每个都有特殊的含义,其实Java关键字也就那几十个,这个不需要背,以后都
- 关于这个系列里的问题,每个学Java的人都应该搞懂。当然,若是仅仅学Java玩玩就无所谓了。若是你以为本人现已逾越初学者了,却不很懂这些问题
- 最近在用SpringMvc写项目的时候,遇到一个问题,就是方法的鉴权问题,这个问题弄了一天了终于解决了,下面看下解决方法项目需求:需要鉴权的
- 1. ObsoleteAttributeObsoleteAttribute 适用于除组件、模块、参数和返回值以外的所有程序元素。将元素标记为
- 先给大家展示下效果图,如果感觉不错,请参考实现思路详解Android开发中关于短息验证码的设计层出不穷,越来越多的应用为了更好的提高软件的安
- 1.需求WPF本身没有直接把点集合绘制成曲线的函数。可以通过贝塞尔曲线函数来绘制。贝兹曲线由线段与节点组成,节点是可拖动的支点,线段像可伸缩
- 日常对于金额计算,应该都是用的BigDecimal,可是苦于没有好的工具类方法,现在贡献一个我正在用的对于数字计算的工具类,项目中就是用的这
- 最近项目里面做了一个定时器,结果报错这个。网上的原因大多说是什么版本问题。我记录下我的问题所在。由于项目启动在局域网,不能访问互联网。打出来
- RelativePanel是在Windows 10 UWP程序中引入的一种新的布局面板,它是通过附加属性设置元素间的位置关系来对实现布局的。
- 1、概述首先和大家一起回顾一下Java 消息服务,在我之前的博客《Java消息队列-JMS概述》中,我为大家分析了:1.消息服务:一个中间件
- 单链表:每个数据是以节点的形式存在的每个节点分为数据域和指针域数据域中保存该节点的数据指针域中保存指向下一个节点的指针实现思路:节点类Sin