码迷,mamicode.com
首页 > 其他好文 > 详细

28.悬浮窗、listview显示不同条目

时间:2015-11-25 10:59:48      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

APP管理界面
布局
技术分享
用户程序那一行一直在最上面,拖动到系统程序时又显示系统多少个
  1. <FrameLayout
  2. android:layout_width="fill_parent"
  3. android:layout_height="fill_parent" >
  4. <LinearLayout
  5. android:id="@+id/ll_loading"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. android:gravity="center"
  9. android:orientation="vertical"
  10. android:visibility="invisible" >
  11. <ProgressBar
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:text="正在加载程序信息..." />
  18. </LinearLayout>
  19. <ListView
  20. android:overScrollMode="never"//去除滚动时的动画
  21. android:fastScrollEnabled="true"//有了右侧的导航栏
  22. android:id="@+id/lv_app_manager"
  23. android:layout_width="fill_parent"
  24. android:layout_height="fill_parent" >
  25. </ListView>
  26. <TextView
  27. android:id="@+id/tv_status"
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. android:text="用户程序:6个"
  31. android:textColor="#ffffff"
  32. android:background="#ff888888"
  33. />
  34. </FrameLayout>

代码
  • 获取SD卡空间
  • 悬浮窗的使用
  • listview滚动事件
  • listview显示不同条目
  • 动画
  • 获取系统应用程序信息
技术分享技术分享
AppInfo 
  1. public class AppInfo {
  2. private Drawable icon;
  3. private String name;
  4. private String packname;
  5. private boolean inRom;//true为手机内存 false为外部内存
  6. private boolean userApp;//true 用户 false 系统
  7. .....
  8. }
