轻松Done Android 引导用户操作的浮层视图

2018-09-10  本文已影响0人  只取一勺

1.需要实现如下图的效果


2.不废话啦,直接撸代码吧,😄


1)FloatingLayerView.java

public class FloatingLayerView extends FrameLayout {

    private Context mContext;
    private LayoutInflater inflater;

    public FloatingLayerView(Context context) {
        this(context, null);
    }

    public FloatingLayerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatingLayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化
     * @param context
     */
    private void init(Context context) {
        this.mContext = context;
        this.inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * 设置内容视图View
     *
     * @param layoutResID
     */
    public void setContentView(int layoutResID) {
        if (layoutResID < 0) {
            return;
        }
        View view = inflater.inflate(layoutResID, null);
        removeAllViews();
        addView(view);
    }
}

2)FloatingLayerViewCreator.java

public class FloatingLayerViewCreator {

    private Activity mActivity;
    private FrameLayout mParentView;
    private FloatingLayerView mFloatView;
    private OnFloatStateListener onFloatStateListener;

    /**
     * 创建实例
     * <p>Create instances</p>
     *
     * @param activity
     * @return
     */
    public static FloatingLayerViewCreator create(Activity activity) {
        return new FloatingLayerViewCreator(activity);
    }

    /**
     * 默认的构造函数(Constructor)
     *
     * @param activity
     */
    private FloatingLayerViewCreator(Activity activity) {
        if (activity == null || isHasFloatView()) {
            return;
        }
        mActivity = activity;
        mParentView = (FrameLayout) mActivity.getWindow().getDecorView();
        if (mFloatView == null) {
            mFloatView = new FloatingLayerView(mActivity);
        }

        //设置触摸监听
        mFloatView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                close();
                return true;
            }
        });

    }

    /**
     * 设置浮层显示内容
     *
     * @param layoutId
     * @return
     */
    public FloatingLayerViewCreator setContentView(int layoutId) {
        if (mFloatView != null) {
            int paddingTop = DisplayUnitManager.getStatusBarHeight(mActivity);
            mFloatView.setPadding(0, paddingTop, 0, 0);
            mFloatView.setContentView(layoutId);
        }
        return this;
    }

    /**
     * 显示浮层
     */
    public void show() {
        if (isHasFloatView()) {
            return;
        }
        if (mParentView != null) {
            mParentView.addView(mFloatView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
        }

        if (onFloatStateListener != null) {
            onFloatStateListener.onShow();
        }
    }

    /***
     * 是否含有FloatView
     * @return
     */
    private boolean isHasFloatView() {
        if (mParentView == null) {
            return false;
        }
        for (int i = 0; i < mParentView.getChildCount(); i++) {
            View view = mParentView.getChildAt(i);
            if (view instanceof FloatingLayerView) {
                return true;
            }
        }
        return false;
    }

    /**
     * 关闭浮层
     */
    public void close() {

        if (mFloatView != null && mParentView != null) {
            mParentView.removeView(mFloatView);
            if (onFloatStateListener != null) {
                onFloatStateListener.onClose();
            }
        }
    }

    /**
     * 设置浮层显示监听
     *
     * @param onFloatStateListener
     */
    public FloatingLayerViewCreator setOnFloatStateListener(OnFloatStateListener onFloatStateListener) {
        this.onFloatStateListener = onFloatStateListener;
        return this;
    }

    /**
     * 浮层显示监听
     */
    public interface OnFloatStateListener {
        /***
         * 浮层显示
         */
        void onShow();

        /***
         * 浮层关闭
         */
        void onClose();
    }
}

3.使用

//显示提示用户操作的浮层
FloatingLayerViewCreator.create(XXX.this)
        .setContentView(R.layout.xxx)
        .setOnFloatStateListener(new FloatingLayerViewCreator.OnFloatStateListener() {
            @Override
            public void onShow() {
                //do nothing
            }

            @Override
            public void onClose() {

            }
        }).show();
上一篇下一篇

猜你喜欢

热点阅读