listview长按弹出窗popupwindow

http://www.apkbus.com/android-232857-1-1.html?_dsign=e6ea8f87


如图:

listview长按弹出窗popupwindow_第1张图片
[AppleScript]  查看源文件  复制代码
package com.example.popupwindowtest;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;
import android.R.string;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
        private ListView mListView;
        private List mItems;
        private PopupWindow mPopupWindow;
        private View contentView;
        private int mScreenHeight;

        private Button ibtn1, ibtn2, ibtn3;
        private TextView showinfo;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                // 初始化数据
                initItems();
                // 用于显示信息
                showinfo = (TextView) findViewById(R.id.show);

                // 弹窗初始化
                LayoutInflater lf = (LayoutInflater) MainActivity.this
                                .getSystemService(LAYOUT_INFLATER_SERVICE);
                contentView = lf.inflate(R.layout.popupwindow, null);
                mPopupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT);
                mPopupWindow.setFocusable(true);
                mPopupWindow.setOutsideTouchable(true);
                mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

                // 列表初始化
                mListView = (ListView) findViewById(R.id.listView1);
                mListView.setAdapter(new ArrayAdapter(this, R.layout.item,
                                mItems));

                // 获取屏幕实际像素
                mScreenHeight = getScreenHeight();

                // 列表项长按事件
                mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
                        @Override
                        public boolean onItemLongClick(AdapterView parent, View view,
                                        int position, long id) {
                                Toast.makeText(MainActivity.this, "你长按了第" + position + "项",
                                                Toast.LENGTH_SHORT).show();
                                // 获取被点击项所在位置
                                int[] a = new int[2];
                                view.getLocationOnScreen(a);
                                showinfo.setText("x=" + a[0] + "   y=" + a[1]);

                                // 在指定位置显示弹窗, 以底部中间为基准点
                                mPopupWindow.showAtLocation(mListView, Gravity.BOTTOM
                                                | Gravity.CENTER, 0, mScreenHeight - a[1]);
                                return false;
                        }
                });

                // 弹窗内的按钮点击事件
                ibtn1 = (Button) contentView.findViewById(R.id.ibutton1);
                ibtn2 = (Button) contentView.findViewById(R.id.ibutton2);
                ibtn3 = (Button) contentView.findViewById(R.id.ibutton3);
                MyOnClickListener myOnClickListener = new MyOnClickListener();
                ibtn1.setOnClickListener(myOnClickListener);
                ibtn2.setOnClickListener(myOnClickListener);
                ibtn3.setOnClickListener(myOnClickListener);
        }

        // 列表数据初始化
        private void initItems() {
                mItems = new ArrayList();
                for (int i = 0; i < 20; i++) {
                        mItems.add("项目" + i);
                }
        }

        // 点击事件
        private class MyOnClickListener implements View.OnClickListener {
                @Override
                public void onClick(View v) {
                        Button btn = (Button) v;
                        showinfo.setText(btn.getText());
                }
        }

        /**
         * 获取屏幕高度像素
         * 
         * @return
         */
        private int getScreenHeight() {
                // 获取屏幕实际像素
                DisplayMetrics displayMetrics = new DisplayMetrics();
                Display display = MainActivity.this.getWindowManager()
                                .getDefaultDisplay();
                display.getMetrics(displayMetrics);
                return displayMetrics.heightPixels;
        }
}


  PopupWindowTest.zip (2.74 MB, 下载次数: 456) 

你可能感兴趣的:(view)