主题(Theme)
---------------效果图------------
--------------------代码------------------
封装一个AppInfo类
package com.example.app03_allaopp;
import android.graphics.drawable.Drawable;
/**
*应用信息的封装类
- */
public class AppInfo {
private Drawable icon;//应用图标 图片对象
private String appName; //应用名称
private String packageName;//包名
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public AppInfo(Drawable icon, String appName, String packageName) {
super();
this.icon = icon;
this.appName = appName;
this.packageName = packageName;
}
public AppInfo() {
super();
}
}
-------------MainActivity----------------
package com.example.app03_allaopp;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lv_main;//列表控件
private List<AppInfo> data;//数据的集合
private AppAdapter adpater;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_main=(ListView) findViewById(R.id.lv_main);
data=getAllAppInfos();
adpater=new AppAdapter();
//显示列表
lv_main.setAdapter(adpater);
//给ListView设置条目的点击监听
lv_main.setOnItemClickListener(new OnItemClickListener() {
/**
* Parent listView
* view 当前行的Item视图对象
* position 当前行下标
* */
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//提示当前行的应用名称
String appName = data.get(position).getAppName();
//提示
Toast.makeText(MainActivity.this, appName, Toast.LENGTH_SHORT).show();
}
});
//给ListView设置条目的长按监听
lv_main.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
/**
// 删除当前行 数据
// * */
data.remove(position);
//更新列表
adpater.notifyDataSetChanged();//通知适配器更新列表
return true;//事件消费 点击监听不会吐司
}
});
}
/*
* 得到手机中所有应用信息的列表
* AppInfo
* Drawable icon 图片对象
* String appName 应用名称
* String packageName 包名
*/
protected List<AppInfo> getAllAppInfos() {
List<AppInfo> list = new ArrayList<AppInfo>();
// 得到应用的packgeManager
PackageManager packageManager = getPackageManager();
// 创建一个主界面的intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
// 得到包含应用信息的列表
List<ResolveInfo> ResolveInfos = packageManager.queryIntentActivities(
intent, 0);
// 遍历
for (ResolveInfo ri : ResolveInfos) {
// 得到包名
String packageName = ri.activityInfo.packageName;
// 得到图标
Drawable icon = ri.loadIcon(packageManager);
// 得到应用名称
String appName = ri.loadLabel(packageManager).toString();
// 封装应用信息对象
AppInfo appInfo = new AppInfo(icon, appName, packageName);
// 添加到list
list.add(appInfo);
}
return list;
}
//适配器
class AppAdapter extends BaseAdapter{
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override//返回带有数据当前行的Item试图对象
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView==null) {
//1. 如果convertView 为null 加载布局
convertView=View.inflate(MainActivity.this, R.layout.item_main, null);
holder= new ViewHolder();
//2.得到当前行需要更新的子view的对象
holder.imageview=(ImageView) convertView.findViewById(R.id.iv_item_icon);
holder.textview=(TextView) convertView.findViewById(R.id.tv_item_name);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();//如果不为空就取
}
//3.得到当前行的对象
AppInfo info=data.get(position);
//4.给视图绑定数据
holder.imageview.setImageDrawable(info.getIcon());
holder.textview.setText(info.getAppName());
//5.返回convertView
return convertView;
}
class ViewHolder{
ImageView imageview;
TextView textview;
}
}
// public void dialog(View v){
//
// new AlertDialog.Builder(this)
// .setTitle("删除数据")
// .setMessage("要删除数据吗")
// .setPositiveButton("删除", new DialogInterface.OnClickListener() {
//
// @Override
// public void onClick(DialogInterface dialog, int which) {
// /**
// 删除当前行 数据
// * */
// data.remove(position);
// //更新列表
// adpater.notifyDataSetChanged();//通知适配器更新列表
// Toast.makeText(MainActivity.this, "删除数据", Toast.LENGTH_SHORT).show();
// }
// }
//}
}
---------------GridView的应用-------------
-------------效果图---------------------
-----------------代码----------------
package com.example.app04_gridview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
public class MainActivity extends Activity {
private GridView gv_main;
private MainAdapter adapter;
String [] names=new String[]{"手机防盗","通讯卫士","软件管理","流量管理","进程管理","手机杀毒","22","33","44"};
int [] icons=new int[]{R.drawable.widget01,
R.drawable.widget02,R.drawable.widget03,
R.drawable.widget04,R.drawable.widget05,R.drawable.widget06,
R.drawable.widget07,R.drawable.widget08,R.drawable.widget09};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv_main=(GridView) findViewById(R.id.gv_main);
adapter=new MainAdapter(MainActivity.this,names,icons);
gv_main.setAdapter(adapter);
}
}
-------------自定义的封装类MainAdpater-----------
···
package com.example.app04_gridview;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MainAdapter extends BaseAdapter{
private String[] names;
private int[] icons;
private Context context;
public MainAdapter(Context context,String[] names,int[] icons) {
this.context=context;
this.names=names;
this.icons=icons;
}
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int position) {
return names[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolser holder=null;
if (convertView==null) {
convertView=View.inflate(context, R.layout.item_main, null);
holder=new ViewHolser();
holder.imageView = (ImageView) convertView.findViewById(R.id.iv_id1);
holder.textView = (TextView) convertView.findViewById(R.id.tv_id1);
convertView.setTag(holder);
}else{
holder = (ViewHolser) convertView.getTag();
}
holder.imageView.setImageResource(icons[position]);
holder.textView.setText(names[position]);
return convertView;
}
class ViewHolser{
ImageView imageView;
TextView textView;
}
}
···
*************item.xml*****
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:id="@+id/iv_id1"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv_id1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="名称" />
</LinearLayout>
···
--------activity.xml-----------------
···
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gv_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="3"
>
</GridView>
···