Popwindow彈出框的模板使用
阿新 • • 發佈:2018-12-10
Pop彈出框
public class ProductSortPop {
private Context mContext;
private View.OnClickListener mOnClickListener; //建立一個點選事件介面回撥資料
private TextView mAllSort;
private TextView mNewSort;
private TextView mCommentSort;
private View mLeftV;
private PopupWindow mPopupWindow;
public ProductSortPop(View.OnClickListener onClickListener, Context context) {
mOnClickListener = onClickListener;
mContext = context;
initView();
}
//載入佈局
private void initView() {
View view = LayoutInflater.from(mContext).inflate(R.layout.product_sort_pop_layout, null );
//找到控制元件
mAllSort = (TextView) view.findViewById(R.id.all_sort);
mNewSort = (TextView) view.findViewById(R.id.new_sort);
mCommentSort = (TextView) view.findViewById(R.id.comment_sort);
//設定點選事件
mAllSort.setOnClickListener(mOnClickListener);
mNewSort.setOnClickListener(mOnClickListener);
mCommentSort.setOnClickListener(mOnClickListener);
view.findViewById(R.id.left_v).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onDismiss();
}
});
mPopupWindow = new PopupWindow(view,
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
//設定內部獲取焦點
mPopupWindow.setFocusable(true);
//設定外部獲取焦點
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable());
//更新當前的狀態
mPopupWindow.update();
}
//建立一個顯示的方法
public void onShow(View v){
if (!mPopupWindow.isShowing()){
mPopupWindow.showAsDropDown(v,0,5);
}
}
//建立一個隱藏的方法
public void onDismiss(){
if (mPopupWindow.isShowing()){
mPopupWindow.dismiss();
}
}
}