主要布局文件:
mian.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <GridView android:id="@+id/myGrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:columnWidth="60dp" android:stretchMode="columnWidth" android:gravity="center" /> </LinearLayout>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/GridItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:background="#000080"> <imageview android:id="@+id/grid_item_image" android:layout_width="wrap_content" android:layout_height="wrap_content"> </imageview> <textview android:id="@+id/grid_item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:gravity="center_horizontal" android:textColor="#000000"> </textview> </linearlayout>
AppsDB.java
package gridview_3.com; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class AppsDB extends SQLiteOpenHelper { private final static String DATABASE_NAME = "Apps.db"; private final static int DATABASE_VERSION = 1; private final static String TABLE_NAME = "apps_table"; public final static String APP_ID = "app_id"; public final static String APP_PACKAGENAME = "app_packagename"; public final static String APP_FLAG = "app_flag"; public AppsDB(Context context) { // TODO Auto-generated constructor stub super(context, DATABASE_NAME, null, DATABASE_VERSION); } // 创建table @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE " + TABLE_NAME + " (" + APP_ID + " INTEGER primary key autoincrement, " + APP_PACKAGENAME + " text, " + APP_FLAG + " text);"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; db.execSQL(sql); onCreate(db); } public Cursor select() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db .query(TABLE_NAME, null, null, null, null, null, null); return cursor; } // 增加操作 public long insert(String packagename, String flag) { SQLiteDatabase db = this.getWritableDatabase(); /* ContentValues */ ContentValues cv = new ContentValues(); cv.put(APP_PACKAGENAME, packagename); cv.put(APP_FLAG, flag); long row = db.insert(TABLE_NAME, null, cv); return row; } // 删除操作 public void delete(String packagename) { SQLiteDatabase db = this.getWritableDatabase(); String where = APP_PACKAGENAME + " = ?"; String[] whereValue = { packagename }; db.delete(TABLE_NAME, where, whereValue); } // 修改操作 public int update(String packagename, String flag) { SQLiteDatabase db = this.getWritableDatabase(); String where = APP_PACKAGENAME + " = ?"; String[] whereValue = { packagename }; ContentValues cv = new ContentValues(); // cv.put(APP_PACKAGENAME, packagename); cv.put(APP_FLAG, flag); int mLine = db.update(TABLE_NAME, cv, where, whereValue); return mLine; } }
package gridview_3.com; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Set; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.pm.ResolveInfo; import android.database.Cursor; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.ImageView.ScaleType; public class GridViewTest extends Activity { private GridView myGrid; private List<ResolveInfo> mApps; @SuppressWarnings({ "unchecked", "rawtypes" }) private List<ResolveInfo> mDisApps = new ArrayList(); private IconsAdapter iconsAdapter = new IconsAdapter(mDisApps); private AppsDB mAppsDB; private Cursor mCursor; @SuppressWarnings("rawtypes") HashMap hm = new HashMap(); @SuppressWarnings("unchecked") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); mAppsDB = new AppsDB(this); mCursor = mAppsDB.select(); loadApps(); getDisApps(); setContentView(R.layout.main); // mAppsDB = new AppsDB(this); // mCursor = mAppsDB.select(); myGrid = (GridView) findViewById(R.id.myGrid); // iconsAdapter = new IconsAdapter(mDisApps); myGrid.setAdapter(iconsAdapter); myGrid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) { try { Intent it = new Intent(Intent.ACTION_VIEW); ComponentName componentName = new ComponentName(mDisApps .get(index).activityInfo.packageName, mDisApps .get(index).activityInfo.name); it.setComponent(componentName); startActivity(it); } catch (Exception e) { e.printStackTrace(); } /* * mApps.remove(index); * iconsAdapter.notifyDataSetChanged(); */ } }); /** 长按改变图标的透明度和标志位 */ myGrid.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int index, long arg3) { Object mPackageName = arg1.getTag(); boolean mFlag = Boolean.parseBoolean(hm.get(mPackageName).toString()); if (mFlag) { // mDisApps.get(index).activityInfo.loadIcon( // getPackageManager()).setAlpha(100); arg1.setBackgroundColor(Color.RED); hm.put(mPackageName, false); Log.v("长按", "隐藏"); Log.v("Flag", hm.get(mPackageName).toString()); } else { // mDisApps.get(index).activityInfo.loadIcon( // getPackageManager()).setAlpha(255); arg1.setBackgroundColor(Color.BLACK); hm.put(mPackageName, true); Log.v("长按", "显示"); Log.v("Flag", hm.get(mPackageName).toString()); } return true; } }); } @Override public void onStop() { super.onStop(); Log.v("onStop()", "~~~onStop"); @SuppressWarnings("rawtypes") Set mset = hm.keySet(); /* 保存用户当前的设置 */ for (Object mPackageName : mset) { /* 果然更新的行数为0,表示用新装载的应用,这要调用insert()方法 */ int mLineNum = mAppsDB.update(mPackageName.toString(), hm.get(mPackageName).toString()); mCursor.requery(); if (0 == mLineNum) { mAppsDB.insert(mPackageName.toString(), hm.get(mPackageName) .toString()); mCursor.requery(); } } Log.v("onStop()", "~~~onStop~~~~"); } /** 设置两个按钮,“显示所有”和“显示/隐藏” */ public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, R.string.all); menu.add(0, 1, 1, R.string.display_hide); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case 0: iconsAdapter = new IconsAdapter(mApps); myGrid.setAdapter(iconsAdapter); break; case 1: getDisApps(); iconsAdapter = new IconsAdapter(mDisApps); myGrid.setAdapter(iconsAdapter); break; } return true; } @SuppressWarnings("unchecked") public void loadApps() { Intent i = new Intent(Intent.ACTION_MAIN, null); i.addCategory(Intent.CATEGORY_LAUNCHER); if (mApps != null) { mApps.clear(); } mApps = getPackageManager().queryIntentActivities(i, 0); /* 重构HashMap,为每个应用程序设置是否显示的标志:true 为显示,false为隐藏 */ if (hm != null) { hm.clear(); } for (int i1 = 0; i1 < mApps.size(); i1++) { hm.put(mApps.get(i1).activityInfo.packageName, true); } /* 读取用户之前的设置信息 */ @SuppressWarnings("rawtypes") Set ms = hm.keySet(); // if ( !mCursor.isNull(1)) { for (mCursor.moveToFirst(); !(mCursor.isAfterLast()); mCursor .moveToNext()) { if (ms.contains(mCursor.getString(1))) { /* 在hm中存在,这从SQLite中读取并更新hm */ hm.put(mCursor.getString(1), mCursor.getString(2)); } else { /* 不在hm中,说明用户卸载了应用,应该从SQLite中删除该记录 */ mAppsDB.delete(mCursor.getString(1)); mCursor.requery(); } } // } } /** 构建显示图标的程序的List */ public void getDisApps() { if (!mDisApps.isEmpty()) { mDisApps.clear(); } for (int i = 0; i < hm.size(); ++i) { boolean mFlag = Boolean.parseBoolean(hm.get( mApps.get(i).activityInfo.packageName).toString()); if (mFlag) { mDisApps.add(mApps.get(i)); } } Log.v("getDisApps()", "msg"); } public class IconsAdapter extends BaseAdapter { private List<ResolveInfo> list; public IconsAdapter(List<ResolveInfo> list) { super(); this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } // // 通过这个api来动态通知adapter需要更新界面 // @Override // public void notifyDataSetChanged() { // super.notifyDataSetChanged(); // } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView mImageView; if (null == convertView) { mImageView = new ImageView(GridViewTest.this); mImageView.setScaleType(ScaleType.FIT_CENTER); mImageView.setLayoutParams(new GridView.LayoutParams(50, 50)); } else { mImageView = (ImageView) convertView; } ResolveInfo info = list.get(position); mImageView.setImageDrawable(info.activityInfo .loadIcon(getPackageManager())); Object mPackageName = info.activityInfo.packageName; boolean Flag = Boolean.parseBoolean(hm.get(mPackageName).toString()); /* 区别隐藏的图标在显示所用中的不同状态 */ if (Flag) { mImageView.setBackgroundColor(Color.BLACK); } else { mImageView.setBackgroundColor(Color.RED); } /* 给在GridView中的显示的每个View调用一个 setTag()方法,用来确定一个应用程序 */ mImageView.setTag(mPackageName); return mImageView; } } }