对于PopupWindow,需求一般都是,显示窗口视图,指定窗口大小,高度,显示的位置,编写业务逻辑的代码。许多人的做法是直接在activity或fragment初始化,并且加载view等等操作,那么这样做法是不好的,如果视图内容和业务逻辑代码多情况下,后期维护管理比较烦.因此我们可以选择为PopupWindow构造一个基类的builder,加速开发同时又可以在builder里面编写业务逻辑代码,进一步提高了代码的质量.
例如,我在一个activity或者fragment里面需要一个PopupWindow,那么他的操作就仅仅是:
1构建builder引用
2调用api创建PopupWindow
3被触发显示窗口视图
如下:
windowAdapter = new MyWindowBuidler(getActivity()); windowAdapter.buildWindow();
@Override public void onClick(View view) { windowAdapter.getWindow().showAsDropDown(view); }
对于MyWindowBuidler的编写是
1提供视图 2设置窗口大小 3设置窗口高度 4编写业务逻辑代码 如 xml视图 名为R.layout.window_dota2categate
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" /> </LinearLayout>public class MyWindowBuidler extends BasePopupWindowBuilder { public MyWindowBuidler(Context context) { super(context); } @Override protected int setResource() { //提供Buidler的视图 return R.layout.window_dota2categate; } @Override protected int setWidth() { WindowManager windowManager = (WindowManager) getWindowContext().getSystemService(Context.WINDOW_SERVICE); int with = windowManager.getDefaultDisplay().getWidth(); //在这里我设置了window宽度为去掉屏幕 1/4的屏幕宽度 return with - with/4; } @Override protected int setHeight() { //在这里我设置了windowg高度为自适应控件的高度 return ViewGroup.LayoutParams.WRAP_CONTENT; } private TextView textView; @Override protected void findViews(View windowView) { //加载UI textView = (TextView) windowView.findViewById(R.id.text); } @Override protected void init() { //以下处理业务逻辑等 textView.setText("66666"); } }
而对于builder基类大致编写import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager; import android.widget.PopupWindow; /** * Created by Sam on 2015/10/7. */ public abstract class BasePopupWindowBuilder { private PopupWindow popupWindow; private Context context; public BasePopupWindowBuilder(Context context) { this.context = context; } public Context getWindowContext() { return context; } /** * 设置PopupWindow资源 * @return */ protected abstract int setResource(); /** * 设置PopupWindow的宽度 * @return */ protected abstract int setWidth(); /** * 设置PopupWindow的高度 * @return */ protected abstract int setHeight(); /** * * 推举在次加载控件。然后 */ protected abstract void findViews(View windowView); protected abstract void init(); private View windowView; public void buildWindow() { windowView = LayoutInflater.from(context).inflate(setResource(),null); popupWindow = new PopupWindow(windowView, setWidth(), setHeight()); // 使其聚集 popupWindow.setFocusable(true); // 设置允许在外点击消失 popupWindow.setOutsideTouchable(true); // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 popupWindow.setBackgroundDrawable(new BitmapDrawable()); findViews(windowView); init(); } public PopupWindow getWindow() { return popupWindow; } }