GithubHelp home page GithubHelp logo

zyyoona7 / easypopup Goto Github PK

View Code? Open in Web Editor NEW
1.5K 34.0 214.0 1.34 MB

「暂停维护」PopupWindow Wrapper. 对 PopupWindow 的封装。可指定相对于 anchor view 各个方位弹出,设置背景变暗,指定 ViewGroup 背景变暗等特性。

License: Apache License 2.0

Java 98.57% Kotlin 1.43%
android popupwindow relative-anchorview background-dark

easypopup's Introduction

EasyPopup「暂停维护」

PopupWindow

对 PopupWindow 的封装,使得在项目中使用起来更加简单、方便、快捷

项目特性

  • 链式调用:除了在传统的 PopupWindow 使用方法之外还加入了更多的方法
  • 带有相对于 AnchorView 的各个方位弹出的方法,弹出 PopupWindow 更轻松、更简单
  • 支持 PopupWindow 弹出时背景变暗、指定 ViewGroup 背景变暗、设置变暗颜色等 (API>=18)
  • 加入了简单的生命周期方法,自定义 PopupWindow、处理逻辑更方便、更清晰

效果图

EasyPopup

仓库依赖

Step 1. Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
	//...
	maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    compile 'com.github.zyyoona7:EasyPopup:VERSION_CODE'
}

最新的VERSION_CODE

使用

1. 基本使用

创建 EasyPopup 对象

可以调用 setXxx() 方法进行属性设置,最后调用 createPopup() 方法实现对PopupWindow的初始化。

private EasyPopup mCirclePop;
mCirclePop = EasyPopup.create()
        .setContentView(this, R.layout.layout_circle_comment)
        .setAnimationStyle(R.style.RightPopAnim)
  	//是否允许点击PopupWindow之外的地方消失
        .setFocusAndOutsideEnable(true)
        .apply();

初始化 View

可以调用 findViewById() 方法来获取 View 对象。

TextView tvZan=mCirclePop.findViewById(R.id.tv_zan);
TextView tvComment=mCirclePop.findViewById(R.id.tv_comment);
tvZan.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ToastUtils.showShort("赞");
        mCirclePop.dismiss();
    }
});

tvComment.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ToastUtils.showShort("评论");
        mCirclePop.dismiss();
    }
});

显示

相对于 view 位置显示

/**
 * 相对anchor view显示,适用 宽高不为match_parent
 *
 * @param anchor
 * @param yGravity  垂直方向的对齐方式
 * @param xGravity  水平方向的对齐方式
 * @param x            水平方向的偏移
 * @param y            垂直方向的偏移
 */
mCirclePop.showAtAnchorView(view, YGravity.CENTER, XGravity.LEFT, 0, 0);

除了 showAtAnchorView() 方法,内部还保留了 showAsDropDown()、showAtLocation() 方法。

注意:如果使用 YGravity 和 XGravity 时,请确保使用之后 PopupWindow 没有超出屏幕边界,如果超出屏幕边界,YGravity 和 XGravity 可能无效,从而达不到你想要的效果。#4

方位注解介绍

垂直方向对齐:YGravity

YGravity.CENTER,//垂直居中
YGravity.ABOVE,//anchor view之上
YGravity.BELOW,//anchor view之下
YGravity.ALIGN_TOP,//与anchor view顶部对齐
YGravity.ALIGN_BOTTOM,//anchor view底部对齐

水平方向对齐:XGravity

XGravity.CENTER,//水平居中
XGravity.LEFT,//anchor view左侧
XGravity.RIGHT,//anchor view右侧
XGravity.ALIGN_LEFT,//与anchor view左边对齐
XGravity.ALIGN_RIGHT,//与anchor view右边对齐

2. 弹出 PopupWindow 并伴随背景变暗

mCirclePop = EasyPopup.create()
        .setContentView(this, R.layout.layout_circle_comment)
        .setAnimationStyle(R.style.RightPopAnim)
  	//是否允许点击PopupWindow之外的地方消失
        .setFocusAndOutsideEnable(true)
  	//允许背景变暗
  	.setBackgroundDimEnable(true)
  	//变暗的透明度(0-1),0为完全透明
        .setDimValue(0.4f)
  	//变暗的背景颜色
  	.setDimColor(Color.YELLOW)
  	//指定任意 ViewGroup 背景变暗
  	.setDimView(viewGroup)
        .apply();

备注:背景变暗效果只支持 4.2 以上的版本。

3. 点击 PopupWindow 之外的地方不让其消失

mCirclePop = EasyPopup.create()
        .setContentView(this, R.layout.layout_circle_comment)
        .setAnimationStyle(R.style.RightPopAnim)
  	//是否允许点击PopupWindow之外的地方消失,
  	//设置为false点击之外的地方不会消失,但是会响应返回按钮事件
        .setFocusAndOutsideEnable(false)
        .apply();

4. 自定义 PopupWindow

EasyPopup中自定义了三个生命周期:

  • onPopupWindowCreated():PopupWindow 对象初始化之后调用
  • onPopupWindowViewCreated(View contentView):PopupWindow 设置完 contentView 和宽高之后调用
  • onPopupWindowDismiss():PopupWindow dismiss 时调用

