打造自定義彈出框
阿新 • • 發佈:2018-12-31
自定義彈出框的使用還是比較頻繁,本次主要講解自定義彈出框和使用的封裝,通過介面利於維護。
首先給出自定義彈出框的實現:
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);
}
}
剩下的就是使用介面呼叫方法來完成邏輯業務了。你可能會有不同的介面需求,那麼你只需要仿照上面的介面實現類來做就行,當然這需要花上幾分鐘閱讀一下程式碼。