可扩展的popUpwindow
发布日期:2021-11-12 07:57:42 浏览次数:34 分类:技术文章

本文共 6801 字,大约阅读时间需要 22 分钟。

原文地址:http://blog.csdn.net/yaya_soft/article/details/40399727

通常我们使用popUpwindow的时候例如都是写一个布局文件然后设置到popUpwindow的自定义布局当中,但有时候我们可能有这样的需求,底部的取消按钮是一定的,但是上面的选项不是写死的两个或者三个而是更加我们的需求动态来决定的,这个时候我们不可能去写死一个布局了,那么就需要我们自定义一个pop,动态添加同时要可以监听各项的点击事件。。。

   先说一下思路,我们先定义一个basepopwindow,来抽象几个公用方法,然后通过子类来实现

  具体代码:

A:抽象基类

      

[java]
  1. public abstract class BasePopupWindow extends PopupWindow {  
  2.   
  3.     protected View popRootView;  
  4.       
  5.     public BasePopupWindow() {  
  6.         super();  
  7.     }  
  8.   
  9.     public BasePopupWindow(Context context, AttributeSet attrs, int defStyle) {  
  10.         super(context, attrs, defStyle);  
  11.     }  
  12.   
  13.     public BasePopupWindow(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.     }  
  16.   
  17.     public BasePopupWindow(Context context) {  
  18.         super(context);  
  19.     }  
  20.   
  21.     public BasePopupWindow(int width, int height) {  
  22.         super(width, height);  
  23.     }  
  24.   
  25.     public BasePopupWindow(View contentView, int width, int height,  
  26.             boolean focusable) {  
  27.         super(contentView, width, height, focusable);  
  28.     }  
  29.   
  30.     public BasePopupWindow(View contentView) {  
  31.         super(contentView);  
  32.     }  
  33.       
  34.     public BasePopupWindow(View contentView, int width, int height){  
  35.         super(contentView, width, height,true);  
  36.         this.popRootView = contentView;  
  37.         setFocusable(true);  
  38.         setOutsideTouchable(true);  
  39.         setTouchable(true);  
  40.         ColorDrawable dw = new ColorDrawable(0xb0000000);  
  41.         this.setBackgroundDrawable(dw);  
  42.         setAnimationStyle(R.style.AnimBottom);  
  43.         initViews();  
  44.         initEvents();  
  45.         init();  
  46.           
  47.     }  
  48.     public abstract void initViews();  
  49.   
  50.     public abstract void initEvents();  
  51.   
  52.     public abstract void init();  
  53.   
  54.     public View findViewById(int id) {  
  55.         return popRootView.findViewById(id);  
  56.     }  
B: 实现子类:

   里面加入我的一些说明:

[java]
  1. public class CommBottomPopWindow extends BasePopupWindow implements  
  2.         OnClickListener {  
  3.   
  4.     private Button cancleBtn;  
  5.   
  6.     private PopWindowListener listener;  
  7.   
  8.     private LinearLayout mLayout;  
  9.   
  10.     private Context mContext;  
  11.   
  12.     private boolean isHasSubTitle = false;  
  13.   
  14.     private LayoutInflater inflater;  
  15.   
  16.     /** 
  17.      * 功能描述: 设置点击事件<br> 
  18.      * 〈功能详细描述〉 
  19.      *  点击的自定义回调接口 
  20.      */  
  21.     public void setPopListener(PopWindowListener listener) {  
  22.         this.listener = listener;  
  23.     }  
  24.   
  25.     public CommBottomPopWindow(Context context) {  
  26.        //布局填充  
  27.         super((LayoutInflater.from(context).inflate(  
  28.                 R.layout.comm_bottom_popwindow, null)),  
  29.                 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);  
  30.         mContext = context;  
  31.     }  
  32.   
  33.     /** 
  34.      * 功能描述:初始化小标题 <br> 
  35.        顶部是否需要提示的小标题 
  36.      */  
  37.     public void initPopSubTitle(String notiTxt) {  
  38.         mLayout.addView(createItem(notiTxt, true));  
  39.     }  
  40.   
  41.     /** 
  42.      * 功能描述: 初始化item<br> 
  43.      * 〈功能详细描述〉 
  44.         动态添加的条目 
  45.      */  
  46.     public void initPopItem(List<String> list) {  
  47.   
  48.         if (list == null || list.size() == 0) {  
  49.             return;  
  50.         }  
  51.   
  52.         for (int i = 0; i < list.size(); i++) {  
  53.             String title = list.get(i);  
  54.             mLayout.addView(createItem(title, i, list.size()));  
  55.         }  
  56.     }  
  57.   
  58.     private View createItem(String itemTxt, boolean isSubTitle) {  
  59.         return createItem(itemTxt, -1, -1, isSubTitle);  
  60.     }  
  61.   
  62.     private View createItem(String itemTxt, final int index, int total) {  
  63.         return createItem(itemTxt, index, total, false);  
  64.     }  
  65.   
  66.     /** 
  67.      * 功能描述: 创建item<br> 
  68.      * 〈功能详细描述〉 
  69.      *  
  70.        创建具体的条目 
  71.      */  
  72.     private View createItem(String itemTxt, final int index, int total,  
  73.             boolean isSubTitle) {  
  74.         inflater = (LayoutInflater) mContext  
  75.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  76.         View view = inflater.inflate(R.layout.comm_bottom_popwindow_item, null);  
  77.         LinearLayout layout = (LinearLayout) view  
  78.                 .findViewById(R.id.comm_popwindow_item_layout);  
  79.   
  80.         TextView textView = (TextView) view  
  81.                 .findViewById(R.id.comm_popwindow_item_txt);  
  82.   
  83.         textView.setText(itemTxt);  
  84.   
  85.         if (isSubTitle) {  
  86.             isHasSubTitle = true;  
  87.             layout.setBackgroundResource(R.drawable.selectpopwin_up);  
  88.             textView.setTextColor(ResUtil.getColor(R.color.color_999999));  
  89.         } else if (index == 0 && !isHasSubTitle) {  
  90.             layout.setBackgroundResource(R.drawable.btn_selectpopwin_up);  
  91.         } else if (index == total - 1) {  
  92.             layout.setBackgroundResource(R.drawable.btn_selectpopwin_down);  
  93.         } else {  
  94.             layout.setBackgroundResource(R.drawable.btn_camp_selpopwin_center);  
  95.         }  
  96.   
  97.         view.setOnClickListener(new OnClickListener() {  
  98.   
  99.             @Override  
  100.             public void onClick(View v) {  
  101.                 if (index == -1) {  
  102.                     return;  
  103.                 }  
  104.                 if (listener != null) {  
  105.                     listener.onPopSelected(index);  
  106.                 }  
  107.             }  
  108.         });  
  109.         return view;  
  110.     }  
  111.   
  112.     @Override  
  113.     public void initViews() {  
  114.         mLayout = (LinearLayout) findViewById(R.id.comm_bottom_popwindow_layout);  
  115.         cancleBtn = (Button) findViewById(R.id.camp_pop_cancle);  
  116.         isHasSubTitle = false;  
  117.     }  
  118.   
  119.     @Override  
  120.     public void initEvents() {  
  121.         cancleBtn.setOnClickListener(this);  
  122.         popRootView.setOnClickListener(new OnClickListener() {  
  123.   
  124.             @Override  
  125.             public void onClick(View v) {  
  126.                 dismiss();  
  127.             }  
  128.         });  
  129.     }  
  130.   
  131.     @Override  
  132.     public void init() {  
  133.     }  
  134.   
  135.     @Override  
  136.     public void onClick(View v) {  
  137.         switch (v.getId()) {  
  138.             case R.id.camp_pop_cancle:  
  139.                 dismiss();  
  140.                 break;  
  141.             default:  
  142.                 break;  
  143.         }  
  144.   
  145.     }  
  146.   
  147.     /** 
  148.      * 功能描述: 显示pop window<br> 
  149.      * 〈功能详细描述〉 
  150.      */  
  151.     public void show(View view) {  
  152.         showAtLocation(view, Gravity.BOTTOM, 00);  
  153.     }  
  154.     //回调接口定义  
  155.     public interface PopWindowListener {  
  156.         public void onPopSelected(int which);  
  157.     }  

C:贴下布局

    comm_bottom_popwindow.xml

[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:gravity="bottom"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <LinearLayout  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginLeft="20dp"  
  12.         android:layout_marginRight="20dp"  
  13.         android:gravity="center_horizontal"  
  14.         android:orientation="vertical"  
  15.         android:paddingTop="20dp" >  
  16.   
  17.         <LinearLayout  
  18.             android:id="@+id/comm_bottom_popwindow_layout"  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:orientation="vertical" >  
  22.         </LinearLayout>  
  23.   
  24.         <Button  
  25.             android:id="@+id/camp_pop_cancle"  
  26.             android:layout_width="fill_parent"  
  27.             android:layout_height="@dimen/comm_padding_size_9"  
  28.             android:layout_marginBottom="15dp"  
  29.             android:layout_marginTop="15dp"  
  30.             android:background="@drawable/btn_selectpopwin_cancel"  
  31.             android:text="@string/camp_cancle"  
  32.             android:textColor="@color/text_blue"  
  33.             android:textSize="17sp" />  
  34.     </LinearLayout>  
  35.   
  36. </LinearLayout>  
comm_bottom_popwindow_item.xml
[html]
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/comm_popwindow_item_layout"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="@dimen/comm_padding_size_9"  
  6.     android:gravity="center"  
  7.     android:orientation="horizontal" >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/comm_popwindow_item_txt"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="@string/camp_balance_query"  
  14.         android:gravity="center"  
  15.         android:textColor="@color/text_blue"  
  16.         android:textSize="17sp" />  
  17.   
  18. </LinearLayout>  
  其实就是定义一个linearlayout,根据具体的需求添加里面的item即可,同时通过一个自定义的接口监听item的点击就ok了

  ps 用法如下:

[java]
  1. private void initPopWindow() {  
  2.       if (mPopWindow == null) {  
  3.           mPopWindow = new CommBottomPopWindow(getActivity());  
  4.           List<String> list = new ArrayList<>();  
  5.           list.add("test1");  
  6.           list.add("test2");  
  7.           // 带显示小标题,可加可不加  
  8.           mPopWindow.initPopSubTitle("sub title context");  
  9.           mPopWindow.initPopItem(list);  
  10.           mPopWindow.setPopListener(mPopListener);  
  11.       }  
  12.       // 显示pop window  
  13.       mPopWindow.show(view);  
  14.   } 

转载地址:https://blog.csdn.net/happy_love1990/article/details/78327977 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:android开发游记:ItemTouchHelper 使用RecyclerView打造可拖拽的GridView
下一篇:android ImageLoader加载本地图片的工具类

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月24日 07时57分12秒