Skip to content

Latest commit

 

History

History
543 lines (397 loc) · 23 KB

README_V2_EN.md

File metadata and controls

543 lines (397 loc) · 23 KB

Chinese | English

Logo load failed

BasePopup - A powerful and convenient PopupWindow library for Android

Release Candy License Api Author



apk demo download

Guide



Feature



Precautions

WARNING:

  • Please be sure to read this README carefully. Please check the update log for each version upgrade, which can reduce unnecessary detours for you.
  • Please pay attention on the dependence version, the Release version is a stable version, and Candy is a preview version.
    • Release version: Generally published to Release after repeated verification of the Candy version. If you have higher stability requirements, please use the Release version.
    • Candy version: new features, issue fixes will be published to the Candy version, Candy version is updated more frequently, but usually has new features, if you like to test new features and stability requirements are not high, please use the Candy version.
    • Switching between Release and Candy versions may cause Build to fail. At this time, you can clean Project.
  • If you are a previous 1.x user and want to update to 2.x now, please check before the update: 1.x migration to 2.x help documentation

Android P has been adapted, thanks to the method of @Guolei1130 collection.

Article address:android_p_no_sdkapi_support



Quick start


See more:Wiki#Usage

Dependence

Release Candy
Download Download

Add dependencies to Gradle (Please replace {$latestVersion} with the version shown in the Jcenter tab above)

	dependencies {
	        implementation 'com.github.razerdp:BasePopup:{$latestVersion}'

	        //candy version
	        //implementation 'com.github.razerdp:BasePopup_Candy:{$latestVersion}'
	}

Configuration

Blur Configuration

Support blur background from 1.9.0-alpha(Just call:setBlurBackgroundEnable(boolean)

RenderScript minimum support api 17 (lower case will use fastblur),you need to configure the following code in gradle

defaultConfig {
        renderscriptTargetApi 25
        renderscriptSupportModeEnabled true
    }

Common Usage

1.Create your popup xml file

Customize your PopupWindow layout just like you would normally customize a View layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/holo_blue_dark"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/tx_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="16dp"
        android:text="test1"
        android:textColor="@color/color_black1"/>

</LinearLayout>

2.Create popup class which extends BasePopupWindow

public class DemoPopup extends BasePopupWindow {
    public DemoPopup(Context context) {
        super(context);
    }

    @Override
    public View onCreateContentView() {
        return null;
    }
}

3.Complete abstract method

It is strongly recommended to use createPopupById() in onCreateContentView() to inflate view so that the library can correctly parse and adapt.

public class DemoPopup extends BasePopupWindow {
    public DemoPopup(Context context) {
        super(context);
    }

    // Must be implemented, return your contentView here
    // recommended to use createPopupById() for inflate
    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popup_normal);
    }

    // The following are optional codes (not required)
    // Return to the show and dismiss animations for PopupWindow. Basepopup provides several default animations, which can be freely implemented here.
    @Override
    protected Animation onCreateShowAnimation() {
        return getDefaultScaleAnimation(true);
    }

    @Override
    protected Animation onCreateDismissAnimation() {
        return getDefaultScaleAnimation(false);
    }
}

4.show!

There are three ways to show PopupWindow:showPopupWindow(),showPopupWindow(View anchor) and showPopupWindow(int x, int y)

new DemoPopup(getContext()).showPopupWindow();
//new DemoPopup(getContext()).showPopupWindow(v);
//new DemoPopup(getContext()).showPopupWindow(x,y);

These three methods have different meanings:

  • showPopupWindow():No-params method,At this point, the PopupWindow reference object is the screen (or the entire DecorView).Gravity behaves just like the Gravity in FrameLayout, indicating which position it is on the screen.
  • showPopupWindow(View anchor):Set an anchorView.At this point, the PopupWindow reference object is the incoming anchorView.The performance of Gravity means that this PopupWindow should be in the orientation of the target AnchorView.
  • showPopupWindow(int x, int y):Set the position for razerdp.basepopup,At this point PopupWindow will pop up at the specified location.

