Android kotlin 第三方项目开源弹窗库 - XPopup

一、实现从底部自定义像Activity一样的全屏弹窗的功能

实现:

主工程项目要导入的第三方项目library,如下图:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
1、CustomFullScreenPopup.kt

class CustomFullScreenPopup(context: Context) : FullScreenPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.custom_fullscreen_popup
    }

    override fun onShow() {
        super.onShow()
        Log.e("tag", "CustomFullScreenPopup onShow")
    }

    override fun onDismiss() {
        super.onDismiss()
        Log.e("tag", "CustomFullScreenPopup onDismiss")
    }
}

custom_fullscreen_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#fff"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="0dp">

        <TextView
            android:layout_marginTop="40dp"
            android:textSize="20sp"
            android:textColor="#111"
            android:text="全屏弹窗"
            android:gravity="center"
            android:padding="15dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="我是全屏弹窗,可以设置任意的动画器。我看起来就像一个从底部弹出的Activity,可以用来做一些登录,选择界面。"
            android:textColor="#333"
            android:textSize="18sp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:hint="测试软键盘交互" />

        <View
            android:background="#ccc"
            android:layout_width="match_parent"
            android:layout_height="300dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:hint="测试软键盘交互" />

        <View
            android:background="#9C27B0"
            android:layout_width="match_parent"
            android:layout_height="150dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="85dp"
            android:hint="测试软键盘交互" />
        <View
            android:background="#AC5959"
            android:layout_width="match_parent"
            android:layout_height="150dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="85dp"
            android:hint="测试软键盘交互55" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

2、MainActivity.kt

class MainActivity : AppCompatActivity() {
    var popupView: BasePopupView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnFullScreenPopup.setOnClickListener { //全屏弹窗,看起来像Activity
            popupView = CustomFullScreenPopup(this@MainActivity)
            XPopup.Builder(this@MainActivity)
                .isLightStatusBar(true)
                .autoOpenSoftInput(true)
                .asCustom(popupView)
                .show()
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
        android:id="@+id/btnFullScreenPopup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="自定义全屏弹窗"
        android:textAllCaps="false" />
</LinearLayout>

主工程的build.gradle中增加:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

二、实现从底部弹出带手势拖拽的列表弹窗的功能

实现:

主工程项目要导入的第三方项目library,如下图:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnShowBottomList.setOnClickListener { //从底部弹出,带手势拖拽的列表弹窗
            XPopup.Builder(this@MainActivity)
                .isDarkTheme(true)
                .hasShadowBg(true)
                .moveUpToKeyboard(false)
                .isCoverSoftInput(true) //
                .asBottomList(
                    "请选择一项",
                    arrayOf(
                        "条目1",
                        "条目2",
                        "条目3",
                        "条目4",
                        "条目5",
                        "条目6",
                        "条目7"
                    )
                ) { position, text ->
                    Toast.makeText(this@MainActivity, "click $text", Toast.LENGTH_SHORT).show()
                }.show()
        }
    }
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

三、实现从底部弹出带手势拖拽的列表弹窗(带选中效果)的功能

实现:

主工程项目要导入的第三方项目library,如下图:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnShowBottomListWithCheck.setOnClickListener { //从底部弹出,带手势拖拽的列表弹窗,带选中效果
            XPopup.Builder(this@MainActivity)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
                .asBottomList(
                    "标题可以没有",
                    arrayOf(
                        "条目1",
                        "条目2",
                        "条目3",
                        "条目4",
                        "条目5"
                    ),
                    null, 2
                ) { position, text ->
                    Toast.makeText(this@MainActivity, "click $text", Toast.LENGTH_SHORT).show()
                }.show()
        }
    }
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

四、实现在Activity中间弹出的List列表弹窗的功能

实现:

