Android 復雜Listview的顯示,仿金山手機助手程序卸載界面

仿金山手機助手程序卸載界面,主要使用的知識點有:

1、listview中添加textview、listview排列顯示

2、PopupWindow的使用

3、程序卸載

4、幀佈局的使用

下面是項目中APP管理模塊的代碼:

package com.itheima.mobilesafe;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.StatFs;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itheima.mobilesafe.domain.AppInfo;
import com.itheima.mobilesafe.engine.AppInfoProvider;
import com.itheima.mobilesafe.ui.FocusedTextView;

public class AppManageActivity extends Activity implements OnClickListener{
private static final String TAG = “AppManageActivity”;
private TextView tv_avail_sd;
private TextView tv_avail_rom;
private ListView lv_app_manage;
private TextView tv_state;

private View loading;
// private List appinfos;
private List systemappinfos;
private List userappinfos;

/**
* 當前被點擊的appinfo對象
*/
private AppInfo clickedAppInfo;

PopupWindow popupWindow;

//PopupWindow 上的三個安妮
private LinearLayout ll_uninstall;
private LinearLayout ll_start;
private LinearLayout ll_share;

Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
loading.setVisibility(View.INVISIBLE);
lv_app_manage.setAdapter(new AppManagerBaseAdapter());
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_manage);
tv_avail_sd = (TextView) findViewById(R.id.tv_avail_sd);
tv_avail_rom = (TextView) findViewById(R.id.tv_avail_rom);
tv_state = (TextView) findViewById(R.id.tv_state);
loading = findViewById(R.id.loading);

tv_avail_rom.setText(“內存可用:”
+ getAvailableSpace(Environment.getDataDirectory()
.getAbsolutePath()));
tv_avail_sd.setText(“SD卡可用:”
+ getAvailableSpace(Environment.getExternalStorageDirectory()
.getAbsolutePath()));

// android 1.5 1.6 2.1 /sdcard
// android 2.2 ~4.1 /mnt/sdcard
// android 4.2 /mnt/sdcard01
// /mnt/storage/sdcard01

lv_app_manage = (ListView) findViewById(R.id.lv_app_manage);
// 當listview滑動時
lv_app_manage.setOnScrollListener(new OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
dismissPopupWindow();
//當Item上下滑動的時候,動態設置TextView的值
if (userappinfos != null && systemappinfos != null) {

if (firstVisibleItem > (userappinfos.size() + 1)) {
tv_state.setText(“系統程序個數:” + systemappinfos.size());
tv_state.setBackgroundColor(Color.GRAY);
} else {
tv_state.setText(“用戶程序個數:” + userappinfos.size());
tv_state.setBackgroundColor(Color.GRAY);

}
}
}
});

lv_app_manage.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
if (popupWindow != null) {
popupWindow.dismiss();// 如果有popupWindow存在,則關閉它
} else {
Object object = parent.getItemAtPosition(position);
if (object != null && object instanceof AppInfo) {
clickedAppInfo = (AppInfo) object;
Log.d(TAG, “—>>>:” + clickedAppInfo.getPackName());
// TextView tv = new TextView(getApplicationContext());
// tv.setText(clickedAppInfo.getPackName());
// 加載PopupWindow佈局
View popupview = View.inflate(AppManageActivity.this,
R.layout.pop_app_item, null);

ll_uninstall = (LinearLayout) popupview.findViewById(R.id.ll_uninstall);
ll_start = (LinearLayout) popupview.findViewById(R.id.ll_start);
ll_share = (LinearLayout) popupview.findViewById(R.id.ll_share);
ll_uninstall.setOnClickListener(AppManageActivity.this);
ll_start.setOnClickListener(AppManageActivity.this);
ll_share.setOnClickListener(AppManageActivity.this);

dismissPopupWindow();
popupWindow = new PopupWindow(popupview, -2, -2);
// 下面的一句話是非常重要的 一定要給popwindow設置一個背景顏色,要不然動畫效果是不會出現的
popupWindow.setBackgroundDrawable(new ColorDrawable(
Color.TRANSPARENT));
int[] location = new int[2];
view.getLocationInWindow(location);
popupWindow.showAtLocation(parent, Gravity.TOP
| Gravity.LEFT, 100, location[1]);

// 設置動畫
ScaleAnimation sa = new ScaleAnimation(0.5f, 1.2f,
0.5f, 1.2f, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0.5f);
sa.setDuration(200);//動畫時長
popupview.setAnimation(sa);
}
}

}
});

