private PopupWindow window;
private RelativeLayout rl_complete;
private void showPopowindowDialog() {
View popwindow = inflater.inflate(R.layout.dialog_sort, null);ListView lv = (ListView) popwindow.findViewById(R.id.main_dialog_lv);
PopupWindowAdapter popupWindowAdapter = null;
if(popupWindowAdapter == null){
popupWindowAdapter = new PopupWindowAdapter();
}
lv.setAdapter(popupWindowAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
key = datas[arg2];
currentSortIndex = arg2;
window.dismiss();
sendRankInfoReq();
}
});
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
window = new PopupWindow(popwindow, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.title_function_bg)); //这句话必须有
window.setAnimationStyle(android.R.style.Animation_Dialog);
window.update();
window.setFocusable(true);
window.setTouchable(true); //设置PopupWindow可触摸
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
int[] xy = new int[2];
rl_complete.getLocationOnScreen(xy);
window.showAtLocation(rl_complete, Gravity.RIGHT | Gravity.TOP,
rl_complete.getWidth()/5,xy[1]+rl_complete.getWidth());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (window != null && window.isShowing()) {
window.dismiss();
window = null;
}
return super.onTouchEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (window != null && window.isShowing()) {
window.dismiss();
window = null;
} else {
finish();
}
}
return false;
}