自定义 PopupWindow 需继承 BasePopup 抽象类,实现内部的两个抽象方法:

  • initAttributes():可以在此方法中设置 PopupWindow 需要的属性,该方法在 onPopupWindowCreated() 中调用
  • initViews():在此方法中初始化 view,该方法在 onPopupWindowViewCreated(View contentView) 中调用

示例

public class ComplexPopup extends BasePopup<ComplexPopup> {
    private static final String TAG = "ComplexPopup";

    private Button mOkBtn;
    private Button mCancelBtn;

    public static ComplexPopup create(Context context){
        return new ComplexPopup(context);
    }

    protected ComplexPopup(Context context) {
        setContext(context);
    }


    @Override
    protected void initAttributes() {
        setContentView(R.layout.layout_complex, ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(300));
        setFocusAndOutsideEnable(false)
                .setBackgroundDimEnable(true)
                .setDimValue(0.5f);
		//setXxx() 方法
    }

    @Override
    protected void initViews(View view) {
        mOkBtn = findViewById(R.id.btn_ok);
        mCancelBtn = findViewById(R.id.btn_cancel);

        mOkBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mCancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}
mComplexPopup = ComplexPopup.create(this)
	   .setDimView(mComplexBgDimView)
           .createPopup();

5. 其他方法介绍

方法名 作用 备注
setContentView(View contentView) 设置 contentView
setContentView(Context context, @LayoutRes int layoutId) 设置 contentView
setWidth(int width) 设置宽
setHeight(int height) 设置高
setAnchorView(View view) 设置目标 view
setYGravity(@YGravity int yGravity) 设置垂直方向对齐
setXGravity(@XGravity int xGravity) 设置水平方向对齐
setOffsetX(int offsetX) 设置水平偏移
setOffsetY(int offsetY) 设置垂直
setAnimationStyle(@StyleRes int animationStyle) 设置动画风格
getContentView() 获取PopupWindow中加载的view @Nullable
getContext() 获取context @Nullable
getPopupWindow() 获取PopupWindow对象 @Nullable
dismiss() 消失

6.版本迁移

在最新的 1.1.0 版本中对代码结构进行了跳转,在之前的基础上优化了泛型的继承,使得链式调用更加的顺畅;另外对 EasyPopup 继承使用也做了优化;对部分方法的命名也做了调整。

i.继承使用修改、命名修改

  • 自定义 PopupWindow 时由原来的继承 BaseCustomPopup 改为继承 BasePopup (具体使用请查看demo)。
  • 将原来的 createPopup() 方法更名为 apply() 方法,新版中 apply() 方法不强制调用,在 showXxx() 方法中会检查,如果忘了调用 apply() 方法会主动调用一次。
  • 将原来的 VerticalGravity、HorizontalGravity 注解更名为 YGravity、XGravity 精简了许多。
  • 将原来的 getView() 方法更名为 findViewById()。

ii.其他用法调整

  • 无论是自定义 PopupWindow 还是调用 EasyPopup 现在在构造方法中不在强制传入 Context 对象了,因为只有在设置 contentView 时传入了 layoutRes 才需要 Context 对象。如果你设置布局的方式是上述方式则需要手动设置 Context 对象:setContext(Context context)/setContentView(Context context, @LayoutRes int layoutId) 方法。
  • 直接使用 EasyPopup 时提供了静态方法 create()/create(Context context) 方法创建对象,这样用起来比较酷。
  • 加入了更多的方法,欢迎阅读源码。

TODO

感谢

RelativePopupWindow
CustomPopwindow
android-simple-tooltip
EasyDialog
Android弹窗_PopupWindow详解

License

Copyright 2017 zyyoona7

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

easypopup's People

Contributors

xuqk avatar zyyoona7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

easypopup's Issues

有一个疑问

mQQPop = EasyPopup.create()
.setContext(this)
.setContentView(R.layout.layout_right_pop)
.setAnimationStyle(R.style.RightTop2PopAnim)
.setOnViewListener(new EasyPopup.OnViewListener() {
@OverRide
public void initViews(View view) {
// View arrowView = mQQPop.findViewById(R.id.v_arrow);
// arrowView.setBackground(new TriangleDrawable(TriangleDrawable.TOP, Color.parseColor("#404040")));

                }
            })
            .setFocusAndOutsideEnable(true)
            .apply();
    **View arrowView = mQQPop.findViewById(R.id.v_arrow);
    arrowView.setBackground(new TriangleDrawable(TriangleDrawable.TOP, Color.parseColor("#404040")));**

定义的View写在initView()里面,和外面有啥区别?

向上展示recyclerview高度无法包裹的问题

您好,我在使用的时候发现设置成向上展示时,列表中的item不管多少popupwindow的高度都不改变,但是我的item设置的高度为wrap,只有设置recyclerview的高度才能够改变这个高度

点击外部按钮无法响应事件

EasyPopup(this)
.setContentView(R.layout.popup_test)
.setFocusAndOutsideEnable(false)
.showAsDropDown(it)
布局是一个很简单的TextView

键盘遮挡控件问题

如果PopupWindow布局有EditText,我在你封装的控件中添加了setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)发现并没有用,有什么解决方案吗?

