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<AppInfo> appinfos;
private List<AppInfo> systemappinfos;
private List<AppInfo> 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<AppInfo> appinfos = AppInfoProvider
.getAppInfos(getApplicationContext());
systemappinfos = new ArrayList<AppInfo>();
userappinfos = new ArrayList<AppInfo>();
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<AppInfo> getAppInfos(Context context){
PackageManager pm = context.getPackageManager();
List<AppInfo> appInfos = new ArrayList<AppInfo>();
List<PackageInfo> 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="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="#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卡可用:" />
    </RelativeLayout>


    <FrameLayout
        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="正在获取数据" />
        </LinearLayout>


        <ListView
            android:id="@+id/lv_app_manage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fastScrollEnabled="true" >
        </ListView>
        <!--  FrameLayout 中 越是往下的空间  布局越是在顶端-->
        《《《Te<TextView
        android:id="@+id/tv_state"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        
    </FrameLayout>


</LinearLayout>

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

适配器的activity_app_manage_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://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="外部存储" />
</RelativeLayout>


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

PopupWindow所用到的pop_app_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="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>


    <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>


    <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="分享" />
    </LinearLayout>


</LinearLayout>


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

Android 复杂Listview的显示,仿金山手机助手程序卸载界面,布布扣,bubuko.com

Android 复杂Listview的显示,仿金山手机助手程序卸载界面

上一篇:android 调用系统分享功能以及实现自定义分享


下一篇:Android listview中嵌套Checkbox的布局文件