Skip to content

Latest commit

 

History

History
592 lines (439 loc) · 25.7 KB

File metadata and controls

592 lines (439 loc) · 25.7 KB

Chinese | English

Logo load failed

BasePopup - A powerful and convenient PopupWindow library for Android

Release Candy License Api Author


Compat library support lifecycle androidx
Release
Candy

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)

Attention!,If you use the androidX support library,please don't dependence the other two support library, otherwise it will conflict

	dependencies {

	        //BasePopup main library
	        implementation 'com.github.razerdp:BasePopup:{$latestVersion}'

                //Optional below
	        //BasePopup support lib for android.support(For PopupWindow show above the DialogFragment)
	        implementation 'com.github.razerdp:BasePopup-compat-support:{$latestVersion}'

	        //BasePopup support lib for lifecycle(auto dismiss and release on activity or fragment destroy)
	        implementation 'com.github.razerdp:BasePopup-compat-lifecycle:{$latestVersion}'

	        //BasePopup support lib for androidx(for the above two supported function in androidX versions)
	        implementation 'com.github.razerdp:BasePopup-compat-androidx:{$latestVersion}'

	        //candy version (preview version,frequent updates version)
		//implementation 'com.github.razerdp:BasePopup_Candy:{$latestVersion}'
		//implementation 'com.github.razerdp:BasePopup_Candy-compat-support:{$latestVersion}'
		//implementation 'com.github.razerdp:BasePopup_Candy-compat-lifecycle:{$latestVersion}'
		//implementation 'com.github.razerdp:BasePopup_Candy-compat-androidx:{$latestVersion}'
	}

Configuration

Blur Configuration

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

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

We recommend that you set the renderscriptTargetApi to the lowest API level that provides all the features you are using

defaultConfig {
        renderscriptTargetApi 18
        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)

  • 【Candy】2.2.2

    • 【Candy】190704
      • Fix the time issue of AnimatorSet fix #203
    • 【Candy】190722
      • Rollback to #188
      • After checking, [#188] (razerdp#188) fixes the problem, it is recommended to upgrade to this version.
  • 【Release】2.2.1(2019/06/24)

    • Support for showing popupwindow in Service or non-ActivityContext
    • Refactoring PopupUiUtils to optimize the screen width and height algorithm
    • Modify and optimize keyboard displacement logic
    • Optimize the determination of the click range in full screen state,fixed #200
  • 【Release】2.2.0(2019/05/15)

    • The release version 2.2.0 was upgraded, this version is a refactored version~
    • Optimize input method alignment logic
    • Refactoring blur implementation:
      • After testing, the 720p mobile phone has a full-screen blur time average between 6ms~16ms
      • Increase the blurriness of the default parameters
      • Blur progressive time follows BasePopup's animation time
      • Fix the situation of blur invalidation
    • Measurement/Layout:
      • Reconstruction measurement implementation:
        • Now in the case of clipToScreen, PopupDecor will be re-measured according to the remaining space to ensure the complete display of Popup. If you need to keep the original measured value, please call keepSize(true)
        • Refactor layout implementation, optimized for outSideTouch
        • Adapt screen rotation,fix #180
        • Use a flag instead of a variety of boolean
        • Reduce redundant code
    • Optimization:
      • Add the GravityMode parameter, now allows you to configure the reference mode of PopupGravity~
        • RELATIVE_TO_ANCHOR:Default mode,Use Anchor as a reference to specify the orientation of PopupWindow displayed in Anchor.
        • ALIGN_TO_ANCHOR_SIDE:Align mode,Specify the edge of the PopupWindow and which side of the Anchor is aligned with the side of the Anchor.
      • Add the minWidth()/minHeight() apis
      • Add the maxWidth()/maxHeight() apis.
      • Fix measurement differences for match_parent and wrap_content
      • Deprecated some apis:
        • setAllowDismissWhenTouchOutside -> setOutSideDismiss
        • setAllowInterceptTouchEvent -> setOutSideTouchable
      • Add setBackgroundView(View) api,Now the background control of BasePopup can be customized by you~ Of course, the background animation control method of PopupWindow still takes effect.
    • Function split:
      • Now BasePopup will split the package.
      • The source project will only adapt to the native Android without any dependencies.
      • If you need other adaptations, please rely on the following modules or modules:
        • If you need support for the support library, such as DialogFragment support, please implementation
          • implementation 'com.github.razerdp:BasePopup-compat-support:{$latestVersion}'
        • If you need support for the lifecycle library, such as automatic release or dismiss in destroy, etc., please implementation
          • implementation 'com.github.razerdp:BasePopup-compat-lifecycle:{$latestVersion}'
        • If you need support for the androidX library, please implementation
          • implementation 'com.github.razerdp:BasePopup-compat-androidx:{$latestVersion}'
        • Attention!,If you use the androidX support library,please don't dependence the other two support library, otherwise it will conflict
    • Bug fixed:
    • Other:
      • Add 996 license

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