想请教一个问题

popup里有个RecyclerView,我动态删减item,popup能自动改变高度吗?(比如饿了麽商家界面购物车)

AS3.0.1版本引入库,运行的时候报合并错误

Error:Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.

java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex

重复类名

lib和lib-kt 里面发生了类名冲突,
有两个 com.zyyoona7.lib.BuildConfig

可以加下qq吗? 现在显示有点问题不明白

mBgDimPop.showAtLocation(view,Gravity.BOTTOM,0,0);用这个是正常显示

mBgDimPop.showAtAnchorView(view, YGravity.ALIGN_BOTTOM, 0, 0, 0); 用这个只会显示一半另一半跑到屏幕下边外面去了

有两个按钮 一个按钮显示一个popwindow,点击一个按钮,

有两个按钮 一个按钮显示一个popwindow,点击一个按钮,显示出来一个popwindow,再点击另外一个按钮,希望关闭当前显示的popwindow,显示另外一个popwindow出来,但是我发现你的popwindow是全屏的,点击布局以外的默认是关闭当前的,有什么方法能解决吗

隐藏键盘

com/zyyoona7/easypopup/easypop/EasyPopActivity.java:314
com/zyyoona7/easypopup/easypop/EasyPopActivity.java:303

用这句代码才能隐藏键盘:
KeyboardUtils.hideSoftInput(v);

请教一个问题

就是pop在recyclerview的item的一个view点击触发之后位于该view的下方弹出.有时候会出现这么个情况,就是会被该item遮挡住,请问这是什么引起的?

窗口高度为奇数时,最底部1px高度忽隐忽现

假如窗口高度为奇数时,最底部1px高度忽隐忽现,描边1px看更明显。窗口高度刚好是偶数的时候是正常的,有动态数据时,一个笨方法是计算设置偶数高度,希望有更好的解决方法

popup位置乱跳。

有一个可以滑动的recyview,滑动未停止时,去点击可以弹出的按钮,popu位置乱跳,不能在指定位置弹出。
secondMenuPopup.showAtAnchorView(view, VerticalGravity.CENTER, HorizontalGravity.RIGHT, 0, 0);

库是不是限制了support最低版本是27.1.1?

我的项目support版本是26.1.0,引入库不是提示support版本要求27.1.1

All com.android.support libraries must use the exact same version specification (mixing versions can lead to runtime crashes). Found versions 27.1.1, 26.1.0. Examples include com.android.support:support-compat:27.1.1 and com.android.support:animated-vector-drawable:26.1.0 more... (Ctrl+F1)

就是编译通过运行报错
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
我查过这个错误通常是引入了两个版本不同的support库导致的。
请问有什么办法可以解决?因为我的项目support库升级的话涉及很多的类,影响太大,所以暂时不能升级,希望有其他版本解决这个问题。

运行不起来

E:\CodeTest\EasyPopup\app\src\main\java\com\zyyoona7\easypopup\kt\KotlinActivity.kt
Error:(15, 13) Unresolved reference: layoutId
Error:Execution failed for task ':app:compileDebugKotlin'.

Compilation error. See log for more details

软件盘冲突

在有软件盘弹出的情况下,再弹出pop,位置改变

java.lang.OutOfMemoryError

Failed to allocate a 1973732 byte allocation with 358096 free bytes and 349KB until OOM
com.zyyoona7.popup.BasePopup.initContentViewAndWH(BasePopup.java:139)

显示不全

当recycleview的item(最后一条或第一条item)条目只露出一点点的时候,如果设置在右边(或左边)显示的话popup的布局会被挤压,变成窄窄的一条。这个bug如何解决!需要多点击几次会把item带上去,然后才能显示全.(注:recycleview顶到屏幕最下边或者最上边时)

It crashed on call showAtAnchorView().

It call android.view.ContextThemeWrapper cannot be cast to android.app.Activity.

My source:
image

        EasyPopup(textView.context)
                .setContentView<EasyPopup>(R.layout.popup_vehicle_picker)
                //是否允许点击PopupWindow之外的地方消失
                .setFocusAndOutsideEnable<EasyPopup>(true)
                //允许背景变暗
                .setBackgroundDimEnable<EasyPopup>(true)
                //变暗的透明度(0-1),0为完全透明
                .setDimValue<EasyPopup>(0.4f)
                .createPopup<EasyPopup>()
                .showAtAnchorView(textView, VerticalGravity.BELOW, HorizontalGravity.CENTER, 0, 0)

Log:

04-03 16:01:14.574 32057-32057/? E/MtaSDK: java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity
        at com.zyyoona7.lib.EasyPopup.handleBackgroundDim(EasyPopup.java:607)
        at com.zyyoona7.lib.EasyPopup.showAtAnchorView(EasyPopup.java:452)
        at com.lxt.app.ui.maink6.helper.VehiclePickerHelper.popupVehiclePicker(VehiclePickerHelper.kt:35)

It happened at here:
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.