本文实例分析了Android开发中PopupWindow用法。分享给大家供大家参考,具体如下:
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相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android通信方式总结》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:Android编程实现popupwindow弹出后屏幕背景变成半透明效果Android Animation实战之屏幕底部弹出PopupWindowAndroid编程之ICS式下拉菜单PopupWindow实现方法详解(附源码下载)android使用PopupWindow实现页面点击顶部弹出下拉菜单Android PopupWindow使用实例Android PopupWindow 点击外面取消实现代码Android中PopupWindow响应返回键并关闭的2种方法Android入门之PopupWindow用法实例解析android教程之使用popupwindow创建菜单示例Android之用PopupWindow实现弹出菜单的方法详解android popwindow实现左侧弹出菜单层及PopupWindow主要方法介绍android PopupWindow 和 Activity弹出窗口实现方式