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


猜你喜欢
- 职责链模式简介及UML职责链也叫责任链,他是一种行为型模式,它为请求创建了一个接收请求者对象的链,并将请求沿着这条链传递到目标对象去处理。该
- 这篇文章主要介绍了Spring Cloud Hystrix异常处理方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 打包与运行在项目开发完成之后,可以直接用IDEA将其打包成JAR包运行,也可以打包成WAR包运行以便在多服务器、多配置环境下运行。双击cle
- idea统计代码行数可以用到插件:Statistic。步骤:File→Settings进入Plugins点击Marketplace搜索Sta
- 一、图示spring再简化:SpringBoot-jar:内嵌tomacat;微服务架构!二、springboot是什么spring是一个为
- 在上篇文章给大家介绍了使用XSD校验Mybatis的SqlMapper配置文件的方法(1),需要的朋友可以参考下。编写好XSD文件,然后来看
- 本文主要讲解MVP开发模式以及具体实例。一、简介MVP(Model View Presenter)模式是著名的MVC(Model View
- 一、尽量不要使用e.printStackTrace(),而是使用log打印。反例:try{ // do what you want }cat
- 感觉很久不写模拟器代码了,昨天调试的时候碰了点壁,记录下来,避免大家再跟我犯同样的错误。加入Javascript脚本的地方:HtmlElem
- 1)new 运算符:用于创建对象和调用构造函数。这种大家都比较熟悉,没什么好说的了。2)new 修饰符:在用作修饰符时,new 关键字可以显
- 验证码逻辑以前在项目中也做过验证码,生成验证码的代码网上有很多,也有一些第三方的jar包也可以生成漂亮的验证码。验证码逻辑很简单,就是在登录
- 上一小节简单分析了directArena内存分配大概流程 ,知道其先命中缓存, 如果命中不到, 则区分配一款连续内存, 这一小节带
- 在 Android 加载图片一般使用 ImageView,这里简单记录一下这个控件的使用方法。最简单就是在 xml 里直接使用 ImageV
- 目录问题产生如何解决总结问题产生默认生成的flutter工程,在启动时候会白屏,设备性能越差,白屏时间越长。原生Android开发也会遇到类
- 存首先初始化private SP sp;sp = new SP( context );存入数据第一个参数为上下文,第二个参数为key,第三个
- SpringBoot的具体介绍可以参看其他网上介绍,这里就不多说了,就这几天的学习,个人理解,简而言之: (1)它是Spring的
- 在项目中,时常会有异步调用的需求web.xml配置<servlet> <description>spri
- 通过Canvas的平移与旋转简化绘图逻辑是一个非常有用的技巧,下面的时钟view就是利用这个方法完成的,省去了使用三角函数计算坐标的麻烦。p
- 这篇文章主要介绍了Java数组索引异常产生及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 一、Android Color设置1、在xml文件中想设置颜色直接设置background的属性或者其他的color属性。随便设置一个颜色如