Android记录3--ExpandableListView使用+获取SIM卡状态信息
Android记录3--ExpandableListView使用+获取SIM卡状态信息
2013年8月9日Android记录
ExpandableListView是一个可以实现下拉列表的控件,大家可能都用过QQ,QQ中的好友列表就是用ExpandableListView实现的,不过它是自定义的适配器。本篇博客除了要介绍ExpandableListView的使用,还整合了获取SIM的状态,这个很简单也就是获取系统服务,再调用相应的方法就可以实现,在这里只是记录一下。
本票博客要实现效果图如下:
除了子列表不怎么好看之外,组列表还是蛮好看的,这里只是为了演示,子列表就没做特别的处理。
布局文件:
/SIM_Card_Demo/res/layout/inbox.xml
<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:background="@drawable/bg"
android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/banner_bg" > <ImageButton
android:id="@+id/imgBtn_Sort"
android:layout_width="@dimen/banner_btn_width"
android:layout_height="wrap_content"
android:background="@drawable/banner_btn_bg_selector"
android:src="@drawable/sort" /> <ImageView
android:id="@+id/img_DividerSort"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_toRightOf="@+id/imgBtn_Sort"
android:src="@drawable/banner_divider" /> <ImageView
android:id="@+id/img_DividerSearch"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_toLeftOf="@+id/imgBtn_Search"
android:src="@drawable/banner_divider" /> <LinearLayout
android:id="@+id/linear_Title"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_centerHorizontal="true"
android:gravity="center_vertical" > <TextView
android:id="@+id/text_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inbox"
android:textColor="@color/white"
android:textSize="22sp"
android:textStyle="bold" /> <TextView
android:id="@+id/text_MailCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(0/0)"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout> <RelativeLayout
android:layout_width="40dp"
android:layout_height="42dp"
android:layout_alignParentRight="true"
android:layout_marginRight="54dp" > <ImageView
android:id="@+id/img_Synchronizing"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_centerInParent="true"
android:contentDescription="@string/empty"
android:scaleType="fitXY"
android:src="@drawable/synchronizing" />
</RelativeLayout> <ImageButton
android:id="@+id/imgBtn_Search"
android:layout_width="@dimen/banner_btn_width"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/banner_btn_bg_selector"
android:contentDescription="@string/search"
android:src="@drawable/search" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" > <ExpandableListView
android:id="@+id/list_Inbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@color/transparent"
android:childDivider="@color/transparent"
android:divider="@drawable/list_divider"
android:fastScrollEnabled="true"
android:groupIndicator="@color/transparent"
android:listSelector="@color/transparent"
android:scrollbars="none" /> </RelativeLayout> </LinearLayout>
组列表项
/SIM_Card_Demo/res/layout/group_item.xml
<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:background="@drawable/bg"
android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/banner_bg" > <ImageButton
android:id="@+id/imgBtn_Sort"
android:layout_width="@dimen/banner_btn_width"
android:layout_height="wrap_content"
android:background="@drawable/banner_btn_bg_selector"
android:src="@drawable/sort" /> <ImageView
android:id="@+id/img_DividerSort"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_toRightOf="@+id/imgBtn_Sort"
android:src="@drawable/banner_divider" /> <ImageView
android:id="@+id/img_DividerSearch"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_toLeftOf="@+id/imgBtn_Search"
android:src="@drawable/banner_divider" /> <LinearLayout
android:id="@+id/linear_Title"
android:layout_width="wrap_content"
android:layout_height="42dp"
android:layout_centerHorizontal="true"
android:gravity="center_vertical" > <TextView
android:id="@+id/text_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/inbox"
android:textColor="@color/white"
android:textSize="22sp"
android:textStyle="bold" /> <TextView
android:id="@+id/text_MailCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(0/0)"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold" />
</LinearLayout> <RelativeLayout
android:layout_width="40dp"
android:layout_height="42dp"
android:layout_alignParentRight="true"
android:layout_marginRight="54dp" > <ImageView
android:id="@+id/img_Synchronizing"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_centerInParent="true"
android:contentDescription="@string/empty"
android:scaleType="fitXY"
android:src="@drawable/synchronizing" />
</RelativeLayout> <ImageButton
android:id="@+id/imgBtn_Search"
android:layout_width="@dimen/banner_btn_width"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/banner_btn_bg_selector"
android:contentDescription="@string/search"
android:src="@drawable/search" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" > <ExpandableListView
android:id="@+id/list_Inbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@color/transparent"
android:childDivider="@color/transparent"
android:divider="@drawable/list_divider"
android:fastScrollEnabled="true"
android:groupIndicator="@color/transparent"
android:listSelector="@color/transparent"
android:scrollbars="none" /> </RelativeLayout> </LinearLayout>
子列表项
/SIM_Card_Demo/res/layout/child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/item_bg"
> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:text="SIM卡状态:" /> <TextView
android:id="@+id/tv_sim_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff00ff00"
android:text="@string/app_name" /> </LinearLayout>
自定义适配器(重要)
/SIM_Card_Demo/src/com/wwj/sim/demo/InboxListAdapter.java
package com.wwj.sim.demo; import java.util.List;
import java.util.Map; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView; /**
* 自定义下列列表适配器
* @author wwj
* 注:要继承BaseExpandableListAdpater
*
*/
public class InboxListAdapter extends BaseExpandableListAdapter {
private Inbox inbox;
private ExpandableListView list_Inbox;
private List<Map<String, Object>> groupList;
private List<Map<String, String>> childList; private LayoutInflater inflater; /**
* 构造函数
*
* @param inbox
* Activity对象引用
* @param list_Inbox
* 下拉列表对象引用
* @param data
* 数据
*/
public InboxListAdapter(Inbox inbox, ExpandableListView list_Inbox,
List<Map<String, Object>> groupList,
List<Map<String, String>> childList) {
super();
this.inbox = inbox;
this.list_Inbox = list_Inbox;
this.groupList = groupList;
this.childList = childList; inflater = LayoutInflater.from(inbox);
} /**
*
* @author wwj
*
*/
private class GroupViewHolder {
ImageButton imgBtn_Photo;
TextView tv_name;
TextView tv_number;
TextView tv_details;
ImageView img_status; public GroupViewHolder(View convertView) {
imgBtn_Photo = (ImageButton) convertView
.findViewById(R.id.imgBtn_Photo);
img_status = (ImageView) convertView.findViewById(R.id.img_Status);
tv_details = (TextView) convertView.findViewById(R.id.text_Details);
tv_name = (TextView) convertView.findViewById(R.id.text_Name);
tv_number = (TextView) convertView.findViewById(R.id.text_Number); // 这个是防止图片聚焦,造成列表不能点击
imgBtn_Photo.setFocusable(false); } } private class ChildViewHolder {
TextView tv_sim_status; public ChildViewHolder(View converView) {
tv_sim_status = (TextView) converView
.findViewById(R.id.tv_sim_status);
}
} @Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View converView, ViewGroup parent) {
final ChildViewHolder childViewHolder;
if (converView == null) {
converView = inflater.inflate(R.layout.child_item, null);
childViewHolder = new ChildViewHolder(converView);
converView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) converView.getTag();
} childViewHolder.tv_sim_status.setText(childList.get(childPosition)
.get("tv_sim_status"));
return converView;
} @Override
public int getChildrenCount(int groupPosition) {
return childList.size();
} @Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
} @Override
public int getGroupCount() {
return groupList.size();
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final GroupViewHolder groupViewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group_item, null);
groupViewHolder = new GroupViewHolder(convertView);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
} groupViewHolder.img_status.setImageResource(R.drawable.mail_status_new);
groupViewHolder.tv_name.setText(groupList.get(groupPosition)
.get("username").toString());
groupViewHolder.tv_number.setText(groupList.get(groupPosition)
.get("number").toString());
groupViewHolder.tv_details.setText(groupList.get(groupPosition)
.get("detail").toString());
groupViewHolder.imgBtn_Photo
.setImageResource(R.drawable.contact_photo_default);
return convertView;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public boolean isChildSelectable(int arg0, int arg1) {
return false;
} }
/SIM_Card_Demo/src/com/wwj/sim/demo/Inbox.java
package com.wwj.sim.demo; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.ExpandableListView; public class Inbox extends Activity {
public ExpandableListView list_Inbox; //下拉List控件
public InboxListAdapter adapter;
private TelephonyManager telephonyManager; //用来获取电话的一些信息
List<Map<String, Object>> groupList;
List<Map<String, String>> childList;
String sim_status; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox); sim_status = readSIMCard();
getData(); //找到这个控件
list_Inbox = (ExpandableListView) findViewById(R.id.list_Inbox); list_Inbox.setAdapter(new InboxListAdapter(Inbox.this, list_Inbox, groupList, childList)); list_Inbox.setOnGroupExpandListener(new OneListItemExpandListener(list_Inbox)); } void getData() {
groupList = new ArrayList<Map<String,Object>>();
Map<String, Object> groupMap = new HashMap<String, Object>();
groupMap.put("username", "张三");
groupMap.put("number", "44440011");
groupMap.put("detail", "4秒 2013-08-06 13:08");
groupList.add(groupMap); groupMap = new HashMap<String, Object>();
groupMap.put("username", "李四");
groupMap.put("number", "44440012");
groupMap.put("detail", "6秒 2013-08-06 13:09");
groupList.add(groupMap); groupMap = new HashMap<String, Object>();
groupMap.put("username", "王五");
groupMap.put("number", "44440013");
groupMap.put("detail", "5秒 2013-08-06 13:10");
groupList.add(groupMap); childList = new ArrayList<Map<String,String>>();
Map<String, String> childMap1 = new HashMap<String, String>();
childMap1.put("tv_sim_status", sim_status);
childList.add(childMap1);
} public String readSIMCard() {
TelephonyManager tm = (TelephonyManager) this
.getSystemService(TELEPHONY_SERVICE);// 取得相关系统服务
StringBuffer sb = new StringBuffer();
switch (tm.getSimState()) { // getSimState()取得sim的状态 有下面6中状态
case TelephonyManager.SIM_STATE_ABSENT:
sb.append("无卡");
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
sb.append("未知状态");
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
sb.append("需要NetworkPIN解锁");
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
sb.append("需要PIN解锁");
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
sb.append("需要PUK解锁");
break;
case TelephonyManager.SIM_STATE_READY:
sb.append("良好");
break;
} if (tm.getSimSerialNumber() != null) {
sb.append("@" + tm.getSimSerialNumber().toString());
} else {
sb.append("@无法取得SIM卡号");
} if (tm.getSimOperator().equals("")) {
sb.append("@无法取得供货商代码");
} else {
sb.append("@" + tm.getSimOperator().toString());
} if (tm.getSimOperatorName().equals("")) {
sb.append("@无法取得供货商");
} else {
sb.append("@" + tm.getSimOperatorName().toString());
} if (tm.getSimCountryIso().equals("")) {
sb.append("@无法取得国籍");
} else {
sb.append("@" + tm.getSimCountryIso().toString());
} if (tm.getNetworkOperator().equals("")) {
sb.append("@无法取得网络运营商");
} else {
sb.append("@" + tm.getNetworkOperator());
}
if (tm.getNetworkOperatorName().equals("")) {
sb.append("@无法取得网络运营商名称");
} else {
sb.append("@" + tm.getNetworkOperatorName());
}
if (tm.getNetworkType() == 0) {
sb.append("@无法取得网络类型");
} else {
sb.append("@" + tm.getNetworkType());
}
return sb.toString();
} }
点击列表展开的监听器
/SIM_Card_Demo/src/com/wwj/sim/demo/OneListItemExpandListener.java
package com.wwj.sim.demo; import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener; /**
* 自定义列表监听器
* @author wwj
* 实现OnGroupExpandListener接口
*/
public class OneListItemExpandListener implements OnGroupExpandListener {
protected ExpandableListView listView; public OneListItemExpandListener(ExpandableListView listView) {
this.listView = listView;
} @Override
public void onGroupExpand(int groupPosition) {
for(int i = 0, count = listView.getExpandableListAdapter().getGroupCount(); i < count; i++) {
if(groupPosition != i) { //关闭其他
if(listView.isGroupExpanded(groupPosition)) {
listView.collapseGroup(i);
}
} else if(groupPosition == listView.getFirstVisiblePosition()){ // 让第一条可见
// android1.6不支持以下方法
listView.smoothScrollToPosition(groupPosition);
}
}
} }
Android记录3--ExpandableListView使用+获取SIM卡状态信息的更多相关文章
- Android ExpandableListView使用+获取SIM卡状态信息
ExpandableListView 是一个可以实现下拉列表的控件,大家可能都用过QQ,QQ中的好友列表就是用ExpandableListView实现的,不过它是自定义的适配器.本篇 博客除了要介绍E ...
- Android获取SIM卡信息--TelephonyManager
1>获得TelephonyManager TelephonyManager telMgr = (TelephonyManager) getSystemService(TELEPHONY_SER ...
- Android本机号码及Sim卡状态的获取
SIM卡存储的数据可分为四类:第一类是固定存放的数据.这类数据在移动电话机被出售之前由SIM卡中心写入,包括国际移动用户识别号(IMSI).鉴权密钥(KI).鉴权和加密算法等等.第二类是暂时存放的有关 ...
- TelephonyManager类:Android手机及Sim卡状态的获取
TelephonyManager这个类很有用,可以得到很多关于手机和Sim卡的信息. 直接上注释后的代码,请享用 package net.sunniwell.app;import android.ap ...
- MTK Android 读取SIM卡参数,获取sim卡运营商信息
android 获取sim卡运营商信息(转) TelephonyManager tm = (TelephonyManager)Context.getSystemService(Context.TE ...
- android中判断sim卡状态和读取联系人资料的方法
在写程序中,有时候可能需要获取sim卡中的一些联系人资料.在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息. Pho ...
- 十九、android中判断sim卡状态和读取联系人资料的方法
在写程序中,有时候可能需要获取sim卡中的一些联系人资料.在获取sim卡联系人前,我们一般会先判断sim卡状态,找到sim卡后再获取它的资料,如下代码我们可以读取sim卡中的联系人的一些信息. Pho ...
- 获取sim卡序列号
//获取sim卡序列号TelephoneManager TelephonyManager manager = (TelephonyManager)getSystemService(Context.TE ...
- iphone获取sim卡信息
/* iphone获取sim卡信息 1.加入一个Framework(CoreTelephony.framework). 2.引入头文件 #import <CoreTelephony/CTTele ...
随机推荐
- 使用Eclipse运行第一个Go程序
Windows 10家庭中文版,go version go1.11 windows/amd64, Eclipse IDE for C/C++ Developers Photon Release (4. ...
- jQuery-属性操着
jquery属性操作分为属性操作,CSS类操作,HTML代码/文本/值操作 一:属性 使用 说明 例子 attr(name|pro|key,val|fn) 设置或返回被选元素的属性值, # 专门用于做 ...
- 使用TensorFlow给花朵🌺分类
第一步:准备好需要的库 tensorflow-gpu 1.8.0 opencv-python 3.3.1 numpy skimage os pillow 第二步:准备数据集: 链接:http ...
- ThinkPHP中的统计查询方法
• count() 表示查询表中总的记录数 • max() 表示查询某个字段的最大值 • min() 表示查询某个字段的最小值 • avg() 表示查询某个字段的平均值 • sum() 表示求出某个字 ...
- linux系统下创建lvm挂载到指定目录
1 .背景 在企业中有时我们为方便安装软件.数据的管理,需要把安装软件.数据放到固定目录下,磁盘满了方便扩展,这里假如需要一个/data目录存放数据,并单独进行挂载. 2.操作步骤 2.1 划分磁盘 ...
- 桌面图形化安装的CentOS6.7中默认安装的yum不能正常使用
使用rpm -qa|grep yum,可以发现有好多关于yum的安装插件等东西... 从里面将的一些东西删除掉,只留下下面三个即可,其余的全部删除掉rpm -e yum-plugin-security ...
- Going Home
题意:n个人,进n个房子,每走一格花费1美元,每个房子只能进一人,求所有人进房子的最小花费. 就是推箱子 箱子最短行走距离 这题无法用bfs做 ! 用最小花费最大流 通过EK,Dinic,ISAP ...
- word2013 如何设置从第三页开始编码 或 如何设置封面页和正文页页码不连续
首先说明一下 “分节符”作用,它就是用来将整个文档分节的,添加一个分节符,文档就分成1.2两节:添加两个分节符,文档就分成1.2.3节. 当前页面具体是第几节,可以通过点击页眉页脚来查看: 从第三页开 ...
- ld: -pie can only be used when targeting iOS 4.2 or later
ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command failed with e ...
- 【*】单线程的redis为什么吞吐量可以这么大
一.Redis的高并发和快速原因 1.redis是基于内存的,内存的读写速度非常快: 2.redis是单线程的,省去了很多上下文切换线程的时间: 3.redis使用多路复用技术,可以处理并发的连接.非 ...