Suggestion:If PopupWindow needs to repeat the display or retain state, it is recommended to be used as a member variable instead of being created as a local variable each time.

For more apis on Gravity, check out:Wiki-Api:Gravity

Sample for showPopupWindow()

  • showPopupWindow()
gravity = CENTER
In the above example
xml specifies layout_gravity=center
gravity = RIGHT | CENTER_VERTICAL

  • showPopupWindow(View v)
gravity = CENTER
In the above example
xml specifies layout_gravity=center
gravity = RIGHT | CENTER_VERTICAL

  • showPopupWindow(int x, int y)
gravity = CENTER
In the above example
xml specifies layout_gravity=center


QuickPopupBuilder chained usage

QuickPopupBuilder supports chained calls to generate a PopupWindow based on QuickPopup.The Builder is designed to quickly build a simple PopupWindow that does not contain complex logic, such as the above case.Avoid creating too many BasePopupWindow implementation classes.

Sample


Attention:The PopupWindow animation in the default QuickPopupBuilder.QuickPopupConfig is zoomed out and disappears.

        QuickPopupBuilder.with(getContext())
                .contentView(R.layout.popup_normal)
                .config(new QuickPopupConfig()
                        .gravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL)
                        .withClick(R.id.tx_1, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(getContext(), "clicked", Toast.LENGTH_LONG).show();
                            }
                        }))
                .show();
		//.show(anchorView);
show() show(anchorView)


Api (see wiki)

See more in wiki (continuous improvement)

Link👉WIKI


