您的位置:首页 > 其它

打造自定义弹出框

2016-10-09 19:44 99 查看
自定义弹出框的使用还是比较频繁,本次主要讲解自定义弹出框和使用的封装,通过接口利于维护。

首先给出自定义弹出框的实现:

public class CustomPopupWindow extends PopupWindow implements OnTouchListener{

protected View mPopupView;
protected Context context;
/**
* PopupWindow显示在哪个view的下面
*/
protected View locationView;
protected LayoutInflater inflater;
protected int _popupWindow_Layout_ID = -1;

public View getPopupWindowView(){
return mPopupView;
}
/**
*
* @param _popupWindow_Layout_ID:需要显示的视图的布局id
* @param viewLayoutLocation_ID:视图显示在哪个布局的下面
*/
public void toShowPopupWindowLayout(int _popupWindow_Layout_ID, int viewLayoutLocation_ID){
if (this._popupWindow_Layout_ID  == -1){
this._popupWindow_Layout_ID = _popupWindow_Layout_ID;
}
configPopupWindowView();
showPopupWindow(viewLayoutLocation_ID);
}

public void toShowPopupWindowLayout(int _popupWindow_Layout_ID, int viewLayoutLocation_ID,
PopupWindowLayoutParams params){
if (this._popupWindow_Layout_ID  == -1){
this._popupWindow_Layout_ID = _popupWindow_Layout_ID;
}
if (inflater == null){
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
mPopupView = inflater.inflate(_popupWindow_Layout_ID, null);

this.setContentView(mPopupView);
if (params == PopupWindowLayoutParams.wrap_parent){
this.setWidth(LayoutParams.WRAP_CONTENT);
} else if (params == PopupWindowLayoutParams.match_parent){
this.setWidth(LayoutParams.MATCH_PARENT);
}
this.setHeight(LayoutParams.WRAP_CONTENT);
mPopupView.setFocusable(true);
mPopupView.setFocusableInTouchMode(true);
this.setOutsideTouchable(false);
this.setFocusable(false);
this.setTouchable(true);

this.setAnimationStyle(R.style.anim_menu_bottombar);
ColorDrawable dw = new ColorDrawable(0xEEEEEE);//实例化一个ColorDrawable颜色为0xb0000000半透明
this.setBackgroundDrawable(dw); //设置弹出窗体的背景
mPopupView.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View arg0, int keyCode, KeyEvent arg2) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
CustomPopupWindow.this.dismiss();
return true;
}
return false;
}
});
showPopupWindow(viewLayoutLocation_ID);

}

public enum PopupWindowLayoutParams{
wrap_parent, match_parent
}

private void configPopupWindowView(){
if (inflater != null){
return;
}
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mPopupView = inflater.inflate(_popupWindow_Layout_ID, null);

this.setContentView(mPopupView);
this.setWidth(LayoutParams.WRAP_CONTENT);
this.setHeight(LayoutParams.WRAP_CONTENT);
mPopupView.setFocusable(true);
mPopupView.setFocusableInTouchMode(true);
this.setOutsideTouchable(false);
this.setFocusable(false);
this.setTouchable(true);

this.setAnimationStyle(R.style.anim_menu_bottombar);
ColorDrawable dw = new ColorDrawable(0xEEEEEE);//实例化一个ColorDrawable颜色为0xb0000000半透明
this.setBackgroundDrawable(dw); //设置弹出窗体的背景
mPopupView.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View arg0, int keyCode, KeyEvent arg2) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
CustomPopupWindow.this.dismiss();
return true;
}
return false;
}
});
}

public CustomPopupWindow(Context context) {
super(context);
this.context = context;
}

private void showPopupWindow(int viewLayoutLocation_ID){
locationView = inflater.inflate(viewLayoutLocation_ID, null);
this.showAtLocation(locationView,
Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction() == MotionEvent.ACTION_OUTSIDE){
boolean isShow = this.isShowing();
if (isShow){
// 不做处理
}
}
return false;
}

}


首先定义接口分别用于显示和不显示弹出框:

public interface IPopupWindowManager {

void disposePopupWindowToShow();
void dismissPopupWindow();
}


实现接口并注入弹出框对象,并且回调点击事件

public class PopupWindowManagerImpl implements IPopupWindowManager{

private CustomPopupWindow customPopupWindow;
private Context context;
private Button btn_popupwindow_cancel_id;

private OnPopupWindowClickListener listener;

public PopupWindowManagerImpl(Context context, OnPopupWindowClickListener listener){
this.context = context;
this.listener = listener;
}

@Override
public void disposePopupWindowToShow() {
if (customPopupWindow == null){
customPopupWindow = new CustomPopupWindow(context);
}
customPopupWindow.toShowPopupWindowLayout(R.layout.popupwindow_view, R.layout.download_files_queue_activity,
CustomPopupWindow.PopupWindowLayoutParams.match_parent);
btn_popupwindow_cancel_id = (Button) customPopupWindow.getPopupWindowView().findViewById(R.id.popupwindow_cancel_id);

btn_popupwindow_cancel_id.setText(Application.getAppString(R.string.cancel_down_queue));
btn_popupwindow_cancel_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onPopupWindowClick(v);
}
});

}

@Override
public void dismissPopupWindow() {
if (customPopupWindow != null && customPopupWindow.isShowing()){
customPopupWindow.dismiss();
}
}

public interface OnPopupWindowClickListener{
void onPopupWindowClick(View view);
}

}


剩下的就是使用接口调用方法来完成逻辑业务了。你可能会有不同的界面需求,那么你只需要仿照上面的接口实现类来做就行,当然这需要花上几分钟阅读一下代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  自定义弹出框