public class MainActivity extends Activity implements OnItemLongClickListener, OnClickListener {

    private ListView lv_main;
    private List<AppInfo> data;
    private AppAdapter adapter;

    private PopupWindow pw;
    private View pwView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化成员变量
        lv_main = (ListView) findViewById(R.id.lv_main);
        data = getAllAppInfos();
        adapter = new AppAdapter();
        //显示列表
        lv_main.setAdapter(adapter);

        //给ListView设置item的点击监听
        lv_main.setOnItemClickListener(new OnItemClickListener() {
            /**
             * parent : ListView
             * view : 当前行的item视图对象
             * position : 当前行的下标
             */
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                //提示当前行的应用名称
                String appName = data.get(position).getAppName();
                //提示
                //Toast.makeText(MainActivity.this, appName, 0).show();

                if(pw==null) {
                    pwView = View.inflate(MainActivity.this, R.layout.pw_layout, null);
                    //得到子view设置点击监听
                    pwView.findViewById(R.id.ll_pw_uninstall).setOnClickListener(MainActivity.this);
                    pwView.findViewById(R.id.ll_pw_run).setOnClickListener(MainActivity.this);
                    pwView.findViewById(R.id.ll_pw_share).setOnClickListener(MainActivity.this);

                    pw = new PopupWindow(pwView, view.getWidth()-80, view.getHeight());
                    pw.setBackgroundDrawable(new BitmapDrawable());//必须指定一个背景图片,随便一个即可,不然以下设置的动画不显示
                }

                //如果正在显示, 移除
                if(pw.isShowing()) {
                    pw.dismiss();//只是不显示了, 但对象仍然在内存中
                }

                //(再)显示
                pw.showAsDropDown(view, 40, -view.getHeight());

                //缩放动画
                ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1);
                animation.setDuration(1000);
                //启动动画
                pwView.startAnimation(animation);
            }
        });

        //给LitView设置Item的长按监听
        lv_main.setOnItemLongClickListener(this);
        //设置listView的滚动监听
        lv_main.setOnScrollListener(new OnScrollListener() {

            /*
             * SCROLL_STATE_IDLE : 空闲(不动)
             * SCROLL_STATE_TOUCH_SCROLL : 跟着手指滚动
             * SCROLL_STATE_FLING : 快速滚动
             */
            //当listView的滚动状态发生改变时调用
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                Log.e("TAG", "onScrollStateChanged()..."+scrollState);
                if(scrollState==OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                    //移除正在显示的pw
                    if(pw!=null && pw.isShowing()) {
                        pw.dismiss();
                    }
                }
            }

            //当listView正在滚动时调用(产生move时不断调用)
            @Override
            public void onScroll(AbsListView view, int firstVisibleItemPosition,
                    int visibleItemCount, int totalItemCount) {
                Log.e("TAG", "onScroll()...");
            }
        });
    }

    class  AppAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        //返回带数据当前行的Item视图对象
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //1. 如果convertView是null, 加载item的布局文件
            if(convertView==null) {
                Log.e("TAG", "getView() load layout");
                convertView = View.inflate(MainActivity.this, R.layout.item_main, null);

            }
            //2. 得到当前行数据对象
            AppInfo appInfo = data.get(position);
            //3. 得到当前行需要更新的子View对象
            ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_item_icon);
            TextView textView = (TextView) convertView.findViewById(R.id.tv_item_name);
            //4. 给视图设置数据
            imageView.setImageDrawable(appInfo.getIcon());
            textView.setText(appInfo.getAppName());

            //返回convertView
            return convertView;
        }

    }

    /*
     * 得到手机中所有应用信息的列表
     * AppInfo
     *  Drawable icon  图片对象
     *  String appName
     *  String packageName
     */
    protected List<AppInfo> getAllAppInfos() {

        List<AppInfo> list = new ArrayList<AppInfo>();
        // 得到应用的packgeManager
        PackageManager packageManager = getPackageManager();
        // 创建一个主界面的intent
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 得到包含应用信息的列表
        List<ResolveInfo> ResolveInfos = packageManager.queryIntentActivities(
                intent, 0);
        // 遍历
        for (ResolveInfo ri : ResolveInfos) {
            // 得到包名
            String packageName = ri.activityInfo.packageName;
            // 得到图标
            Drawable icon = ri.loadIcon(packageManager);
            // 得到应用名称
            String appName = ri.loadLabel(packageManager).toString();
            // 封装应用信息对象
            AppInfo appInfo = new AppInfo(icon, appName, packageName);
            // 添加到list
            list.add(appInfo);
        }
        return list;
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {
        //删除当前行
            //删除当前行的数据
        data.remove(position);
            //更新列表
        //lv_main.setAdapter(adapter);//显示列表, 不会使用缓存的item的视图对象
        adapter.notifyDataSetChanged();//通知更新列表, 使用所有缓存的item的视图对象

        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ll_pw_uninstall:
            pw.dismiss();
            Toast.makeText(this, "卸载", 0).show();
            break;
        case R.id.ll_pw_run:
            pw.dismiss();
            Toast.makeText(this, "运行", 0).show();
            break;
        case R.id.ll_pw_share:
            pw.dismiss();
            Toast.makeText(this, "分享", 0).show();
            break;

        default:
            break;
        }
    }
}
item_bg_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_pressed="true">
        <shape>
            <solid android:color="#00ffff"></solid>
        </shape>
    </item>

</selector>

pw_layout