主工程项目要导入的第三方项目library,如下图:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnShowCenterList.setOnClickListener {//在中间弹出的List列表弹窗
            XPopup.Builder(this@MainActivity)
                //.maxWidth(600)
                .maxHeight(800)
                .isDarkTheme(true)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
                .asCenterList(
                    "请选择一项",
                    arrayOf(
                        "textView1",
                        "textView2",
                        "textView3",
                        "textView4",
                        "textView1",
                        "textView2",
                        "textView3",
                        "textView4",
                        "textView1",
                        "textView2",
                        "textView3",
                        "textView4",
                        "textView1",
                        "textView2",
                        "textView3",
                        "textView4",
                        "textView1",
                        "textView2",
                        "textView3",
                        "textView4"
                    )
                ) { position, text ->
                    Toast.makeText(this@MainActivity, "click $text", Toast.LENGTH_SHORT).show()
                }
                .show()
        }
    }
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

五、实现在Activity中间弹出的List列表弹窗(带选中效果)的功能

实现:

主工程项目要导入的第三方项目library,如下图:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnShowCenterListWithCheck.setOnClickListener {//在中间弹出的List列表弹窗,带选中效果
            XPopup.Builder(this@MainActivity)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
                .asCenterList(
                    "请选择一项",
                    arrayOf(
                        "textView1",
                        "textView2",
                        "textView3",
                        "textView4",
                        "textView1",
                        "textView2",
                        "textView3",
                        "textView4"
                    ),
                    null, 1
                ) { position, text ->
                    Toast.makeText(this@MainActivity, "click $text", Toast.LENGTH_SHORT).show()
                }
                .show()
        }
    }
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

六、实现仿手机单指长按传送门从左右上下方平移进入的功能

实现:

项目:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
QQMsgPopup.kt

class QQMsgPopup(context: Context) : PositionPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.popup_qq_msg
    }
}

popup_qq_msg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="@drawable/bg_round2"
    android:layout_height="wrap_content">
    <TextView
        android:padding="20dp"
        android:textColor="#E43838"
        android:textSize="16sp"
        android:text="我是QQ的消息弹窗,嘿嘿!!!"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnShowPosition1.setOnClickListener {
            XPopup.Builder(this@MainActivity)

                //从右平移进入
                .offsetY(600)
                .offsetX(400)
                .popupAnimation(PopupAnimation.TranslateFromRight)

                //从上方平移进入(右)
//                .offsetY(600)
//                .offsetX(400)
//                .popupAnimation(PopupAnimation.TranslateFromTop)

                //从上方平移进入(右)
//                .offsetY(600)
//                .offsetX(400)
//                .popupAnimation(PopupAnimation.TranslateFromBottom)

                //从左平移进入
//                .offsetY(600)
//                .offsetX(-100)
//                .popupAnimation(PopupAnimation.TranslateFromLeft)

                //从上方平移进入(左)
//                .offsetY(600)
//                .offsetX(-100)
//                .popupAnimation(PopupAnimation.TranslateFromTop)

                //从下方平移进入(左)
//                .offsetY(600)
//                .offsetX(-100)
//                .popupAnimation(PopupAnimation.TranslateFromBottom)

                .asCustom(QQMsgPopup(this@MainActivity))
                .show()
        }
    }
}

MainActivity.kt代码中:

.popupAnimation(PopupAnimation.TranslateFromRight)

PopupAnimation.java,仿手机单指长按传送门从左右上下方平移进入

public enum PopupAnimation {
......
    // 平移,不带透明渐变
    TranslateFromLeft,  // 从左平移进入
    TranslateFromRight, // 从右平移进入
    TranslateFromTop,   // 从上方平移进入
    TranslateFromBottom, // 从下方平移进入
......
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

七、实现从顶部带TextView(点击取消)的弹窗的功能

实现:

项目:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

引入依赖包:

implementation 'com.blankj:utilcode:1.30.6'

代码:
NotificationMsgPopup.kt

class NotificationMsgPopup(context: Context) : PositionPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.popup_notification_msg
    }

    override fun onCreate() {
        super.onCreate()
        findViewById<View>(R.id.tvClose).setOnClickListener(
            OnClickListener { dismiss() })
    }

    override fun getPopupWidth(): Int {
        return ScreenUtils.getScreenWidth()
    }
}

