4.TableLayout、回调接口


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical" ><!-- android:shrinkColumns="0" 压缩第0列,当第0列的内容过多时,不至于将其他列的内容,挤出屏幕 --><TableLayoutandroid:id="@+id/tl_receive"android:layout_width="match_parent"android:shrinkColumns="0"android:layout_height="wrap_content" ><TableRow android:layout_height="wrap_content" ><TextViewandroid:id="@+id/tv_msg_receive"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/receive_msg_bubble"android:text="asdfasdfasdfasdfasdfsdfadfadfasdfasfsdfadffad" /><TextViewandroid:id="@+id/tv_date_receive"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="bottom"android:text="2014/10/10" /></TableRow></TableLayout><!-- android:shrinkColumns="1" 压缩第0列,当第0列的内容过多时,不至于将其他列的内容,挤出屏幕 --><TableLayoutandroid:id="@+id/tl_send"android:layout_width="match_parent"android:shrinkColumns="1"android:layout_height="wrap_content" ><TableRow android:layout_height="wrap_content"android:gravity="right"><TextViewandroid:id="@+id/tv_date_send"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="bottom"android:text="2014/10/10" /><TextViewandroid:id="@+id/tv_msg_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/send_msg_bubble"android:text="sfsdfadadsadsfasdfadfffad" /></TableRow></TableLayout><!-- 另一种实现 方式 --><!-- <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="right"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="bottom"android:text="2014/10/10" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="2"android:background="@drawable/send_msg_bubble"android:text="asdfdf" /></LinearLayout></LinearLayout> --></LinearLayout>
public class ConversationDetail extends Activity implements OnClickListener{/***联系人的电话号码*/private String address;private Context ctx;private ListView listView;private EditText inputMsg;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ctx = this;address = getIntent().getStringExtra("address");if(address == null){throw new RuntimeException("联系人是空,我不知道显示哪个会话记录");}setContentView(R.layout.activity_conversation_detail);init();listView = (ListView) findViewById(R.id.lv_conversation_detail);adapter = new MyListAdapter(this, null);listView.setAdapter(adapter);//设置listView条目之间的分隔线为null ,即,不要分隔线listView.setDivider(null);prepareData();}private void prepareData() {MyQueryHandler
public class MyQueryHandler extends AsyncQueryHandler{public MyQueryHandler(ContentResolver cr) {super(cr);}@Overrideprotected void onQueryComplete(int token, Object cookie, Cursor cursor) {System.out.println("onQueryComplete : token:"+token);System.out.println("onQueryComplete : cookie:"+cookie);Tools.printCursor(cursor);if(cookie!=null && cookie instanceof CursorAdapter){CursorAdapter adapter = (CursorAdapter) cookie;// 给adapter 设置新的cursoradapter.changeCursor(cursor);}if(cursorChangedListener!=null){cursorChangedListener.onCursorChanged(token, cookie, cursor);}}public IOnCursorChangedListener getCursorChangedListener() {return cursorChangedListener;}public void setOnCursorChangedListener(IOnCursorChangedListener cursorChangedListener) {this.cursorChangedListener = cursorChangedListener;}private IOnCursorChangedListener cursorChangedListener;/*** 声明,cursor改变时的监听接口* @author Administrator**/public interface IOnCursorChangedListener{void onCursorChanged(int token, Object cookie, Cursor cursor);}}//回调接口写法:这样就把adapter有回传回来了MyQueryHandler myQueryHandler = new MyQueryHandler(getContentResolver());myQueryHandler.setOnCursorChangedListener(new MyQueryHandler.IOnCursorChangedListener() {@Override/*** 当adapter 获得 cursor 的时候,回调此方法*/public void onCursorChanged(int token, Object cookie, Cursor cursor) {// 让listview 显示最后一行listView.setSelection(adapter.getCount()-1);}});myQueryHandler.startQuery(99, adapter, MyConstants.URI_SMS, projection, " address="+address, null, " date ");}/*** 显示会话详情,所需要的列*/private String[] projection={"_id","body","date","type"};/*** 短信内容所在列的索引值 为 1*/private final int INDEX_BODY = 1;private final int INDEX_DATE = 2;private final int INDEX_TYPE = 3;private void init() {TextView title = (TextView) findViewById(R.id.tv_title_conversation_detail);String name = Tools.findNameByNumber(ctx, address);if(name !=null){ // 有此联系人title.setText(name);}else{ // 无此联系人title.setText(address);}findViewById(R.id.btn_back).setOnClickListener(this);findViewById(R.id.btn_ok).setOnClickListener(this);inputMsg = (EditText) findViewById(R.id.et_input_msg_conversation_detail);}@Override/*** 响应按钮的点击事件*/public void onClick(View v) {switch (v.getId()) {case R.id.btn_back: // 后退按钮finish();break;case R.id.btn_ok: // 确定按钮//先判断输入的是否有内容,//如果有内容的话,就将内容以短信的形式发送出去,String msg = inputMsg.getText().toString();if(TextUtils.isEmpty(msg.trim())){Toast.makeText(ctx, "请输入短信内容", 0).show();return ;}// 发送短信Tools.sendMessage(ctx,msg,address);//清空输入框inputMsg.setText("");// 隐藏输入法键盘InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);// 隐藏输入法的 APIimm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);break;}}private MyListAdapter adapter;private class MyListAdapter extends CursorAdapter{public MyListAdapter(Context context, Cursor c) {super(context, c);}@Override/*** 当内容发生改变的时候,回调此方法*/protected void onContentChanged() {// super 里面,做了重新查询的动作super.onContentChanged();// 让listView 显示最后一行listView.setSelection(getCount()-1);}@Overridepublic View newView(Context context, Cursor cursor, ViewGroup parent) {View view =View.inflate(ctx, R.layout.list_item_conversation_detail, null);ViewHolder vh = new ViewHolder();vh.tlReceive = (TableLayout) view.findViewById(R.id.tl_receive);vh.msgReceive = (TextView) view.findViewById(R.id.tv_msg_receive);vh.dateReceive = (TextView) view.findViewById(R.id.tv_date_receive);vh.tlSend = (TableLayout) view.findViewById(R.id.tl_send);vh.msgSend = (TextView) view.findViewById(R.id.tv_msg_send);vh.dateSend = (TextView) view.findViewById(R.id.tv_date_send);view.setTag(vh);return view;}@Overridepublic void bindView(View view, Context context, Cursor cursor) {ViewHolder vh = (ViewHolder) view.getTag();// 给listView条目设置内容int type = cursor.getInt(INDEX_TYPE);// 获得短信类型String text = cursor.getString(INDEX_BODY);//获得短信内容long when = cursor.getLong(INDEX_DATE);// 获得日期String dateStr = DateFormat.getDateFormat(ctx).format(when);if(type == MyConstants.TYPE_RECEIVE){ // 接收到的短信vh.tlReceive.setVisibility(View.VISIBLE);vh.tlSend.setVisibility(View.GONE);//设置短信内容vh.msgReceive.setText(text);//设置日期vh.dateReceive.setText(dateStr);}else{vh.tlReceive.setVisibility(View.GONE);vh.tlSend.setVisibility(View.VISIBLE);//设置短信内容vh.msgSend.setText(text);vh.dateSend.setText(dateStr);}}}private class ViewHolder {public TableLayout tlReceive;public TextView msgReceive;public TextView dateReceive;public TableLayout tlSend;public TextView msgSend;public TextView dateSend;}}
4.TableLayout、回调接口的更多相关文章
- Android回调接口的写法
方法一: 定义一个接口,里面写想要对外提供的方法,在逻辑层方法的参数里传递进去,让在需要的时候调接口里的方法. 实例一: public class SmsUtils { public interfac ...
- Android中回调接口的使用
MainActivity如下: package cn.testcallback; import android.os.Bundle; import android.app.Activity; /** ...
- 手势识别官方教程(2)识别常见手势用GestureDetector+手势回调接口/手势抽象类
简介 GestureDetector识别手势. GestureDetector.OnGestureListener是识别手势后的回调接口.GestureDetector.SimpleOnGesture ...
- 使用回调接口实现ActiveX控件和它的容器程序的通讯
本文阅读基础:有一定的C++基础知识(了解继承.回调函数),对MFC的消息机制有一定了解,对COM的基础知识有一定了解,对ActiveX控件有一定了解. 一. 前言 ActiveX控件和它的容器程序如 ...
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
博客地址 : http://blog.csdn.net/shulianghan/article/details/41520569 代码下载 : -- GitHub : https://github.c ...
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- C#POST 支付宝/微信回调接口
一般支付宝/微信的回调接口都会返回xml格式,下面是调用类似这种接口的办法: public async Task<string> GetData() { string requestUrl ...
- vue回调接口
1.微博回调接口 1.1oauth/urls.py 中添加路由 urlpatterns = [ path('weibo/callback/', views.OauthWeiboCallback.as_ ...
- Android中添加监听回调接口的方法
在Android中,我们经常会添加一些监听回调的接口供别的类来回调,比如自定义一个PopupWindow,需要让new这个PopupWindow的Activity来监听PopupWindow中的一些组 ...
随机推荐
- functools 之 partial(偏函数)
当函数的参数个数太多,需要简化时,使用functools.partial可以创建一个新的函数,这个新函数可以固定住原函数的部分参数,从而在调用时更简单.当然,decorator(装饰器) 也可以实现, ...
- 机器学习入门-BP神经网络模型及梯度下降法-2017年9月5日14:58:16
BP(Back Propagation)网络是1985年由Rumelhart和McCelland为首的科学家小组提出,是一种按误差逆传播算法训练的多层前馈网络,是目前应用最广泛的神经网络模型之一. B ...
- Android设备直接运行java项目?还杀不死?
思路:拿到dex可执行文件,使用android执行 使用idea创建java类库,写相关逻辑代码 使用idea导出该类库jar包 使用android dx工具 将jar文件转换为dex可执行文件 dx ...
- Redis master/slave,sentinel,Cluster简单总结
现在互联网项目中大量使用了redis,本文著主要分析下redis 单点,master/slave,sentinel模式.cluster的一些特点. 一.单节点模式 单节点实例还是比较简单的,平时做个测 ...
- Saliency Detection: A Spectral Residual Approach
Saliency Detection: A Spectral Residual Approach 题目:Saliency Detection: A Spectral Residual Approach ...
- 生信分析常用脚本(二)--SOAPdenovo
1.SOAPDenovo配置文件示例 软件下载安装和使用:http://soap.genomics.org.cn/soapdenovo.html asm.cfg #maximal read lengt ...
- TCP/IP数据加密传输及CA简述
TCP/IP跨主机之间的通信数据封装发送的都是明文数据,现代通讯中会有安全问题. 三个安全问题 如:A发送消息给B的三个安全问题机密性:明文传输如:ftp,http,smtp,telnet等完整性:数 ...
- 初学html,任务1:一个简单html页面,要求:内容页面装一篇文章 用html来分段
这是主要内容部分,用html实现版块分布. 接下来是样式部分. 让页面所有元素的padding和margin都设置为0 : 否则加入一张大的覆盖的背景图片后,会由于浏览器的缘故,图片周边有白边: 设置 ...
- Python之路(第三十一篇) 网络编程:简单的tcp套接字通信、粘包现象
一.简单的tcp套接字通信 套接字通信的一般流程 服务端 server = socket() #创建服务器套接字 server.bind() #把地址绑定到套接字,网络地址加端口 server.lis ...
- spring事务相关
在 SPRING 中一共定义了六种事务传播属性 PROPAGATION_REQUIRED -- 支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. PROPAGATION_SUPPOR ...