fillData();
}

/**
* PopupWindow 取消
*/
private void dismissPopupWindow() {
if (popupWindow != null && popupWindow.isShowing()) {
popupWindow.dismiss();// 關閉掉已經存在的popupwindow
popupWindow = null;
}
}

/**
* 填充數據
*/
private void fillData() {
loading.setVisibility(View.VISIBLE);
new Thread() {
public void run() {
List appinfos = AppInfoProvider
.getAppInfos(getApplicationContext());
systemappinfos = new ArrayList();
userappinfos = new ArrayList();
for (AppInfo appinfo : appinfos) {
if (appinfo.isUserApp()) {// 手機內存
userappinfos.add(appinfo);
} else {// 外部存儲
systemappinfos.add(appinfo);
}
}
Log.d(TAG, “獲取到的全部集合大小為:” + appinfos.size());
handler.sendEmptyMessage(0);
};
}.start();
}

/**
* 獲取某個目錄的大小
*
* @param path
* @return
*/
public String getAvailableSpace(String path) {
StatFs statf = new StatFs(path);
long size = statf.getAvailableBlocks() * statf.getBlockSize();
return Formatter.formatFileSize(this, size);
}

static class ViewHolder {
FocusedTextView tv_name;
TextView tv_storage;
ImageView iv_icon;
}

private class AppManagerBaseAdapter extends BaseAdapter {

@Override
public int getCount() {
return systemappinfos.size() + 1 + userappinfos.size() + 1;
}

@Override
public Object getItem(int position) {
AppInfo appinfo;
if (position == 0) {
return null;
} else if (position == userappinfos.size() + 1) {
return null;
} else if (position < userappinfos.size() + 1) {
int newposition = position – 1;
appinfo = userappinfos.get(newposition);
} else {
int newposition = position – 1 – userappinfos.size() – 1;
appinfo = systemappinfos.get(newposition);
}
return appinfo;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
AppInfo appinfos = null;
if (position == 0) {// 第0個位置 ,顯示一個textview的標簽
TextView tv_user = new TextView(AppManageActivity.this);
tv_user.setText(“用戶程序個數:” + userappinfos.size());
tv_user.setBackgroundColor(Color.GRAY);
return tv_user;
} else if (position == userappinfos.size() + 1) {
TextView tv_system = new TextView(AppManageActivity.this);
tv_system.setText(“系統程序個數:” + systemappinfos.size());
tv_system.setBackgroundColor(Color.GRAY);
return tv_system;
} else if (position < (userappinfos.size() + 1)) {
int newpositon = position – 1;
appinfos = userappinfos.get(newpositon);
} else {
int newpositon = position – 1 – userappinfos.size() – 1;
appinfos = systemappinfos.get(newpositon);
}

View view = null;
ViewHolder holder = null;
if (convertView != null && convertView instanceof RelativeLayout) {
view = convertView;
holder = (ViewHolder) view.getTag();
} else {
view = View.inflate(getApplicationContext(),
R.layout.activity_app_manage_item, null);
holder = new ViewHolder();
holder.tv_name = (FocusedTextView) view
.findViewById(R.id.tv_name);
holder.tv_storage = (TextView) view
.findViewById(R.id.tv_storage);
holder.iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
view.setTag(holder);
}
holder.tv_name.setText(appinfos.getAppName());
holder.iv_icon.setImageDrawable(appinfos.getAppIcon());
boolean userapp = appinfos.isUserApp();
Log.d(TAG,
“======================isUserApp:” + appinfos.isUserApp());
if (userapp) {
holder.tv_storage.setText(“手機內存”);
} else {
holder.tv_storage.setText(“外部存儲”);
}
return view;
}

}

@Override
protected void onDestroy() {
super.onDestroy();

dismissPopupWindow();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ll_share:
Log.d(TAG, “—>>>ll_share”);
break;
case R.id.ll_start:
Log.d(TAG, “—>>>ll_start”);
break;
case R.id.ll_uninstall:
unistallApplication();
Log.d(TAG, “—>>>ll_uninstall”);
break;

default:
break;
}
}

/**
* 應用程序卸載
*/
private void unistallApplication(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DELETE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse(“package:” + clickedAppInfo.getPackName()));
startActivityForResult(intent, 0);
}