popup_notification_msg.xml

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:padding="20dp"
        android:text="我是从顶部向下弹窗的自由定位弹窗,可以做一些通知的UI交互。\n这里只是用自由定位Position弹窗来演示,\n其他弹窗如Attach弹窗,PartShadow弹窗都能实现这个效果,感兴趣的朋友可以试试!!!"
        android:textColor="#fff"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tvClose"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="20dp"
        android:background="@drawable/bg_round2"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:text="我知道了!"
        android:textColor="#222" />
</LinearLayout>

bg_round2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="100dp"/>
    <solid android:color="#fff"/>
</shape>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnShowPosition3.setOnClickListener {
            XPopup.Builder(this@MainActivity)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
                .popupAnimation(PopupAnimation.TranslateFromTop)
                .asCustom(NotificationMsgPopup(this@MainActivity))
                .show()
        }
    }
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

八、实现自定义下拉阴影弹窗的功能

实现:

项目:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
CustomPartShadowPopupView.kt

class CustomPartShadowPopupView(context: Context) : PartShadowPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.custom_part_shadow_popup
    }

    override fun onCreate() {
        super.onCreate()
        btnClose.setOnClickListener { dismiss() }
    }

    override fun onShow() {
        super.onShow()
        Log.e("tag", "CustomPartShadowPopupView onShow")
    }

    override fun onDismiss() {
        super.onDismiss()
        Log.e("tag", "CustomPartShadowPopupView onDismiss")
    }
}

custom_part_shadow_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:background="#eeeeee"
    android:scrollbars="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="18dp"
            android:text="我只能帮你到这了,这里的UI和逻辑每个应用都不一样,只能你自己来实现了!"
            android:textColor="#111"
            android:textSize="18sp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="测试输入" />

        <Button
            android:id="@+id/ch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="改变高度" />

        <Button
            android:id="@+id/btnClose"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:text="关闭" />
    </LinearLayout>
</androidx.core.widget.NestedScrollView>

MainActivity.kt

class MainActivity : AppCompatActivity(), View.OnClickListener {
    private var popupView: CustomPartShadowPopupView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tv_all.setOnClickListener(this)
        tv_price.setOnClickListener(this)
        tv_sales.setOnClickListener(this)
    }

    private fun showPartShadow(v: View) {
        popupView = XPopup.Builder(this@MainActivity)
            .atView(v)
            .isViewMode(true)
            .autoOpenSoftInput(true)
            .setPopupCallback(object : SimpleCallback() {
                override fun onShow(popupView: BasePopupView) {
                    Toast.makeText(this@MainActivity, "Show显示了", Toast.LENGTH_SHORT).show()
                }

                override fun onDismiss(popupView: BasePopupView) {
                    Toast.makeText(this@MainActivity, "关闭Show了", Toast.LENGTH_SHORT).show()
                }
            })
            .asCustom(CustomPartShadowPopupView(this@MainActivity)) as CustomPartShadowPopupView
        popupView!!.show()
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_all, R.id.tv_price, R.id.tv_sales -> showPartShadow(v)
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff">

        <Space
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/tv_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:drawableRight="@mipmap/dropdown"
            android:drawablePadding="8dp"
            android:padding="15dp"
            android:text="综合"
            android:textColor="#000"
            android:textSize="16sp" />

        <Space
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="2" />

        <TextView
            android:id="@+id/tv_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:drawableRight="@mipmap/dropdown"
            android:drawablePadding="8dp"
            android:padding="15dp"
            android:text="价格"
            android:textColor="#000"
            android:textSize="16sp" />

        <Space
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="2" />

        <TextView
            android:id="@+id/tv_sales"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:drawableRight="@mipmap/dropdown"
            android:drawablePadding="8dp"
            android:padding="15dp"
            android:text="销量"
            android:textColor="#000"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

activity_main.xml中代码,如下图:
在这里插入图片描述
dropdown.png图片,需要的话可以CSDN私聊我,我就发

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

九、实现在任何处垂直带气泡Attach弹窗(未超屏)的功能

实现:

项目:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
CustomBubbleAttachPopup.kt

public class CustomBubbleAttachPopup extends BubbleAttachPopupView {
    public CustomBubbleAttachPopup(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_bubble_attach_popup;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        final TextView tv = findViewById(R.id.tv);
        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                tv.setText(tv.getText() + "\n 啊哈哈哈啊哈");
//                tv.setText("\n 啊哈哈哈啊哈");
                dismiss();
            }
        });
    }
}

