Android开发中PopupWindow用法实例分析

时间:2021-12-17 07:26:17

本文实例分析了Android开发中PopupWindow用法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
private TextView tv_appmanager_title;
private ListView lv_app_manager;
private LinearLayout ll_appmanager_loading;
private AppManagerProvider provider;
private List<AppManagerInfo> infos ;
private AppManagerAdapter adapter;
private PopupWindow localPopupWindow;
private Handler handler = new Handler(){
  public void handleMessage(android.os.Message msg) {
    switch (msg.what) {
    case Constants.MSG_APP_MANAGER:
      ll_appmanager_loading.setVisibility(View.INVISIBLE);
      adapter = new AppManagerAdapter(infos, AppManagerActivity.this);
      lv_app_manager.setAdapter(adapter);
      break;
    }
  };
};
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.appmanager);
  lv_app_manager = (ListView) this.findViewById(R.id.lv_app_manager);
  ll_appmanager_loading = (LinearLayout) this.findViewById(R.id.ll_appmanager_loading);
  ll_appmanager_loading.setVisibility(View.VISIBLE);
  new Thread(){
    public void run() {
      provider = new AppManagerProvider(AppManagerActivity.this);
      infos = provider.getAppManagerInfo();
      Message msg = new Message();
      msg.what = Constants.MSG_APP_MANAGER;
      handler.sendMessage(msg);
    };
  }.start();
  lv_app_manager.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
      if(null != localPopupWindow){
        localPopupWindow.dismiss();
        localPopupWindow = null;
      }
      int [] location = new int[2];
      view.getLocationInWindow(location);
      int x = location[0] + 60;
      int y = location[1];
      TextView tv = new TextView(AppManagerActivity.this);
      AppManagerInfo info = (AppManagerInfo) lv_app_manager.getItemAtPosition(position);
      tv.setText(info.getPackName());
      tv.setTextSize(20);
      tv.setTextColor(Color.RED);
      localPopupWindow = new PopupWindow(tv, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      Drawable background = new ColorDrawable(Color.GRAY);
      localPopupWindow.setBackgroundDrawable(background);
      localPopupWindow.showAtLocation(tv, 51, x, y);
    }
  });
  lv_app_manager.setOnScrollListener(new OnScrollListener() {
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
      if(null != localPopupWindow){
        localPopupWindow.dismiss();
        localPopupWindow = null;
      }
    }
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
      if(null != localPopupWindow){
        localPopupWindow.dismiss();
        localPopupWindow = null;
      }
    }
  });
}

希望本文所述对大家Android程序设计有所帮助。