zoukankan      html  css  js  c++  java
  • PopupWindow 的常用api封装

    对PopupWindow常用API的简单封装,几行代码就搞定PopupWindow弹窗,使用Builder模式,链式调用,像使用AlertDialog 一样

    封装通用PopupWindow,CustomPopWindow,使用链式的方式配置并显示

    由于每次写PopupWindow都要写很多重复代码,因此简单的封装了一个CustomPopWindow.封装了PopupWindow 的一些常用API,使用Builder模式,就像写AlertDialog 一样,链式配置。

    相关博客

    1,通用PopupWindow,几行代码搞定PopupWindow弹窗

    2, 通用PopupWindow,几行代码搞定PopupWindow弹窗(续)

    Usage

    由于 1.0.0 版本 是托管到 Jcenter的,添加如下依赖:

    Add the dependency to your build.gradle.

    1 dependencies {
    2     compile 'com.example.zhouwei.library:library:1.0.0'
    3 }

    2.x 版本 代码托管到Jitpack, 需要如下依赖:

    Add it in your root build.gradle :

    1 allprojects {
    2 repositories {        ...
    3         maven {
    4             url 'https://jitpack.io'
    5 
    6         }
    7 
    8 }

    Add the dependency:

    1 dependencies {
    2     compile 'com.github.pinguo-zhouwei:CustomPopwindow:2.0.0'
    3 }

    使用方法:

    更新日志:(添加弹出PopupWindow同时背景变暗的配置,添加配置动画)

    更新1:  背景变暗配置示例:

    1    //创建并显示popWindow
    2  mCustomPopWindow= new CustomPopWindow.PopupWindowBuilder(this)
    3                 .setView(contentView)
    4                 .enableBackgroundDark(true) //弹出popWindow时,背景是否变暗
    5                 .setBgDarkAlpha(0.7f) // 控制亮度
    6                 .create()
    7                 .showAsDropDown(mButton5,0,20);

    更新2:  显示消失动画配置:

    1  CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(this)
    2                 .setView(R.layout.pop_layout1)
    3                 .setFocusable(true)
    4                 .setOutsideTouchable(true)
    5                 .setAnimationStyle(R.style.CustomPopWindowStyle) // 添加自定义显示和消失动画
    6                 .create()
    7                 .showAsDropDown(mButton1,0,10);

    1,简便写法

    1 CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(this)
    2                 .setView(R.layout.pop_layout1)//显示的布局,还可以通过设置一个View
    3            //     .size(600,400) //设置显示的大小,不设置就默认包裹内容
    4                 .setFocusable(true)//是否获取焦点,默认为ture
    5                 .setOutsideTouchable(true)//是否PopupWindow 以外触摸dissmiss
    6                 .create()//创建PopupWindow
    7                 .showAsDropDown(mButton1,0,10);//显示PopupWindow

    以上就是弹出一个简单的PopupWindow,是不是看起来很优雅和简单,还可以简单一点:

    1 CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(this)
    2                 .setView(R.layout.pop_layout1)//显示的布局
    3                 .create()//创建PopupWindow
    4                 .showAsDropDown(mButton1,0,10);//显示PopupWindow

    如果是一个简单的只展示文案的弹窗,就可以只设置一个View,就可以了,很简单吧!!!

    2,展示一个PopupWindow 弹窗菜单(像手机QQ,微信的顶部菜单)

    1 View contentView = LayoutInflater.from(this).inflate(R.layout.pop_menu,null);//处理popWindow 显示内容
    2         handleLogic(contentView);        //创建并显示popWindow
    3         mCustomPopWindow= new CustomPopWindow.PopupWindowBuilder(this)
    4                 .setView(contentView)
    5                 .create()
    6                 .showAsDropDown(mButton3,0,20);

    如果PopupWindow 展示的内容需要在程序代码中设置或者响应点击事件等,可以现获取到这个View,然后处理一些显示和点击事件逻辑,再交给CustomPopWindow 创建显示。比如响应菜单点击事件的逻辑处理:

     1     /**
     2      * 处理弹出显示内容、点击事件等逻辑     * @param contentView
     3      */
     4     private void handleLogic(View contentView) {
     5         View.OnClickListener listener = new View.OnClickListener() {
     6             @Override
     7             public void onClick(View v) {
     8                 if (mCustomPopWindow != null) {
     9                     mCustomPopWindow.dissmiss();
    10                 }
    11                 String showContent = "";
    12                 switch (v.getId()) {
    13                     case R.id.menu1:
    14                         showContent = "点击 Item菜单1";
    15                         break;
    16                     case R.id.menu2:
    17                         showContent = "点击 Item菜单2";
    18                         break;
    19                     case R.id.menu3:
    20                         showContent = "点击 Item菜单3";
    21                         break;
    22                     case R.id.menu4:
    23                         showContent = "点击 Item菜单4";
    24                         break;
    25                     case R.id.menu5:
    26                         showContent = "点击 Item菜单5";
    27                         break;
    28                 }
    29                 Toast.makeText(MainActivity.this, showContent, Toast.LENGTH_SHORT).show();
    30             }
    31         };
    32         contentView.findViewById(R.id.menu1).setOnClickListener(listener);
    33         contentView.findViewById(R.id.menu2).setOnClickListener(listener);
    34         contentView.findViewById(R.id.menu3).setOnClickListener(listener);
    35         contentView.findViewById(R.id.menu4).setOnClickListener(listener);
    36         contentView.findViewById(R.id.menu5).setOnClickListener(listener);
    37     }

    3,展示一个ListView,其实跟上面是一样的,这里贴一下实例代码:

     1 private void showPopListView() {
     2         View contentView = LayoutInflater.from(this).inflate(R.layout.pop_list, null);//处理popWindow 显示内容
     3         handleListView(contentView);        //创建并显示popWindow
     4         mListPopWindow = new CustomPopWindow.PopupWindowBuilder(this)
     5                 .setView(contentView)
     6                 .size(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)//显示大小
     7                 .create()
     8                 .showAsDropDown(mButton4, 0, 20);
     9     }
    10 
    11     private void handleListView(View contentView) {
    12         RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerView);
    13         LinearLayoutManager manager = new LinearLayoutManager(this);
    14         manager.setOrientation(LinearLayoutManager.VERTICAL);
    15         recyclerView.setLayoutManager(manager);
    16         MyAdapter adapter = new MyAdapter();
    17         adapter.setData(mockData());
    18         recyclerView.setAdapter(adapter);
    19         adapter.notifyDataSetChanged();
    20 
    21     }

    好久没写博客了,最近比较忙,但有好的东西还是忍不住写下来。一点点积累,总没有坏处。

  • 相关阅读:
    HDU-6801 2020HDU多校第三场T11 (生成函数)
    [HDU-6791] 2020HDU多校第三场T1(回文自动机)
    回文自动机 (PAM,Palindrome Automaton)
    字符串的Period(周期),Border
    「APIO2019」路灯 (K-D Tree / 树套树 / CDQ + 树状数组)
    「APIO2019」桥梁(询问分块+并查集)
    「APIO2019」奇怪装置
    「APIO2018」选圆圈(K-D Tree/CDQ+Set)
    堆小结
    【[HNOI/AHOI2018]毒瘤】
  • 原文地址:https://www.cnblogs.com/huolongluo/p/6958958.html
Copyright © 2011-2022 走看看