工具类-BasePopupWindow

我们在工作中肯定需要用到许多工具类下面我们介绍的是popWindow的Base类
代码如下

@SuppressLint("UseSparseArrays")
public abstract class BasePopupWindow implements OnClickListener {
    protected PopupWindow mPop;
    protected View mContentView;
    protected Activity mAct;
    protected int mWidth = -2;
    protected int mHeight = -2;
    private String mTag;
    protected boolean mDismiss = true;
    protected Map<Integer, Integer> mViewDrawableSelectCache;
    protected boolean openAnimation;

    public BasePopupWindow(Activity act, int width, int height) {//宽高充满屏幕
        mAct = act;
        mWidth = width;
        mHeight = height;
        init();
    }


    private void init() {
        onCreate();
        findViews();
        setListenerNative();
        initViewDrawableSelectCache();
        loadData();
        initPopAnimationStyle();
    }

    protected void initPopAnimationStyle() {
        if (openAnimation) {
            mPop.setAnimationStyle(R.style.listPopAnimation);//设置弹窗出现的动画效果
        }
    }

    private void setListenerNative() {
        mPop.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mAct instanceof ICoveringLayer) {
                    ICoveringLayer layout = (ICoveringLayer) mAct;
                    // 影藏activity的覆盖层
                    layout.hideCoveringLayer();
                }
            }
        });
        setListener();
    }

    protected void loadData() {
    }

    protected abstract void setListener();


    protected abstract void findViews();

    protected void onCreate() {
        mViewDrawableSelectCache = new HashMap<Integer, Integer>();
        PopupWindow pop = mPop = createPopWindow();
        mPop.setAnimationStyle(R.style.listPopAnimation);// 设置窗口显示的动画效果
        pop.setBackgroundDrawable(new ColorDrawable(0));// 点击窗口外消失
        pop.setFocusable(true);
        pop.setTouchable(true);
        pop.setOutsideTouchable(true);// 点击窗口外消失,需要设置背景、焦点、touchable、update
    }

    protected PopupWindow createPopWindow() {
        if (mContentView == null) {
            int id = getContentViewLayoutId();
            mContentView = inflate(id, null, true);
        }
        return new PopupWindow(mContentView, mWidth, mHeight);
    }

    @SuppressWarnings("unchecked")
    protected <T> T findViewById(int id) {
        return (T) mContentView.findViewById(id);
    }

    protected abstract int getContentViewLayoutId();

    protected ViewGroup inflate(int id, ViewGroup root, boolean attachToRoot) {
        return (ViewGroup) mAct.getLayoutInflater().inflate(id, root, attachToRoot);
    }

    @SuppressWarnings("unused")
    private String getTag() {
        if (TextUtils.isEmpty(mTag)) {
            mTag = getClass().getCanonicalName();
        }
        return mTag;
    }

    public View getContentView() {
        return mContentView;
    }

    public boolean isShow() {
        return mPop.isShowing();
    }

    /**
     * 初始化view在点击时的图片集合 key view的id value view所对应被点击时的背景图片
     *
     * @return
     */
    protected abstract void initViewDrawableSelectCache();

    @Override
    public void onClick(View v) {
        Integer resid = mViewDrawableSelectCache.get(v.getId());
        if (resid != null) {
            v.setBackgroundResource(resid);
        } else {
            int color = getDefaultBackgroundColor();
            if (color != -1) {
                v.setBackgroundColor(color);
            }
        }
        onItemClick(v);
    }

    protected int getDefaultBackgroundColor() {
        return -1;
    }

    private void onItemClick(final View v) {
        v.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mDismiss) {
                    mPop.dismiss();
                }
                onItemClick(v, mPop);
            }
        }, 100);
    }

    protected abstract void onItemClick(View view, PopupWindow pop);

    public void show() {
        onShowPre();
        ShowLocation location = getShowLocation();
        if (mAct instanceof ICoveringLayer) {
            ICoveringLayer layout = (ICoveringLayer) mAct;
            // 显示activity的覆盖层(遮盖)
            layout.showCoveringLayer();
        }
        location.parent = location.parent == null ? mAct.getWindow().getDecorView() : location.parent;
        PopupWindow pop = mPop;
        pop.showAtLocation(location.parent, location.gravity, location.x, location.y);
        pop.setFocusable(false);
        pop.update();
        location.parent.post(new Runnable() {

            @Override
            public void run() {
                onShowPost();
            }
        });
    }


    protected void startShowAnimition() {
    }

    public void dismiss() {
        startDismissAnimationAndDismiss();
    }

    protected void startDismissAnimationAndDismiss() {
        mPop.dismiss();
    }

    /**
     * PopupWindow显示之前调用
     */
    protected void onShowPre() {
    }

    /**
     * PopupWindow显示之后调用
     */
    protected void onShowPost() {
        startShowAnimition();
    }

    protected ShowLocation getShowLocation() {
        return new ShowLocation(mAct.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

    protected class ShowLocation {
        public int x;
        public int y;
        public int gravity = Gravity.NO_GRAVITY;
        public View parent;

        public ShowLocation(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public ShowLocation(int gravity, int x, int y) {
            this(x, y);
            this.gravity = gravity;
        }

        public ShowLocation(View parent, int gravity, int x, int y) {
            this(gravity, x, y);
            this.parent = parent;
        }

    }

    public void setAnimationStyle(int animationStyle){
        mPop.setAnimationStyle(animationStyle);
    }

}

之后我们需要添加开始出去的动画

<!-- popWindow 动画 -->
    <style name="listPopAnimation">
        <item name="android:windowEnterAnimation">@anim/tran_start_pop_in</item>
        <item name="android:windowExitAnimation">@anim/tran_finish_pop_out</item>
    </style>

开始动画

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:duration="20"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="1.0" />
</set>

结束动画

<set xmlns:android="http://schemas.android.com/apk/res/android">

        <alpha
            android:duration="20"
            android:fromAlpha="1.0"
            android:interpolator="@android:anim/decelerate_interpolator"
            android:toAlpha="0.0" />

</set>

基本工具类就完成了下面是具体子类实现

public class AlertPopuWindow extends BasePopupWindow {

    private LinearLayout delete_pop_layout;
    private TextView queding_text;
    private TextView title_text;
    private String title;


    public AlertPopuWindow(Activity act) {
        this(act ,null);
    }

    public AlertPopuWindow(Activity act, String title) {
        super(act, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        this.title = title;
    }

    @Override
    protected void setListener() {
        delete_pop_layout.setOnClickListener(this);
        queding_text.setOnClickListener(this);
        title_text.setOnClickListener(new MyClickListener());
    }

    @Override
    protected void findViews() {
        delete_pop_layout = (LinearLayout) findViewById(R.id.delete_pop_layout);
        title_text = (TextView) findViewById(R.id.title_text);
        queding_text = (TextView) findViewById(R.id.queding_text);
    }
    @Override
    protected int getContentViewLayoutId() {
        return R.layout.popu_warn_item;
    }

    @Override
    protected void initViewDrawableSelectCache() {
    }
    @Override
    protected void onShowPre() {
        super.onShowPre();
        title_text.setText(title);
    }

    @Override
    public boolean isShow() {
        return super.isShow();
    }

    @Override
    protected void onItemClick(View view, PopupWindow pop) {
        switch (view.getId()) {
            case R.id.delete_pop_layout:
                Log.e("点击了布局", "1111111");
                break;
            case R.id.queding_text:
                Log.e("点击了确定按钮","1111111");
                break;
        }
    }

    class MyClickListener implements View.OnClickListener{//

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.title_text:
                    Log.e("点击了标题","1111111");
                    break;
            }
        }
    }
}

子类界面代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/delete_pop_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#40000000"
    android:gravity="center"
    android:layout_gravity="center"
    android:orientation="vertical" >

        <TextView
            android:id="@+id/title_text"
            android:background="@drawable/popu_background"
            android:paddingLeft="21dp"
            android:paddingRight="27px"
            android:paddingTop="27px"
            android:paddingBottom="27px"
            android:layout_width="450px"
            android:layout_height="wrap_content"
            android:textSize="27px"
            android:text="该牛为公牛不能进行妊时光说按时发生发上的方法反反复复反复发的说法是否问问问大水发暗室逢灯"
            android:textColor="#333333" />


            <TextView
                android:id="@+id/queding_text"
                android:textSize="24px"
                android:layout_width="450px"
                android:layout_height="72px"
                android:layout_marginLeft="0.5dp"
                android:background="@drawable/popu_background"
                android:gravity="center"
                android:text="确定"
                android:textColor="#0089e1" />

</LinearLayout>

ok最后一步就是调用了代码如下

   new AlertPopuWindow(MainActivity.this,"哈哈").show();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值