/**
* 開啟一個應用程序
*/
private void startApplication(){

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 0){
fillData();
}
}
}

//==========================================END====================================

獲取所有的應用程序的信息.:

package com.itheima.mobilesafe.engine;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;

import com.itheima.mobilesafe.domain.AppInfo;

//ActivityManager 進程管理器
//PackageManager 程序管理器

public class AppInfoProvider {

/**
* 獲取所有的應用程序的信息.
* @param context
* @return
*/
public static List getAppInfos(Context context){
PackageManager pm = context.getPackageManager();
List appInfos = new ArrayList();
List packInfos = pm.getInstalledPackages(0);
for(PackageInfo packInfo : packInfos){
AppInfo appInfo = new AppInfo();
String packName = packInfo.packageName;
appInfo.setPackName(packName);
String appName = packInfo.applicationInfo.loadLabel(pm).toString();
appInfo.setAppName(appName);
Drawable appIcon = packInfo.applicationInfo.loadIcon(pm);
appInfo.setAppIcon(appIcon);
int flags = packInfo.applicationInfo.flags;//標識
if((ApplicationInfo.FLAG_SYSTEM & flags) ==0){
//用戶程序
appInfo.setUserApp(true);
}else{
//系統程序
appInfo.setUserApp(false);
}
if((ApplicationInfo.FLAG_EXTERNAL_STORAGE&flags)==0){
//手機內存
appInfo.setInRom(true);
}else{
//外部存儲
appInfo.setInRom(false);
}
appInfos.add(appInfo);
}
return appInfos;
}
}

//==========================================END====================================

下面是需要用到的activity_app_manage.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools=”https://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background=”#FFFFFF”
android:orientation=”vertical” >

<TextView
android:layout_width=”match_parent”
android:layout_height=”50dip”
android:background=”#6666ff00″
android:gravity=”center”
android:text=”程序管理器”
android:textSize=”20sp” />

<RelativeLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content” >

<TextView
android:textColor=”#000000″
android:id=”@+id/tv_avail_rom”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”內存可用:” />

<TextView
android:textColor=”#000000″
android:id=”@+id/tv_avail_sd”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentRight=”true”
android:text=”SD卡可用:” />

<frameLayout<br>
android:layout_width="match_parent"
android:layout_height=”match_parent” >

<LinearLayout
android:id=”@+id/loading”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:gravity=”center”
android:orientation=”vertical”
android:visibility=”invisible” >

<ProgressBar
android:id=”@+id/loading”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”正在獲取數據” />

<ListView
android:id=”@+id/lv_app_manage”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:fastScrollEnabled=”true” >


《《《Te<TextView
android:id=”@+id/tv_state”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”/>

</frameLayout>

================================END===============================

適配器的activity_app_manage_item.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”
android:background=”@drawable/home_item_selector”>

<ImageView
android:layout_marginLeft=”10dip”
android:id=”@+id/iv_icon”
android:layout_width=”55dip”
android:layout_height=”55dip”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:src=”@drawable/app” />

<com.itheima.mobilesafe.ui.FocusedTextView
android:textColor=”#000000″
android:textSize=”18dip”
android:id=”@+id/tv_name”
android:singleLine=”true”
android:ellipsize=”marquee”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_above=”@+id/tv_storage”
android:layout_alignLeft=”@+id/tv_storage”
android:text=”手機衛士” />

<TextView
android:layout_marginLeft=”10dip”
android:id=”@+id/tv_storage”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBottom=”@+id/iv_icon”
android:layout_toRightOf=”@+id/iv_icon”
android:text=”外部存儲” />

//=======================END=======================

PopupWindow所用到的pop_app_item.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”@drawable/local_popup_bg”
android:orientation=”horizontal” >

<LinearLayout
android:id=”@+id/ll_uninstall”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center_horizontal”
android:orientation=”vertical” >

<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/img1″ />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”卸載” />

<LinearLayout
android:id=”@+id/ll_start”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center_horizontal”
android:orientation=”vertical” >

<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/img2″ />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”啟動” />

<LinearLayout
android:id=”@+id/ll_share”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:gravity=”center_horizontal”
android:orientation=”vertical” >

<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/img3″ />

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”分享” />

//=====================END==========================

發佈留言