custom_bubble_attach_popup.xml

<?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:orientation="vertical"
    android:padding="15dp">

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="床前明月光,\n疑是地上霜,\n举头望明月,\n低头思故乡低头思故乡。"
        android:textColor="#fff" />
</LinearLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btnBubbleAttachPopup2.setOnClickListener {
            XPopup.Builder(this@MainActivity)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
                .atView(vv)
                .hasShadowBg(false) // 去掉半透明背景
                .offsetY(XPopupUtils.dp2px(this@MainActivity, 6f))
                .asCustom(
                    CustomBubbleAttachPopup(this@MainActivity)
                        //.setBubbleBgColor(Color.RED)  //气泡背景
                        .setArrowWidth(XPopupUtils.dp2px(this@MainActivity, 5f))
                        .setArrowHeight(
                            XPopupUtils.dp2px(
                                this@MainActivity,
                                6f
                            )
                        )
                        .setArrowRadius(XPopupUtils.dp2px(this@MainActivity, 3f))
                )
                .show()
        }
    }
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

十、实现在中间显示带标题、内容、输入框(带有hint名)和确认取消按钮的弹窗的功能

实现:

主工程项目要导入的第三方项目library,如下图:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btnShowInputConfirm.setOnClickListener {
            XPopup.Builder(this@MainActivity)
                .hasStatusBarShadow(false)
                //.dismissOnBackPressed(false)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗对象,推荐设置这个
                .autoOpenSoftInput(true)
                .isDarkTheme(false)
                //.isViewMode(true)
                .setPopupCallback(DemoXPopupListener())
                //.autoFocusEditText(false) //是否让弹窗内的EditText自动获取焦点,默认是true
                //.moveUpToKeyboard(false)   //是否移动到软键盘上面,默认为true
                .asInputConfirm(
                    "我是标题", "大萨达撒大所大所大", null, "我是默认Hint文字"
                ) {
                    //                                          new XPopup.Builder(getContext()).asLoading().show();
                }
                .show()
        }
    }

    internal class DemoXPopupListener : SimpleCallback() {
        var fEvaluator = FloatEvaluator()
        var iEvaluator = FloatEvaluator()
        override fun onCreated(pv: BasePopupView) {
            Log.e("tag", "onCreated")
        }

        override fun onShow(popupView: BasePopupView) {
            Log.e("tag", "onShow")
        }

        override fun onDismiss(popupView: BasePopupView) {
            Log.e("tag", "onDismiss")
        }

        override fun beforeDismiss(popupView: BasePopupView) {
            Log.e("tag", "beforeDismiss")
        }

        //如果你自己想拦截返回按键事件,则重写这个方法,返回true即可
        override fun onBackPressed(popupView: BasePopupView): Boolean {
            Log.e("tag", "拦截的返回按键,按返回键XPopup不会关闭了")
            Toast.makeText(
                popupView.context,
                "onBackPressed返回true,拦截了返回按键,按返回键XPopup不会关闭了",
                Toast.LENGTH_SHORT
            ).show()
            return true
        }

        override fun onDrag(
            popupView: BasePopupView,
            value: Int,
            percent: Float,
            upOrLeft: Boolean
        ) {
            super.onDrag(popupView, value, percent, upOrLeft)
            Log.e("tag", "value: $value  percent: $percent")
            //            ((Activity) popupView.getContext()).getWindow().getDecorView().setTranslationX(value);
//            float e = fEvaluator.evaluate(percent, 1.0, 0.8);
//            View decorView = ((Activity) popupView.getContext()).getWindow().getDecorView();
//            decorView.setScaleX(e);
//            decorView.setScaleY(e);
//            FloatEvaluator iEvaluator = new FloatEvaluator();
//            View decorView = ((Activity) popupView.getContext()).getWindow().getDecorView();
//            float t = iEvaluator.evaluate(percent, 0, -popupView.getMeasuredWidth()/2);
//            decorView.setTranslationX(t);
        }

        override fun onKeyBoardStateChanged(popupView: BasePopupView, height: Int) {
            super.onKeyBoardStateChanged(popupView, height)
            Log.e("tag", "onKeyBoardStateChanged height: $height")
        }

        override fun onClickOutside(popupView: BasePopupView) {
            super.onClickOutside(popupView)
            Log.e("tag", "onClickOutside")
        }
    }
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

