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     }

 

好久沒寫博客了,最近比較忙,但有好的東西還是忍不住寫下來。一點點積累,總沒有壞處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM