今天看到了,比人写的一个Demo,看到了之后感觉还不错,自己也是仔细的思考了一哈,其实这样的用途也是比较的多的嘛。

使用PopupWindow可实现弹出窗口效果,,其实和AlertDialog一样,也是一种对话框,两者也经常混用,但是也各有特点。下面就看看使用方法。

showAsDropDown的用法属性介绍

首先初始化一个PopupWindow,指定窗口大小参数。(这下面的东西看起来都是差不多的效果)

PopupWindow mPop = new PopupWindow(getLayoutInflater().inflate(R.layout.window, null),LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

//自定义布局
ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
R.layout.window, null, true);
PopupWindow mPop = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, true);
当然也可以手动设置PopupWindow大小。
mPop.setContentView(menuView );//设置包含视图
mPop.setWidth(int )
mPop.setHeight(int )//设置弹出框大小

设置进场动画:
mPop.setAnimationStyle(R.style.AnimationPreview);//设置动画样式

mPop.setOutsideTouchable(true);//这里设置显示PopuWindow之后在外面点击是否有效。

如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow。

当然这里很明显只能在Touchable下才能使用。(这个效果是十分的有用的哟)

当有mPop.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popuWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。
mPop.setFocusable(true);
mPop.setBackgroundDrawable(....);
mPop.showAsDropDown(anchor, 0, 0);//设置显示PopupWindow的位置位于View的左下方,x,y表示坐标偏移量mPop.setOnDismissListenerd(new PopupWindow.OnDismissListener(){})//设置窗口消失事件
看到了我们上面的这些属性,其实我们也就可以大概的了解到了,我们可以通过监听的方式,让任何的东西点击,然后呈现我们的PopWindow在其恰当的位置,至于我们的PopWinod,我们可以通过自定义的方式,进行改进其中的许多的东西。

PopupWindow 属性,我们的使用的途径_父类

58同城APP找房子的时候,看到筛选下拉框蛮不错的,然后也有很多朋友需要实现这个功能,,重构,封装.把一些公共的东西抽取出来,选择下拉框那块做成一个工具类,然后通过接口回调回来.进行处理吧,哈哈这里是原网址

 

1.MainActivity.java  用户点击区域TextView的时候,初始化自定义控件PopupWindow,然后显示PopupWindow.通过PopupWindow构造参数传入一个选择完成的监听接口实现。

 