Update log (Historical update)

  • 【Release】2.1.9(2019/03/07)

    • Please see candy updateLog on under
  • 【Candy】2.1.9

    • 【Candy】2.1.9-prerelease(2019/03/07)
      • Support layout in cutouts for Android P,fixed #154
    • 【Candy】2.1.9-beta3(2019/03/1)
    • 【Candy】2.1.9-beta1(2019/02/28)
      • optimized code,fix override AnimationListener for dismissAnimation
    • 【Candy】2.1.9-beta(2019/2/26)
      • add oom catcher for BlurHelper
    • 【Candy】2.1.9-alpha4(2019/2/21)
      • optimized background blur
    • 【Candy】2.1.9-alpha3(2019/2/21)
      • fixed alpha2
    • 【Candy】2.1.9-alpha2(2019/2/19)
      • Removed from lib's AndroidManifest,fixed #149
    • 【Candy】2.1.9-alpha1(2019/02/18)
      • Adapted DialogFragment,fixed #145
  • 【Release】2.1.8(2019/01/26)

    • This version update adds a lot of new features.
    • Update details:
      • Adapted to use ImmersionBar
      • Repair of horizontal screen incompatibility problems
      • Fix the problem that the constructor width is not valid
      • Support for background masks without intercepting events. That black technology has changed to a more friendly way!
      • Fix the problem that the virtual button is displayed together when the popup is displayed.
      • QuickPopupConfig adds dismissOnOutSideTouch() method
      • Optimize QuickPopupBuilder, see more inWiki
      • Optimize for problems with #138
      • Fix the call order of setAlignBackgroundGravity() and setAlignBackground()
  • 【Candy】2.1.8

    • 【Candy】2.1.8-prerelease2(2019/01/24)
    • 【Candy】2.1.8-prerelease(2019/01/23)
      • Repair of horizontal screen incompatibility problems
    • 【Candy】2.1.8-beta7(2019/01/22)
      • Beta3 and beta4 and beta5 and beta6 were deleted~
      • Fix beta2's problem with focusable, remove useless code
      • Fix the problem that the constructor width is not valid
      • Support for background masks without intercepting events. That black technology has changed to a more friendly way!
    • 【Candy】2.1.8-beta2(2019/01/22)
      • Fix the problem that the virtual button is displayed together when the popup is displayed.This function is currently tested, please be sure to feedback to candy if you have any questions.
      • QuickPopup adds dismissOnOutSideTouch() method
    • 【Candy】2.1.8-beta1(2019/01/21)
      • Fix the problem that the virtual button is displayed together when the popup is displayed.This function is currently tested, please be sure to feedback to candy if you have any questions.
    • 【Candy】2.1.8-alpha2(2019/01/18)
      • Optimize QuickPopupBuilder, see more inWiki
    • 【Candy】2.1.8-alpha(2019/01/17)
      • Optimize for problems with #138
      • Fix the call order of setAlignBackgroundGravity() and setAlignBackground()
  • 【Release】2.1.7(2019/01/16)

    • Fixed an issue where onAnchorTop() or onAnchorBottom() was called multiple times during setAutoLocatePopup(true)
    • Fixing 'setAllowInterceptTouchEvent(false)`, the problem of not being able to locate anchorView due to default restrictions
    • Optimize the soft keyboard default offset calculation method
    • Optimize keyboard height calculation method
    • Thanks to [@ParfoisMeng] (https://github.com/ParfoisMeng) for finding the soft keyboard offset problem and submitting [PR#130] (https://github.com/razerdp/ BasePopup/pull/130)
  • 【Candy】2.1.7-beta(2019/01/10~2019/01/13)

    • Fixing 'setAllowInterceptTouchEvent(false)`, the problem of not being able to locate anchorView due to default restrictions
    • Optimize the soft keyboard default offset calculation method
    • Optimize keyboard height calculation method

Demo preview

GravityPopupFrag LocatePopupFrag
AnyPosPopupFrag UpdatePopupFrag
BlurSlideFromBottomPopupFrag CommentPopup
SlideFromBottomPopup InputPopup
ListPopup MenuPopup

Coffee me

Wechat Ali-pay

Q&A

More Q&A:WIKI#Q&A

Q:How to cancel the default background color

A:Call setBackgroundColor(Color.TRANSPARENT) or [setBackground](https://github.com /razerdp/BasePopup/wiki/API#setbackgroundint-drawableids)(0)




Q:How to perform no animation when dismiss()

A:Call dismiss(false) or dismissWithOutAnimate()




Q:How to prevent popupwindow from dismissing when click on the popupwindow background

A:Call setAllowDismissWhenTouchOutside(false)




Q:Why can't I pop up in the Service?

A:PopupWindow needs windowToken, so the ApplicationContext or Service can't be popped up. It is recommended to pop the popupwindow by event notification to the top of the stack.




Q:Why is the EditText inside PopupWindow not available for pasting?

ISSUE REF:#140

Google Issue Tracker:#36984016

A:The View in PopupWindow can't get WindowToken, and the paste function is also a PopupWindow. Its display must require WindowToken, so it can't be pasted.




Q:How to prevent PopupWindow for overlaying the status bar

A:Set setPopupWindowFullScreen(false)




Q:How to prevent dismiss under backpress

A:Set setBackPressEnable(false)




Q:The difference between the root layout height match_parent and wrap_content

A:When the root layout is match_parent, razerdp.basepopup will do some difference handling.
When you set setClipToScreen(true), if your root layout is match_parent, then it means The maximum height of your layout is the screen height. If your root layout is wrap_content, the maximum height may be higher than the screen height.
Such as full screen listview in demo

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"   // 请留意这里
    android:background="@android:color/white"
    >

    <ListView
        android:id="@+id/popup_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/line_bg"
        android:dividerHeight="0.5dp"
        android:scrollbars="vertical"
        />
</RelativeLayout>
layout_height = match_parent layout_height = wrap_content


Pay attention to the difference between the bottom of the listview of the two images, where the bottom of the wrap_content has exceeded the bottom of the screen and cannot be displayed completely.




License

Apache-2.0