@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.app_manager); lv_app_manager = (ListView) findViewById(R.id.lv_app_manager); ll_app_manager_progress = (LinearLayout) findViewById(R.id.ll_app_manager_progress); ll_app_manager_progress.setVisibility(View.VISIBLE); tv_app_title = (TextView) findViewById(R.id.tv_app_title); tv_app_title.setOnClickListener(this); initUI(false); lv_app_manager.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub dismissPopupWindow(); //用来存放当前的item的坐标值,第一个是x的坐标,第二个是y的坐标 int[] location = new int[2]; //把当前的item的坐标放到int数组里面 view.getLocationInWindow(location); View popupView = View.inflate(AppManagerActivity.this,R.layout.popup_item,null); LinearLayout ll_app_unistall = (LinearLayout) popupView.findViewById(R.id.ll_app_uninstall); LinearLayout ll_app_run = (LinearLayout) popupView.findViewById(R.id.ll_app_start); LinearLayout ll_app_share = (LinearLayout) popupView.findViewById(R.id.ll_app_share); ll_app_unistall.setOnClickListener(AppManagerActivity.this); ll_app_run.setOnClickListener(AppManagerActivity.this); ll_app_share.setOnClickListener(AppManagerActivity.this); //拿到当时点击的条目,并设置到view里面 AppInfo info = (AppInfo) lv_app_manager.getItemAtPosition(position); ll_app_unistall.setTag(info); ll_app_run.setTag(info); ll_app_share.setTag(info); //添加动画 LinearLayout ll_app_popup = (LinearLayout) popupView.findViewById(R.id.ll_app_popup); ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f,0.0f,1.0f); scaleAnimation.setDuration(300); //new 一个PopupWindow出来 popupWindow = new PopupWindow(popupView,230,70); //一定要给popupWindow设置一个背景图片,不然的话,会有很多未知的问题的 //如没办法给它加上动画,还有显示会有问题等。 //如果我们没有要设置的图片,那么我们就给它加上一个透明的背景图片 Drawable drawable = new ColorDrawable(Color.TRANSPARENT); popupWindow.setBackgroundDrawable(drawable); int x = location[0] + 60; int y = location[1]; //把popupWindow显示出来 popupWindow.showAtLocation(view, Gravity.LEFT|Gravity.TOP, x, y); //开启动画 ll_app_popup.startAnimation(scaleAnimation); } }); //当listView滚动的时候调用方法 lv_app_manager.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub dismissPopupWindow(); } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub dismissPopupWindow(); } }); } //判断PopupWindow是不是存在,存在就把它dismiss掉 private void dismissPopupWindow() { if(popupWindow != null){ popupWindow.dismiss(); popupWindow = null; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if("用户应用".equals(tv_app_title.getText().toString().trim())) { initUI(true); adapter.setAppInfos(getUserApp()); //通知ListView数据发生了变化 adapter.notifyDataSetChanged(); } else { initUI(false); } } @Override protected void onDestroy() { dismissPopupWindow(); super.onDestroy(); }
运行效果:<img src="http://img.blog.csdn.net/20150121222619613?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenFzNjI3NjExMzA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
————————————————————————————————————————————————————————————————————
2.setTag(),getTag()的在popupWindow
(1)View视图设置Tag(),方便以后获取
<pre name="code" class="java">lv_app_manager.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub dismissPopupWindow(); //用来存放当前的item的坐标值,第一个是x的坐标,第二个是y的坐标 int[] location = new int[2]; //把当前的item的坐标放到int数组里面 view.getLocationInWindow(location); View popupView = View.inflate(AppManagerActivity.this,R.layout.popup_item,null); LinearLayout ll_app_unistall = (LinearLayout) popupView.findViewById(R.id.ll_app_uninstall); LinearLayout ll_app_run = (LinearLayout) popupView.findViewById(R.id.ll_app_start); LinearLayout ll_app_share = (LinearLayout) popupView.findViewById(R.id.ll_app_share); ll_app_unistall.setOnClickListener(AppManagerActivity.this); ll_app_run.setOnClickListener(AppManagerActivity.this); ll_app_share.setOnClickListener(AppManagerActivity.this); //拿到当时点击的条目,并设置到view里面 AppInfo info = (AppInfo) lv_app_manager.getItemAtPosition(position); ll_app_unistall.setTag(info); ll_app_run.setTag(info); ll_app_share.setTag(info);
(2)View视图获得Tag()中的内容
@Override <span style="white-space:pre"> </span>public void onClick(View v) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//加载过程中的时候,不能点击 <span style="white-space:pre"> </span>if(!flag) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>return; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>AppInfo item = (AppInfo) v.getTag(); <span style="white-space:pre"> </span>switch(v.getId()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//当我们点击那个标题栏之后,我们就只把用户的应用列出来 <span style="white-space:pre"> </span>case R.id.tv_app_title : <span style="white-space:pre"> </span>if("所有应用".equals(tv_app_title.getText().toString().trim())) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>tv_app_title.setText("用户应用"); <span style="white-space:pre"> </span>adapter.setAppInfos(getUserApp()); <span style="white-space:pre"> </span>//通知ListView数据发生了变化 <span style="white-space:pre"> </span>adapter.notifyDataSetChanged(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>else <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>tv_app_title.setText("所有应用"); <span style="white-space:pre"> </span>adapter.setAppInfos(list); <span style="white-space:pre"> </span>adapter.notifyDataSetChanged(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>case R.id.ll_app_uninstall : <span style="white-space:pre"> </span>if(item.isSystemApp()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>Toast.makeText(AppManagerActivity.this, "不能卸载系统的应用程序", Toast.LENGTH_SHORT).show(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>else <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>String strUri = "package:" + item.getPackageName(); <span style="white-space:pre"> </span>Uri uri = Uri.parse(strUri); <span style="white-space:pre"> </span>Intent deleteIntent = new Intent(); <span style="white-space:pre"> </span>deleteIntent.setAction(Intent.ACTION_DELETE); <span style="white-space:pre"> </span>deleteIntent.setData(uri); <span style="white-space:pre"> </span>startActivityForResult(deleteIntent, 0); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>case R.id.ll_app_start : <span style="white-space:pre"> </span>try <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//拿到这个包对应的PackageInfo对象,这里我们指定了两个flag, <span style="white-space:pre"> </span>//一个就是之前讲过的,所有的安装过的应用程序都找出来,包括卸载了但没清除数据的 <span style="white-space:pre"> </span>//一个就是指定它去扫描这个应用的AndroidMainfest文件时候的activity节点, <span style="white-space:pre"> </span>//这样我们才能拿到具有启动意义的ActivityInfo,如果不指定,是无法扫描出来的 <span style="white-space:pre"> </span>PackageInfo packageInfo = getPackageManager().getPackageInfo(item.getPackageName(), PackageManager.GET_UNINSTALLED_PACKAGES | PackageManager.GET_ACTIVITIES); <span style="white-space:pre"> </span>//扫描出来的所以activity节点的信息 <span style="white-space:pre"> </span>ActivityInfo[] activityInfos = packageInfo.activities; <span style="white-space:pre"> </span>//有些应用是无法启动的,所以我们就要判断一下 <span style="white-space:pre"> </span>if(activityInfos != null && activityInfos.length > 0) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>//在扫描出来的应用里面,第一个是具有启动意义的 <span style="white-space:pre"> </span>ActivityInfo startActivity = activityInfos[0]; <span style="white-space:pre"> </span>//设置好Intent,启动activity <span style="white-space:pre"> </span>Intent intent = new Intent(); <span style="white-space:pre"> </span>intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); <span style="white-space:pre"> </span>intent.setClassName(item.getPackageName(), startActivity.name); <span style="white-space:pre"> </span>startActivity(intent); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>else <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>Toast.makeText(AppManagerActivity.this, "这个应用程序无法启动", Toast.LENGTH_SHORT).show(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>catch (NameNotFoundException e) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>e.printStackTrace(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>case R.id.ll_app_share : <span style="white-space:pre"> </span>Intent shareIntent = new Intent(); <span style="white-space:pre"> </span>//设置Intent的action <span style="white-space:pre"> </span>shareIntent.setAction(Intent.ACTION_SEND); <span style="white-space:pre"> </span>//设定分享的类型是纯文本的 <span style="white-space:pre"> </span>shareIntent.setType("text/plain"); <span style="white-space:pre"> </span>//设置分享主题 <span style="white-space:pre"> </span>shareIntent.putExtra(Intent.EXTRA_SUBJECT, "分享"); <span style="white-space:pre"> </span>//设置分享的文本 <span style="white-space:pre"> </span>shareIntent.putExtra(Intent.EXTRA_TEXT, "有一个很好的应用程序哦!" + item.getAppName()); <span style="white-space:pre"> </span>//shareIntent = Intent.createChooser(shareIntent, "分享"); <span style="white-space:pre"> </span>startActivity(shareIntent); <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>default : <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>dismissPopupWindow(); <span style="white-space:pre"> </span>}