十一、尝试22个不同动画(弹窗)

实现:


主工程项目要导入的第三方项目library,如下图:

温馨提示:第三方项目library里面的不同弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便。订阅后CSDN私聊我,我就发给第三方项目library

代码:
PopupAnimation.java,22个不同动画(注释有解释)

public enum PopupAnimation {
    // 缩放 + 透明渐变
    ScaleAlphaFromCenter, // 从中心进行缩放+透明渐变
    ScaleAlphaFromLeftTop, //从左上角进行缩放+透明渐变
    ScaleAlphaFromRightTop, //从右上角进行缩放+透明渐变
    ScaleAlphaFromLeftBottom, //从左下角进行缩放+透明渐变
    ScaleAlphaFromRightBottom, //从右下角进行缩放+透明渐变

    // 平移 + 透明渐变
    TranslateAlphaFromLeft,  // 从左平移进入
    TranslateAlphaFromRight, // 从右平移进入
    TranslateAlphaFromTop,   // 从上方平移进入
    TranslateAlphaFromBottom, // 从下方平移进入

    // 平移,不带透明渐变
    TranslateFromLeft,  // 从左平移进入
    TranslateFromRight, // 从右平移进入
    TranslateFromTop,   // 从上方平移进入
    TranslateFromBottom, // 从下方平移进入

    // 滑动 + 透明渐变
    ScrollAlphaFromLeft,
    ScrollAlphaFromLeftTop,
    ScrollAlphaFromTop,
    ScrollAlphaFromRightTop,
    ScrollAlphaFromRight,
    ScrollAlphaFromRightBottom,
    ScrollAlphaFromBottom,
    ScrollAlphaFromLeftBottom,

    //禁用动画
    NoAnimation
}

MainActivity.kt

class MainActivity : AppCompatActivity() {
    lateinit var data: Array<PopupAnimation>
    var list = ArrayList<String>()
    var adapter: ArrayAdapter<*>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initViews()
    }

    private fun initViews() {
        data = PopupAnimation.values()
        for (i in 0..data.size - 1) {
            list.add(data[i].toString());
        }
        adapter = ArrayAdapter(this, android.R.layout.activity_list_item, android.R.id.text1, list)
        listview.adapter = adapter
        listview.setOnItemClickListener { adapterView, view, position, l ->
            XPopup.Builder(this@MainActivity)
                .popupAnimation(data[position])
                .asConfirm(
                    "演示应用不同的动画",
                    "你可以为弹窗选择任意一种动画,但这并不必要,因为我已经默认给每种弹窗设定了最佳动画!对于你自定义的弹窗,可以随心选择心仪的动画方案。",
                    null
                )
                .show()
        }
    }
}