[java]  view plain copy
 
  1. /** 
  2.  * 主Activity 
  3.  * @author ansen 
  4.  * @create time 2015-09-25 
  5.  */  
  6. public class MainActivity extends Activity implements OnClickListener{  
  7.     private SelectPopupWindow mPopupWindow = null;  
  8.       
  9.     private TextView tvZuQuyu;  
  10.       
  11.     private String[] parentStrings = {"全城","中原区","二七区","管城区","金水区","上街区","惠济区","郑东新区","高新区","经开区","郑州周边"};  
  12.     private String[][] childrenStrings={{},  
  13.             {"中原1","中原2","中原3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},  
  14.             {"二七1","二七2","二七3","二七4","二七5","二七6","二七7","二七8","二七9","二七10","二七11","二七12","二七13","二七14","二七15"},  
  15.             {"管城1","管城2","管城3","管城4","管城5","管城6","管城7","管城8","管城9","管城10","管城11","管城12","管城13","管城14","管城15"},  
  16.             {"金水1","金水2","金水3","金水4","金水5","金水6","金水7","金水8","金水9","金水10","金水11","金水12","金水13","金水14","金水15"},  
  17.             {"上街1","上街2","上街3","上街4","上街5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},  
  18.             {"中原1","中原2","中原3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},  
  19.             {"郑东新区1","郑东新区2","郑东新区3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},  
  20.             {"高新区1","高新区2","高新区3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},  
  21.             {"经开区1","经开区2","经开区3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},  
  22.             {"周边1","周边2","周边3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},  
  23.     };  
  24.       
  25.     @Override  
  26.     public void onCreate(Bundle savedInstanceState) {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.activity_chuzu_city_main);  
  29.           
  30.         tvZuQuyu = (TextView) findViewById(R.id.tvZuQuyu);  
  31.         tvZuQuyu.setOnClickListener(this);  
  32.     }  
  33.   
  34.     @Override  
  35.     public void onClick(View v) {  
  36.         switch (v.getId()) {  
  37.         case R.id.tvZuQuyu:  
  38.             if(mPopupWindow == null){  
  39.                 mPopupWindow = new SelectPopupWindow(parentStrings,childrenStrings,this,selectCategory);  
  40.             }  
  41.             mPopupWindow.showAsDropDown(tvZuQuyu, -5, 10);  
  42.             break;  
  43.         }  
  44.     }  
  45.       
  46.     /** 
  47.      * 选择完成回调接口 
  48.      */  
  49.     private SelectCategory selectCategory=new SelectCategory() {  
  50.         @Override  
  51.         public void selectCategory(int parentSelectposition,int childrenSelectposition) {  
  52.             String parentStr=parentStrings[parentSelectposition];  
  53.             String childrenStr=childrenStrings[parentSelectposition][childrenSelectposition];  
  54.               
  55.             Toast.makeText(MainActivity.this, "父类别:"+parentStr+"  子类别:"+childrenStr, 0).show();  
  56.         }  
  57.     };  
  58. }  

 

2.SelectPopupWindow.java   自定义的PopupWindow,在构造方法中设置内容,设置背景等.给要显示的两个ListView设置适配器,添加ListView点击事件,点击子类别的时候回调选中的两个下标,关闭PopupWindow。

 

[java]  view plain copy
 
  1. /** 
  2.  * 选择PopupWindow 
  3.  * @author ansen 
  4.  * @create time 2015-10-09 
  5.  */  
  6. public class SelectPopupWindow extends PopupWindow{  
  7.     private SelectCategory selectCategory;  
  8.       
  9.     private String[] parentStrings;  
  10.     private String[][] childrenStrings;  
  11.       
  12.     private ListView lvParentCategory = null;  
  13.     private ListView lvChildrenCategory= null;  
  14.     private ParentCategoryAdapter parentCategoryAdapter = null;  
  15.     private ChildrenCategoryAdapter childrenCategoryAdapter = null;  
  16.       
  17.     /** 
  18.      * @param parentStrings   字类别数据 
  19.      * @param childrenStrings 字类别二位数组 
  20.      * @param activity 
  21.      * @param selectCategory  回调接口注入 
  22.      */  
  23.     public SelectPopupWindow(String[] parentStrings,String[][] childrenStrings,Activity activity,SelectCategory selectCategory) {  
  24.             this.selectCategory=selectCategory;  
  25.             this.parentStrings=parentStrings;  
  26.             this.childrenStrings=childrenStrings;  
  27.               
  28.             View contentView = LayoutInflater.from(activity).inflate(R.layout.layout_quyu_choose_view, null);  
  29.         DisplayMetrics dm = new DisplayMetrics();  
  30.         activity.getWindowManager().getDefaultDisplay().getMetrics(dm); // 获取手机屏幕的大小  
  31.               
  32.             this.setContentView(contentView);  
  33.             this.setWidth(dm.widthPixels);  
  34.             this.setHeight(dm.heightPixels*7/10);  
  35.               
  36.         /* 设置背景显示 */  
  37.         setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.pop_bg));  
  38.         /* 设置触摸外面时消失 */  
  39.         setOutsideTouchable(true);  
  40.         setTouchable(true);  
  41.         setFocusable(true); /*设置点击menu以外其他地方以及返回键退出 */  
  42.           
  43.         /** 
  44.          * 1.解决再次点击MENU键无反应问题 
  45.          */  
  46.         contentView.setFocusableInTouchMode(true);  
  47.           
  48.         //父类别适配器  
  49.         lvParentCategory= (ListView) contentView.findViewById(R.id.lv_parent_category);  
  50.         parentCategoryAdapter = new ParentCategoryAdapter(activity,parentStrings);  
  51.         lvParentCategory.setAdapter(parentCategoryAdapter);  
  52.           
  53.         //子类别适配器  
  54.         lvChildrenCategory= (ListView) contentView.findViewById(R.id.lv_children_category);  
  55.         childrenCategoryAdapter = new ChildrenCategoryAdapter(activity);  
  56.         lvChildrenCategory.setAdapter(childrenCategoryAdapter);  
  57.           
  58.         lvParentCategory.setOnItemClickListener(parentItemClickListener);  
  59.         lvChildrenCategory.setOnItemClickListener(childrenItemClickListener);  
  60.     }  
  61.       
  62.     /** 
  63.      * 子类别点击事件 
  64.      */  
  65.     private OnItemClickListener childrenItemClickListener=new OnItemClickListener(){  
  66.         @Override  
  67.         public void onItemClick(AdapterView<?> parent, View view, int position,long id) {  
  68.             if(selectCategory!=null){  
  69.                 selectCategory.selectCategory(parentCategoryAdapter.getPos(),position);  
  70.             }  
  71.             dismiss();  
  72.         }  
  73.     };  
  74.       
  75.     /** 
  76.      * 父类别点击事件 
  77.      */  
  78.     private OnItemClickListener parentItemClickListener=new OnItemClickListener() {  
  79.         @Override  
  80.         public void onItemClick(AdapterView<?> parent, View view, int position,long id) {  
  81.             childrenCategoryAdapter.setDatas(childrenStrings[position]);  
  82.             childrenCategoryAdapter.notifyDataSetChanged();  
  83.               
  84.             parentCategoryAdapter.setSelectedPosition(position);  
  85.             parentCategoryAdapter.notifyDataSetChanged();  
  86.         }  
  87.     };  
  88.       
  89.     /** 
  90.      * 选择成功回调 
  91.      * @author apple 
  92.      * 
  93.      */  
  94.     public interface SelectCategory{  
  95.         /** 
  96.          * 把选中的下标通过方法回调回来 
  97.          * @param parentSelectposition  父类别选中下标 
  98.          * @param childrenSelectposition 子类别选中下标 
  99.          */  
  100.         public void selectCategory(int parentSelectposition,int childrenSelectposition);  
  101.     }  
  102.       
  103. }  

3.layout_quyu_choose_view.xml   PopupWindow展示的布局文件,两个就两个ListView

 

 

[html]  view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/pop_bg"  
  6.     android:orientation="horizontal">  
  7.   
  8.     <ListView  
  9.         android:id="@+id/lv_parent_category"  
  10.         android:layout_width="0dp"  
  11.         android:layout_height="fill_parent"  
  12.         android:layout_weight="3"  
  13.         android:background="@color/zu_choose_left_item_bg"  
  14.         android:cacheColorHint="@android:color/transparent"  
  15.         android:divider="@color/zu_choose_left_item_diveder"  
  16.         android:dividerHeight="1dp"  
  17.         android:scrollbars="none"/>  
  18.   
  19.     <View  
  20.         android:layout_width="1dp"  
  21.         android:layout_height="fill_parent"  
  22.         android:background="@color/zu_choose_left_item_diveder"/>  
  23.   
  24.     <ListView  
  25.         android:id="@+id/lv_children_category"  
  26.         android:layout_width="0dp"  
  27.         android:layout_height="fill_parent"  
  28.         android:layout_weight="4"  
  29.         android:background="@color/zu_choose_right_item_bg"  
  30.         android:cacheColorHint="@android:color/transparent"  
  31.         android:divider="@color/zu_choose_left_item_diveder"  
  32.         android:dividerHeight="1dp"  
  33.         android:scrollbars="none" />  
  34.   
  35. </LinearLayout>  

 

4.ParentCategoryAdapter.java  父类别适配器的实现,跟我们平时经常写的适配器没啥两样,就在getView方法里面判断是否选中,选中的那个下标颜色设置的不一样.

 

[java]  view plain copy
 
  1. /** 
  2.  * 父类别 适配器 
  3.  * @author ansen 
  4.  * @create time 2015-09-25 
  5.  */  
  6. public class ParentCategoryAdapter extends BaseAdapter {  
  7.     private Context mContext;  
  8.     private String[] str;  
  9.     private int pos;  
  10.       
  11.     public ParentCategoryAdapter(Context context,String[] str) {  
  12.         mContext = context;  
  13.         this.str = str;  
  14.     }  
  15.   
  16.     @Override  
  17.     public int getCount() {  
  18.         return str.length;  
  19.     }  
  20.   
  21.     @Override  
  22.     public Object getItem(int position) {  
  23.         return null;  
  24.     }  
  25.   
  26.     @Override  
  27.     public long getItemId(int position) {  
  28.         return position;  
  29.     }  
  30.   
  31.     @Override  
  32.     public View getView(int position, View convertView, ViewGroup parent) {  
  33.         ViewHolder holder = null;  
  34.         if (convertView == null) {  
  35.             holder = new ViewHolder();  
  36.             convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_parent_category_item, null);  
  37.             holder.tvParentCategoryName = (TextView) convertView.findViewById(R.id.tv_parent_category_name);  
  38.             convertView.setTag(holder);  
  39.         } else {  
  40.             holder = (ViewHolder) convertView.getTag();  
  41.         }  
  42.   
  43.         holder.tvParentCategoryName.setText(str[position]);  
  44.           
  45.         if(pos==position){  
  46.             holder.tvParentCategoryName.setTextColor(mContext.getResources().getColor(R.color.list_text_select_color));  
  47.             convertView.setBackgroundColor(mContext.getResources().getColor(R.color.zu_choose_right_item_bg));  
  48.         }else{  
  49.             holder.tvParentCategoryName.setTextColor(mContext.getResources().getColor(android.R.color.black));  
  50.             convertView.setBackgroundColor(mContext.getResources().getColor(R.color.zu_choose_left_item_bg));  
  51.         }  
  52.         return convertView;  
  53.     }  
  54.   
  55.     private class ViewHolder {  
  56.         private TextView tvParentCategoryName;  
  57.     }  
  58.       
  59.     public void setSelectedPosition(int pos) {  
  60.         this.pos = pos;  
  61.     }  
  62.       
  63.     public int getPos() {  
  64.         return pos;  
  65.     }  
  66. }  

还有子类别适配器,一些布局文件我就不全部贴出来了,有需要的可以下载源码.