AppInfoProvider 
  1. /**
  2. * 业务方法,提供手机里面安装的所有的应用程序信息
  3. */
  4. public class AppInfoProvider {
  5. /**
  6. * 获取所有的安装的应用程序信息。
  7. * @param context 上下文
  8. * @return
  9. */
  10. public static List<AppInfo> getAppInfos(Context context){
  11. PackageManager pm = context.getPackageManager();//获取包的管理器
  12. List<AppInfo> appInfos = new ArrayList<AppInfo>();//存放所有应用程序信息集合
  13. AppInfo appInfo = null;
  14. List<PackageInfo> PackInfos = pm.getInstalledPackages(0);//所有的安装在系统上的应用程序包信息 0为不关心特殊的标记
  15. for (PackageInfo packageInfo : PackInfos) {
  16. String packName = packageInfo.packageName;//获取包名
  17. Drawable icon = packageInfo.applicationInfo.loadIcon(pm);//获取应用程序图标
  18. String name = packageInfo.applicationInfo.loadLabel(pm).toString();//获取应用程序名称
  19. int flag = packageInfo.applicationInfo.flags;
  20. appInfo = new AppInfo();
  21. if((flag&ApplicationInfo.FLAG_SYSTEM) == 0){//用户程序
  22. appInfo.setUserApp(true);
  23. }else{ //系统程序
  24. appInfo.setUserApp(false);
  25. }
  26. if((flag&ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0){//手机内存
  27. appInfo.setInRom(true);
  28. }else{
  29. appInfo.setInRom(false);
  30. }
  31. appInfo.setPackname(packName);
  32. appInfo.setIcon(icon);
  33. appInfo.setName(name);
  34. appInfos.add(appInfo);
  35. }
  36. return appInfos;
  37. }
  38. }
代码
  1. public class AppManagerActivity extends Activity implements OnClickListener {
  2. //手机内存和SD卡的剩余存储空间
  3. private TextView tv_avail_rom;
  4. private TextView tv_avail_sd;
  5. //加载进度
  6. private LinearLayout ll_loading;
  7. //ListView
  8. private ListView lv_app_manager;
  9. //Adpater
  10. private AppManagerAdapter adapter;
  11. //应用程序包集合
  12. private List<AppInfo> appInfos; //所有应用程序包集合
  13. private List<AppInfo> userAppInfos;//所有用户程序包集合
  14. private List<AppInfo> systemAppInfos;//所有系统程序包集合
  15. //程序信息的状态
  16. private TextView tv_status;
  17. //被点击的条目
  18. private AppInfo appInfo;
  19. //悬浮窗体
  20. private PopupWindow popupWindow;
  21. private LinearLayout ll_start;//开启
  22. private LinearLayout ll_uninstall;//卸载
  23. private LinearLayout ll_share;//分享
  24. private ApplockDao dao;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_app_manager);
  29. dao = new ApplockDao(this);
  30. tv_status = (TextView) findViewById(R.id.tv_status);
  31. tv_avail_rom = (TextView) findViewById(R.id.tv_avail_rom);
  32. tv_avail_sd = (TextView) findViewById(R.id.tv_avail_sd);
  33. lv_app_manager = (ListView) findViewById(R.id.lv_app_manager);
  34. ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
  35. showAvailableSize();//显示存储的剩余空间
  36. fillData();
  37. // 给listview注册一个滚动的监听器
  38. lv_app_manager.setOnScrollListener(new OnScrollListener() {
  39. //当前滚动的状态
  40. @Override
  41. public void onScrollStateChanged(AbsListView view, int scrollState) {
  42. switch (scrollState) {
  43. case OnScrollListener.SCROLL_STATE_FLING: //开始滚动
  44. break;
  45. case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滚动
  46. break;
  47. case OnScrollListener.SCROLL_STATE_IDLE://停止滚动
  48. break;
  49. }
  50. }
  51. // 滚动的时候调用的方法。
  52. // firstVisibleItem 第一个可见条目在listview集合里面的位置。
  53. @Override
  54. public void onScroll(AbsListView view, int firstVisibleItem,
  55. int visibleItemCount, int totalItemCount) {
  56. dismissPopupWindow();
  57. if(userAppInfos != null && systemAppInfos != null){
  58. if(firstVisibleItem > userAppInfos.size()){
  59. tv_status.setText("系统程序:"+systemAppInfos.size()+"个");
  60. }else{
  61. tv_status.setText("用户程序:"+userAppInfos.size()+"个");
  62. }
  63. }
  64. }
  65. });
  66. /**
  67. * 设置listview的点击事件
  68. */
  69. lv_app_manager.setOnItemClickListener(new OnItemClickListener() {
  70. @Override
  71. public void onItemClick(AdapterView<?> parent, View view,
  72. int position, long id) {
  73. if(position == 0 || position == userAppInfos.size()+1){ //如果是"用户程序" 或者 "系统程序" 的小标签则直接返回
  74. return;
  75. }else if(position <= userAppInfos.size()){
  76. int newPosition = position - 1;
  77. appInfo = userAppInfos.get(newPosition);
  78. }else{
  79. int newPosition = position-1-userAppInfos.size()-1;
  80. appInfo = systemAppInfos.get(newPosition);
  81. }
  82. dismissPopupWindow();
  83. View contentView = View.inflate(getApplicationContext(),R.layout.popup_app_item,null);
  84. ll_uninstall = (LinearLayout) contentView.findViewById(R.id.ll_uninstall);
  85. ll_start = (LinearLayout) contentView.findViewById(R.id.ll_start);
  86. ll_share = (LinearLayout) contentView.findViewById(R.id.ll_share);
  87. //为悬浮窗体的各项设置点击事件
  88. ll_uninstall.setOnClickListener(AppManagerActivity.this);
  89. ll_start.setOnClickListener(AppManagerActivity.this);
  90. ll_share.setOnClickListener(AppManagerActivity.this);
  91. //弹出悬浮窗体
  92. popupWindow = new PopupWindow(contentView,-2,-2);
  93. popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  94. int[] location = new int[2];
  95. view.getLocationInWindow(location);
  96. int dip = 60;
  97. int px = DensityUtil.dip2px(getApplicationContext(),dip);
  98. popupWindow.showAtLocation(parent,Gravity.LEFT|Gravity.TOP,px,location[1]);
  99. ScaleAnimation sa = new ScaleAnimation(0.3f, 1.0f, 0.3f, 1.0f,
  100. Animation.RELATIVE_TO_SELF, 0,
  101. Animation.RELATIVE_TO_SELF, 0.5f);
  102. sa.setDuration(300);
  103. AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
  104. aa.setDuration(300);
  105. AnimationSet set = new AnimationSet(false);
  106. set.addAnimation(aa);
  107. set.addAnimation(sa);
  108. contentView.startAnimation(set);
  109. }
  110. });
  111. }
  112. /**
  113. * 显示存储的剩余空间
  114. */
  115. public void showAvailableSize(){
  116. long romSize = getAvailSpace(Environment.getDataDirectory().getAbsolutePath());//手机内部存储大小
  117. long sdSize = getAvailSpace(Environment.getExternalStorageDirectory().getAbsolutePath());//外部存储大小
  118. tv_avail_rom.setText("内存可用空间: "+Formatter.formatFileSize(this,romSize));
  119. tv_avail_sd.setText("SD卡可用空间:"+Formatter.formatFileSize(this,sdSize));
  120. }
  121. /**
  122. * 获取某个目录的可用空间
  123. */
  124. public long getAvailSpace(String path){
  125. StatFs statfs = new StatFs(path);
  126. long size = statfs.getBlockSize();//获取分区的大小
  127. long count = statfs.getAvailableBlocks();//获取可用分区块的个数
  128. return size*count;
  129. }
  130. /**
  131. * 填充Adapter数据
  132. */
  133. public void fillData(){
  134. ll_loading.setVisibility(View.VISIBLE);//让加载程序的界面显示出来
  135. new Thread(){
  136. public void run() {
  137. //所有应用程序包的集合
  138. appInfos = AppInfoProvider.getAppInfos(AppManagerActivity.this);
  139. userAppInfos = new ArrayList<AppInfo>();
  140. systemAppInfos = new ArrayList<AppInfo>();
  141. for (AppInfo info : appInfos) {
  142. if(info.isUserApp()){ //如果是 用户程序
  143. userAppInfos.add(info);
  144. }else{
  145. systemAppInfos.add(info);
  146. }
  147. }
  148. // 加载listview的数据适配器
  149. runOnUiThread(new Runnable() {
  150. @Override
  151. public void run() {
  152. if(adapter == null){ //如果适配器为空 则创建适配器对象 为listview设置adapter
  153. adapter = new AppManagerAdapter();
  154. lv_app_manager.setAdapter(adapter);
  155. }else{//
  156. adapter.notifyDataSetChanged(); //动态更新ListView
  157. }
  158. ll_loading.setVisibility(View.INVISIBLE);
  159. }
  160. });
  161. };
  162. }.start();
  163. }
  164. /**
  165. * ListView的适配器
  166. */
  167. private class AppManagerAdapter extends BaseAdapter{
  168. @Override
  169. public int getCount() {
  170. return userAppInfos.size()+1+systemAppInfos.size()+1;
  171. }
  172. @Override
  173. public View getView(int position, View convertView, ViewGroup parent) {
  174. AppInfo appInfo;
  175. if(position == 0){ //显示用户程序有多少个的小标签
  176. TextView tv = new TextView(getApplication());
  177. tv.setTextColor(Color.WHITE);
  178. tv.setBackgroundColor(Color.GRAY);
  179. tv.setText("用户程序:"+userAppInfos.size()+"个");
  180. return tv;
  181. }else if(position == (userAppInfos.size()+1)){
  182. TextView tv = new TextView(getApplicationContext());
  183. tv.setTextColor(Color.WHITE);
  184. tv.setBackgroundColor(Color.GRAY);
  185. tv.setText("系统程序:"+systemAppInfos.size()+"个");
  186. return tv;
  187. }else if(position <= userAppInfos.size()){
  188. int newPosition = position-1;
  189. appInfo = userAppInfos.get(newPosition);
  190. }else{
  191. int newPosition = position-1-userAppInfos.size()-1;
  192. appInfo = systemAppInfos.get(newPosition);
  193. }
  194. View view;
  195. ViewHolder holder;
  196. // 不仅需要检查是否为空,还要判断是否是合适的类型去复用
  197. if(convertView != null && convertView instanceof RelativeLayout){
  198. view = convertView;
  199. holder = (ViewHolder) view.getTag();
  200. }else{
  201. view = View.inflate(getApplicationContext(),R.layout.list_item_appinfo,null);
  202. holder = new ViewHolder();
  203. holder.iv_icon = (ImageView) view.findViewById(R.id.iv_app_icon);
  204. holder.tv_location = (TextView) view.findViewById(R.id.tv_app_location);
  205. holder.tv_name = (TextView) view.findViewById(R.id.tv_app_name);
  206. view.setTag(holder);
  207. }
  208. holder.iv_icon.setImageDrawable(appInfo.getIcon());
  209. holder.tv_name.setText(appInfo.getName());
  210. if(appInfo.isInRom()){
  211. holder.tv_location.setText("手机内存");
  212. }else{
  213. holder.tv_location.setText("外部存储");
  214. }
  215. return view;
  216. }
  217. @Override
  218. public Object getItem(int position) {
  219. return null;
  220. }
  221. @Override
  222. public long getItemId(int position) {
  223. return 0;
  224. }
  225. }
  226. static class ViewHolder {
  227. TextView tv_name;
  228. TextView tv_location;
  229. ImageView iv_icon;
  230. }






28.悬浮窗、listview显示不同条目

标签:

原文地址:http://www.cnblogs.com/liuyu0529/p/4993798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!