主工程的build.gradle中加上:

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }//加上
    }
}

十二、实现左右边抽屉(drawer)弹窗---------第三方项目_XPopup源码的github地址

一、测试
实现:

二、项目

温馨提示:第三方项目library里面的弹窗有很多:从顶部弹窗、从底部弹窗、自由定位弹窗、自定义全屏弹窗、显示旋转弹窗(动画)…,适合工作,你想改什么样的弹窗就在这项目library里直接拿就即可,就是方便

三、代码
1、左边Drawer弹窗,LeftDrawerPopup.kt

class LeftDrawerPopup(context: Context) : DrawerPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.custom_pager_drawer
    }
}

2、右边Drawer弹窗,RightDrawerPopupView.kt

class RightDrawerPopupView(context: Context) : DrawerPopupView(context) {
    override fun getImplLayoutId(): Int {
        return R.layout.custom_list_drawer
    } 
}

3、MainActivity.kt

class MainActivity : AppCompatActivity() {
    var popupView: BasePopupView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btnShowDrawerLeft.setOnClickListener {
            XPopup.Builder(this@MainActivity)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
                .isViewMode(true) //使用了Fragment,必须开启View模式
                .asCustom(PagerDrawerPopup(this@MainActivity)) //                        .asCustom(new ListDrawerPopupView(getContext()))
                .show()
        }

        btnShowDrawerRight.setOnClickListener {
            popupView =
                XPopup.Builder(this@MainActivity) //                        .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
                    .autoOpenSoftInput(true)
                    .popupPosition(PopupPosition.Right) //右边
                    .hasStatusBarShadow(true) //启用状态栏阴影
                    .setPopupCallback(DemoXPopupListener())
                    .asCustom(ListDrawerPopupView(this@MainActivity))
            popupView!!.show()
        }
    }

    internal class DemoXPopupListener : SimpleCallback() {
        var fEvaluator = FloatEvaluator()
        var iEvaluator = FloatEvaluator()
        override fun onCreated(pv: BasePopupView) {
            Log.e("tag", "onCreated")
        }

        override fun onShow(popupView: BasePopupView) {
            Log.e("tag", "onShow")
        }

        override fun onDismiss(popupView: BasePopupView) {
            Log.e("tag", "onDismiss")
        }

        override fun beforeDismiss(popupView: BasePopupView) {
            Log.e("tag", "beforeDismiss")
        }

        //如果你自己想拦截返回按键事件,则重写这个方法,返回true即可
        override fun onBackPressed(popupView: BasePopupView): Boolean {
            Log.e("tag", "拦截的返回按键,按返回键XPopup不会关闭了")
            Toast.makeText(
                popupView.context,
                "onBackPressed返回true,拦截了返回按键,按返回键XPopup不会关闭了",
                Toast.LENGTH_SHORT
            ).show()
            return true
        }

        override fun onDrag(
            popupView: BasePopupView,
            value: Int,
            percent: Float,
            upOrLeft: Boolean
        ) {
            super.onDrag(popupView, value, percent, upOrLeft)
            Log.e("tag", "value: $value  percent: $percent")
        }

        override fun onKeyBoardStateChanged(popupView: BasePopupView, height: Int) {
            super.onKeyBoardStateChanged(popupView, height)
            Log.e("tag", "onKeyBoardStateChanged height: $height")
        }

        override fun onClickOutside(popupView: BasePopupView) {
            super.onClickOutside(popupView)
            Log.e("tag", "onClickOutside")
        }
    }
}

