public class RuanjianManagerActivity extends Activity {
private TextView tv_neicun;
private TextView tv_sdcard;
private LinearLayout lt;
private ListView lst;
private List<RuanjianmanagerInfo> infos;//所有应用程序的集合
private List<RuanjianmanagerInfo> useInfos;//用户应用程序的集合
private List<RuanjianmanagerInfo> systemuseInfos;//系统应用程序的集合
private TextView tv_issystem;
private PopupWindow pop;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ruanjianmanager);
tv_neicun = findViewById(R.id.tv_neicun);
tv_sdcard = findViewById(R.id.tv_sdcard);
tv_issystem = findViewById(R.id.tv_tititleissystem);
long sdSize = getAvailabelSpace(Environment.getExternalStorageDirectory().getAbsolutePath());
long romSize = getAvailabelSpace(Environment.getDataDirectory().getAbsolutePath());
tv_sdcard.setText("SD卡的可用空间:" + sdSize);
tv_neicun.setText("可用空间:" + romSize);
lt = findViewById(R.id.ruanjianmangerLoding);
lst = findViewById(R.id.lst_ruanjianmanegr);
lt.setVisibility(View.VISIBLE);//设置转圈可见
new Thread() {
@Override
public void run() {
super.run();
infos = RuanjianManagerInfoProvide.getInfos(RuanjianManagerActivity.this);
useInfos = new ArrayList<RuanjianmanagerInfo>();
systemuseInfos = new ArrayList<RuanjianmanagerInfo>();
for (RuanjianmanagerInfo info : infos) {
if (info.isUserApp()) {
useInfos.add(info);
} else {
systemuseInfos.add(info);
}
}
//加载listview的数据适配器---回到主线程刷新
runOnUiThread(new Runnable() {
@Override
public void run() {
lst.setAdapter(new RuanjianManagerAdpt());
lt.setVisibility(View.INVISIBLE);//设置转圈不可见
}
});
}
}.start();
//滚动listview的监听
lst.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
//正在滚动
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
if(pop!=null&&pop.isShowing()){//吧旧的弹出窗体关闭掉
pop.dismiss();
pop=null;
}
//i是第一个可见条目在listview集合中的位置
if (useInfos != null && systemuseInfos != null) {
if (i >useInfos.size()) {
tv_issystem.setText("系统程序" + systemuseInfos.size());
} else {
tv_issystem.setText("用户程序" + useInfos.size());
}
}
}
});
//listview的点击事件
lst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
RuanjianmanagerInfo info;
@SuppressLint("ResourceAsColor")
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(i==0){
return;
}else if(i==useInfos.size()+1){
return;
}else if(i<useInfos.size()){//用户的程序
int newposition=i-1;
info=useInfos.get(newposition);
}else {//系统的程序
int newposition=i-1-useInfos.size()-1;
info=systemuseInfos.get(newposition);
}
Log.e("e", "onItemClick:");
//弹出气泡PopupWindow
// TextView contentview=new TextView(getApplicationContext());
// contentview.setText(info.getPackname());
//自定义的布局
View contentview=View.inflate(getApplicationContext(),R.layout.popup_ruanjianmanager_item,null);
if(pop!=null&&pop.isShowing()){//吧旧的弹出窗体关闭掉
pop.dismiss();
pop=null;
}
pop = new PopupWindow(contentview,-2,-2);//-2 是包裹内容,-1 是填充父窗体
pop.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));//透明背景
//箭头指向左边和上边,间隔0
int[] location=new int[2];
view.getLocationInWindow(location);
// pop.showAtLocation(adapterView, Gravity.LEFT|Gravity.TOP,location[0],location[1]);
pop.showAtLocation(adapterView, Gravity.LEFT|Gravity.TOP,60,location[1]);
}
});
}
private class RuanjianManagerAdpt extends BaseAdapter {
@Override
public int getCount() {
// 控制listview的条数
return useInfos.size() + 1 + systemuseInfos.size() + 1;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
RuanjianmanagerInfo info;
if (i == 0) {
TextView tv = new TextView(getApplicationContext());
tv.setText("用户程序:" + useInfos.size());
tv.setBackgroundColor(Color.GRAY);
return tv;
} else if (i == (useInfos.size() + 1)) {
TextView tv = new TextView(getApplicationContext());
tv.setText("系统程序:" + systemuseInfos.size());
tv.setBackgroundColor(Color.GRAY);
return tv;
} else if (i <= useInfos.size()) {//用户程序
int newpoition = i - 1;
info = useInfos.get(newpoition);
} else {//系统程序
int newpoition = i - 1-useInfos.size()-1;
info = systemuseInfos.get(newpoition);
}
View v;
ViewHolder holder;
//
if (view != null&& view instanceof RelativeLayout) {//判断是否是合适的类型复用
v = view;
holder = (ViewHolder) v.getTag();//获取holder
} else {
v = View.inflate(getApplicationContext(), R.layout.ruanjianmanager_item, null);
holder = new ViewHolder();
holder.icon = v.findViewById(R.id.image_ruanjianmanager_item);
holder.tv_name = v.findViewById(R.id.tv_name_ruanjianmanager_item);
holder.tv_location = v.findViewById(R.id.tv_from_ruanjianmanager_item);
v.setTag(holder);//存储holer
}
holder.icon.setImageDrawable(info.getIcon());//设置imageview的图片
holder.tv_name.setText(info.getName());
return v;
}
}
static class ViewHolder {
TextView tv_name;
TextView tv_location;
ImageView icon;
}
@Override
protected void onDestroy() {
super.onDestroy();
pop.dismiss();
pop=null;
}
/*
* 获取某个目录的可用空间
*
* */
private long getAvailabelSpace(String path) {
StatFs statf = new StatFs(path);
statf.getBlockCount();//获取分区的个数
long size = statf.getBlockSize();//获取分区的大小
long count = statf.getAvailableBlocks();//获取可用分区的个数
return size * count;
}
}
<?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">
<TextView
android:id="@+id/tv_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8866ff00"
android:gravity="center"
android:text="软件管理"
android:textSize="22sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp">
<TextView
android:id="@+id/tv_neicun"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="内存可用:" />
<TextView
android:id="@+id/tv_sdcard"
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"
android:layout_marginTop="30dp">
<LinearLayout
android:id="@+id/ruanjianmangerLoding"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在加载...."
android:textSize="22sp" />
</LinearLayout>
<ListView
android:id="@+id/lst_ruanjianmanegr"
android:fastScrollEnabled="true"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<TextView
android:id="@+id/tv_tititleissystem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户数据"/>
</FrameLayout>
</LinearLayout>