ArrayAdapter实现仿QQ左右发送消息显示
2015-11-19 12:35
260 查看
效果图
代码
布局文件
代码
package com.xiaoke.arrayadapte; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { // private String[] data; private List<String> list; private EditText et; private ListView listview; private int index = 0; private ArrayAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.lv_id); et = (EditText) findViewById(R.id.et_id); list = new ArrayList<String>(); Button btn; adapter = new PangAdapter(this, -1); listview.setAdapter(adapter); btn = (Button) findViewById(R.id.btn_left_a_id); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 左边发送消息 sendMessege("#"); } }); btn = (Button) findViewById(R.id.btn_right_id); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 右边发送消息 sendMessege("&"); } }); // data = new String[100]; // for (int i = 0; i < 100; i++) { // data[i] = "数据" + i; // } // ArrayAdapter adapter=new ArrayAdapter(this, // android.R.layout.simple_list_item_1, data); } // 更新数据并且更新界面 public void sendMessege(String str) { list.add(et.getText().toString() + str); et.setText(null); // 界面更新 adapter.notifyDataSetChanged(); listview.setSelection(ListView.FOCUS_DOWN); } public class PangAdapter extends ArrayAdapter { private LayoutInflater flater = null; public PangAdapter(Context context, int resource) { super(context, resource); // flater = LayoutInflater.from(context); } // 获取数组的长度 @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public View getView(int position, View convertView, ViewGroup parent) { // 不需多次调用, if (convertView == null) { convertView = flater.inflate(R.layout.test, null); Log.d("创建数据", list.get(position)); } TextView tvLeft = (TextView) convertView .findViewById(R.id.text_left_a1_id); TextView tvRight = (TextView) convertView .findViewById(R.id.text_right_a2_id); ImageView imageLeft = (ImageView) convertView .findViewById(R.id.image_left_a1_id); ImageView imageRight = (ImageView) convertView .findViewById(R.id.image_right_a2_id); // 取出每次输入后最后一个字符 char c = list.get(position).charAt(list.get(position).length() - 1); Log.d(String.valueOf(c), String.valueOf(c) + "1"); // 每次输入的字符串添加到StringBuffer中 StringBuffer strbuffer = new StringBuffer(); strbuffer.append(list.get(position)); // 最后一个字符判断是左边发送还是右边发送消息#代表左边&代表右边 if (String.valueOf(c).equals("&")) { // 去除每次传入的最后一个字符 strbuffer.deleteCharAt(strbuffer.length() - 1); String s = strbuffer.toString(); // 右边字符串设置 tvRight.setText(s); // imageLeft.setImageResource(-1); // 右边字图片设置 imageRight.setImageResource(R.drawable.ic_launcher); // 左边字图片设置 imageLeft.setImageBitmap(null); // 左边字符串设置 tvLeft.setText(""); // 清空缓存数组中的内容 strbuffer.delete(0, strbuffer.length()); Log.d("tvA2", "出现tvA2"); } else if (String.valueOf(c).equals("#")) { // 去除每次传入的最后一个字符 strbuffer.deleteCharAt(strbuffer.length() - 1); String s = strbuffer.toString(); // 左边字符串设置 tvLeft.setText(s); // 右边字符串设置 tvRight.setText(""); // 左边字图片设置 imageLeft.setImageResource(R.drawable.ic_launcher); // imageRight.setImageResource(-1); // 右边字图片设置 imageRight.setImageBitmap(null); // 清空缓存数组中的内容 strbuffer.delete(0, strbuffer.length()); Log.d("tvA1", "出现tvA1"); } return convertView; } } }
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.xiaoke.arrayadapte.MainActivity" > <ListView android:id="@+id/lv_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" > </ListView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn_left_a_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" /> <EditText android:id="@+id/et_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" /> <Button android:id="@+id/btn_right_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" /> </LinearLayout> </LinearLayout>
<?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" > <ImageView android:id="@+id/image_left_a1_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" /> <TextView android:id="@+id/text_left_a1_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@id/image_left_a1_id" android:textColor="@android:color/holo_red_dark" android:textSize="20sp" android:textStyle="bold" /> <ImageView android:id="@+id/image_right_a2_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="20dp" /> <TextView android:id="@+id/text_right_a2_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@id/image_right_a2_id" android:textColor="@android:color/holo_red_dark" android:textSize="20sp" android:textStyle="bold" /> </RelativeLayout>
相关文章推荐
- FFMpeg+SDL多线程播放器
- Zmodem transfer canceled by remote side问题的解决办法!
- oracle
- linux中find批量删除空文件及空文件夹脚本
- 阿里云--域名,主机,备案都配置好了,就是不能访问网站的解决方案
- 单例模式(多线程不安全,序列化不安全,反射不安全实例)
- mesos上安装hadoop过程简介
- Spring_Security_3.0.1中文参考文档 Chapter 8.核心安全过滤器
- [leetcode 223] Rectangle Area
- 配置MySQL远程访问权限
- dom事件
- android 四大组件service 音乐播放器的实现
- QtCreator下复制文件夹的一个BUG
- 运算符
- Centos平台Supervisord全攻略
- 希尔排序c语言版
- HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap
- 输入子系统
- 实验二固件设计报告
- 回归