四、下载源码github地址:实现左右边抽屉(drawer)弹窗---------第三方项目_XPopup源码的github地址

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: Android开发中使用Kotlin语言开发Material Design项目可以带来很多好处。Kotlin是一种现代化的编程语言,它可以提高开发效率、减少代码量、提高代码可读性和可维护性。同时,Material Design是一种现代化的设计语言,它可以提高用户体验、提高应用的可用性和可访问性。因此,使用Kotlin开发Material Design项目可以使应用更加现代化、高效、易于维护和易于使用。 ### 回答2: 随着移动设备的迅速普及,Android操作系统已经成为全球最流行的移动操作系统之一。这使得Android应用程序的开发变得越来越受欢迎,许多开发人员也开始学习和掌握这个平台。随着时间的推移,开发人员也在不断寻找最好的解决方案来创建优秀的应用程序,其中kotlin和material design就是两种最受欢迎的选择。 Kotlin是一种高级编程语言,它是Java虚拟机的官方语言之一。KotlinAndroid开发中的流行程度日益增加,因为它具有许多特性,如可空类型、lambdas、扩展函数等,使得开发Android应用程序更加便捷和高效。Kotlin支持Java虚拟机,并非Android特定的开发语言,因此具有更广泛的用途,可以与其他语言无缝集成。同时,Kotlin还有很多实用特性,如可空类型、类型推断、lambda表达式等,能够在Android开发中大幅提高开发效率,在代码中减少了很多荣誉的代码和冗长表达式的麻烦。 Material Design是一种设计语言,由Google推出,旨在为移动和Web应用程序提供一致的极致体验。Material Design提供了一系列的设计指南、模式和组件,以便开发人员可以为他们的应用程序在不同平台和设备上提供一致的体验,从而使应用程序更加具有现代感和吸引力。 Material Design为Android开发者提供了一些标准的界面组件,如浮动操作按钮、抽屉式导航等,同时也支持进行自定义设计,为应用程序增加独特的特色。 综合考虑,使用Kotlin和Material Design组合开发Android应用程序可以有很多好处。Kotlin可以使代码更加简洁,同时使用Material Design的组件和元素可以使应用程序显得更加现代和美观。 这种组合还可以提高开发效率,减少代码中的bug,在Android平台上提供更好的用户体验,从而为应用程序的成功打下坚实的基础。总的来说,采用这种开发方式的应用程序将具有更高的可维护性和可扩展性,也将在市场上拥有更高的竞争力。 ### 回答3: 在当前的移动应用开发市场中,Android系统已经成为了主流之一,其开发工具也被越来越多的开发者采用。而Kotlin语言作为一种新兴的编程语言,因其简洁、安全、互通性和易用性等特点,在Android系统开发中越来越受到开发者的欢迎。 在这种情况下,开发Material Design项目需要用到的技术和工具也应该是非常有趣的。 Android系统采用Material Design作为其UI设计风格,提供了一套完备的UI组件。要开发Material Design的应用程序,需要遵循Google的Material Design规范以及使用相关的Android开发API和工具。Kotlin语言提供了很多的便利,在使用Android开发API和工具的同时,还提供了方便的语法和Lambda表达式。 使用Kotlin开发Material Design项目还可以提高应用程序的安全性,避免因类型不安全、空指针等问题导致的错误。Kotlin还支持函数式编程,可以提供更好的编写UI代码的方法,帮助开发人员高效编写代码,提高开发效率。 Kotlin增加了一些与Java不同的特性,使得开发者能够更快、更方便地编写代码。例如,Kotlin具有空安全机制,可帮助开发者尽早发现和解决可能导致应用程序崩溃的问题,从而提高应用程序的质量。Kotlin还提供了lambda表达式、扩展功能和集合操作,更方便开发人员在项目中增加新特性,缩短应用开发周期。 虽然Kotlin开发Material Design项目相对于Java来说还是一个相对新的领域,但随着Kotlin使用率的逐渐增加,越来越多的开发者正在使用和探索KotlinAndroid开发中的应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彬sir哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值