<?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="80dp"
    android:background="@drawable/local_popup_bg"
    android:orientation="horizontal"
    android:paddingLeft="20dp" >

    <LinearLayout
        android:id="@+id/ll_pw_uninstall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:background="@drawable/item_bg_selector" >

        <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="卸载"
            android:textSize="16sp" />
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#00ff00" />

    <LinearLayout
        android:id="@+id/ll_pw_run"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:background="@drawable/item_bg_selector" >

        <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="运行"
            android:textSize="16sp" />
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:background="#00ff00" />

    <LinearLayout
        android:id="@+id/ll_pw_share"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        android:background="@drawable/item_bg_selector">

        <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="分享"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>

PopupWindow 实现 查看所有手机应用弹出框的更多相关文章

  1. android 三种弹出框之一PopupWindow

    PopupWindow 在android的弹出框我目前了解到的是有三种:AlertDialog,PopupWindow,Activity伪弹框, AlertDialog太熟悉了,这里就不介绍了 就先看 ...

  2. iOS风格的弹出框(alert,prompt,confirm)

    前两天,自己写了一个简单的插件,在移动端使用,不管是安卓手机还是iOS系统的手机,弹出框统一使用iOS风格的. 该弹出框是依赖于jQuery的,当然也可以将用jq写的几句代码转换为原生代码. 今天把代 ...

  3. bootstrap 弹出框 另类运用

    下面是我在做一个简单登录页面时,应用boostrap弹出框,通过调节做成警示框的过程,前后经过了一番波折.因为摸索过程十分有趣,最后也是成功的,使用弹出框做除了警示框的效果,下面我们来看一下吧. 首先 ...

  4. 自定义PopupWindow弹出框(带有动画)

    使用PopupWindow来实现弹出框,并且带有动画效果 首先自定义PopupWindow public class LostPopupWindow extends PopupWindow { pub ...

  5. 练习PopupWindow弹出框之实现界面加载的时候显示弹出框到指定的view下面--两种延迟方法

    今天在练习PopupWindow弹出框的时候,打算在界面加载的时候将弹出框展现出来并显示在指定的view下面. 初步方法是直接在OnResume方法里面直接执行showPopupWindows方法. ...

  6. 通用的popupwindow底部弹出框

    前段时间做项目的时候,有几个底部弹出框,当时因为忙着赶进度所有就单独写了好几个popupwindow.后来就想着怎么实现一个通用的PopupWindow工具类 就是在要用到的时候创建该工具类的对象,并 ...

  7. [Phonegap+Sencha Touch] 移动开发19 某些安卓手机上弹出消息框 点击后不消失的解决的方法

    Ext.Msg.alert等弹出框在某些安卓手机上,点击确定后不消失. 原因是: 消息框点击确定后有一段css3 transform动画,动画完毕后才会隐藏(display:none). 有些奇葩手机 ...

  8. 安卓自动化测试工具MonkeyRunner之使用ID进行参数化,以及List选择某项和弹出框点击确定的写法

    一.List选择某项的操作步骤: 1.通过父结点得出列表各子项 2.将选择项的文本与列表中的子项进行比较 3.计算出选择项的坐标位置 截取实例: from com.android.monkeyrunn ...

  9. javascript基于对象的弹出框封装

    先睹为快,移动端:戳这里,打开页面后点击投票查看效果.PC端测试直接切换body的overflow属性:hidden和auto一样可以,比下面相对简化,又有人说这样偶尔不行..如果你知道优缺点欢迎给出 ...

随机推荐

  1. numpy基础语法

    np.zeros(5) [ 0.  0.  0.  0.  0.] 所得类型为数组, numpy.zeros_like(a, dtype=None, order='K', subok=True) 生成 ...

  2. yii2操作数据库 mysql 读写分离 主从复制

    转载地址:http://www.kuitao8.com/20150115/3471.shtml 开始使用数据库首先需要配置数据库连接组件,通过添加 db 组件到应用配置实现("基础的&quo ...

  3. Libimseti推荐系统

    技术:easyUI.jQuery.Spring.Struts.Hibernate.Mahout.MySQL 本Libimseti推荐系统使用数据.代码參考<Mahout in action> ...

  4. Android世界第一个activity启动过程

    Android世界第一个activity启动过程 第一次使用Markdown,感觉不错. Android系统从按下开机键一直到launcher的出现,是一个如何的过程,中间都做出了什么操作呢.带着这些 ...

  5. Linux程序

    1.如何找到一个进程的安装程序目录与启动目录 2.如何完全卸载一个程序

  6. 8种移动APP导航设计模式对照

    当我们确定了移动APP的设计需求和APP产品设计流程之后,開始着手设计APP界面UI或是APP原型图啦.这个时候我们都要面临的第一个问题就是怎样将信息以最优的方式组合起来? 或许我们对照和了解了其它一 ...

  7. Linux 安装、卸载程序

    一, RPM 安装:        rpm -ivh xxx.rpm 重新安装: rpm -ivh -replacepkgs xxx.rpm 卸载:       rpm -e xxx.rpm 二,ta ...

  8. .net 定时执行 windows 服务

    1.新建项目 --> Windows 服务 2.Service1.cs代码 using System; using System.Collections.Generic; using Syste ...

  9. 37:密码截取(回文串manacher算法)

    题目描述:Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解.比如 ...

  10. Spring学习十三----------Spring AOP的基本概念

    © 版权声明:本文为博主原创文章,转载请注明出处 什么是AOP -面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 -主要的功能是:日志记录.性能统计.安全控制.事务处理. ...