QQl聊天消息
2015-12-08 20:21
232 查看
Activity:
package com.zzw.qqchat; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private final int VIEW_TYPE = 0x001; private final int MESSAGE = 0x002; private final int VIEW_TYPE_LEFT = -1; private final int VIEW_TYPE_RIGHT = -2; private ArrayList<HashMap<Integer, Object>> items; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView listView = (ListView) findViewById(R.id.list); items = new ArrayList<HashMap<Integer, Object>>(); addData(); adapter = new MyAdapter(this, -1); listView.setAdapter(adapter); final EditText et = (EditText) findViewById(R.id.msgEditText); findViewById(R.id.msgSend).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String msg = et.getText() + ""; HashMap<Integer, Object> map = new HashMap<Integer, Object>(); map.put(VIEW_TYPE, VIEW_TYPE_RIGHT); map.put(MESSAGE, msg); items.add(map); // item数据变化时通知adapter刷新及时改变view adapter.notifyDataSetChanged(); // 刷新后清空输入框 et.setText(""); // 输入框发送消息后将ListView滚动到底部 listView.setSelection(ListView.FOCUS_DOWN); } }); } private void addData() { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { HashMap<Integer, Object> map = new HashMap<Integer, Object>(); map.put(VIEW_TYPE, VIEW_TYPE_LEFT); map.put(MESSAGE, "对方说的消息:" + i); items.add(map); } else { HashMap<Integer, Object> map = new HashMap<Integer, Object>(); map.put(VIEW_TYPE, VIEW_TYPE_RIGHT); map.put(MESSAGE, "我说的消息:" + i); items.add(map); } } } private class MyAdapter extends ArrayAdapter { private LayoutInflater inflater; public MyAdapter(Context context, int resource) { super(context, resource); inflater = LayoutInflater.from(context); } @Override public int getCount() { return items.size(); } @Override public String getItem(int position) { String message = items.get(position).get(MESSAGE) + ""; return message; } @Override public View getView(int position, View convertView, ViewGroup parent) { int type = getItemViewType(position); String message = getItem(position); switch (type) { case VIEW_TYPE_LEFT: if (convertView == null) { convertView = inflater.inflate(R.layout.left, null); } TextView textViewLeft = (TextView) convertView.findViewById(R.id.textView); textViewLeft.setText(message); break; case VIEW_TYPE_RIGHT: if (convertView == null) { convertView = inflater.inflate(R.layout.right, null); } TextView textViewRight = (TextView) convertView.findViewById(R.id.textView); textViewRight.setText(message); break; } return convertView; } @Override public int getItemViewType(int position) { HashMap map = items.get(position); int type = (Integer) map.get(VIEW_TYPE); return type; } @Override public int getViewTypeCount() { return 2; } } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/commentLinearLayout" android:layout_alignParentTop="true" android:divider="@android:color/transparent" android:dividerHeight="15dip" android:scrollbars="none" /> <LinearLayout android:id="@+id/commentLinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#e0e0e0" android:orientation="horizontal" > <EditText android:id="@+id/msgEditText" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="8" android:hint="发送消息" /> <Button android:id="@+id/msgSend" style="?android:attr/buttonStyleSmall" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="2" android:text="发送" /> </LinearLayout> </RelativeLayout> activity_main
left.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:singleLine="false" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/imageView" android:background="#ff5252" android:text="left" /> </RelativeLayout> left
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/imageView" android:background="#2196f3" android:singleLine="false" android:text="right" /> </RelativeLayout> right
相关文章推荐
- LeetCode OJ:Number of Islands(孤岛计数)
- 数组链表下标指针map list
- Centos root权限的变化
- Codeforces Round #105 (Div. 2) 148C Terse princess(脑洞)
- 一些算法书籍
- fs_initcall、early_initcall、__init宏
- [LeetCode]Decode Ways
- 孤华暗香的Python Spider,Updating
- 把图片变为圆形的方法(QQ头像)
- php 给图片添加水印
- php源码分析之base64_encode函数
- 双系统给ubuntu增加分区
- A计划(bfs)
- Launcher3源码分析 — 数据加载过程
- 关于“静态变量不能跨函数使用”的一个疑问
- greendao->报错:java.lang.NoClassDefFoundError: database.dao.DaoMaster
- ArrayAdapter的简单使用
- 使用 MNIST 图像识别数据集
- 20151208--批处理
- js中innerText,innerHTML的用法