0
点赞
收藏
分享

微信扫一扫

PopupWindow 属性,我们的使用的途径

_铁马冰河_ 2022-02-23 阅读 77


今天看到了,比人写的一个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,我们可以通过自定义的方式,进行改进其中的许多的东西。

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.     private TextView tvZuQuyu;  

  9.     private String[] parentStrings = {"全城","中原区","二七区","管城区","金水区","上街区","惠济区","郑东新区","高新区","经开区","郑州周边"};  
  10.     private String[][] childrenStrings={{},  
  11.             {"中原1","中原2","中原3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},  
  12.             {"二七1","二七2","二七3","二七4","二七5","二七6","二七7","二七8","二七9","二七10","二七11","二七12","二七13","二七14","二七15"},  
  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.     };  

  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.activity_chuzu_city_main);  

  26.         tvZuQuyu = (TextView) findViewById(R.id.tvZuQuyu);  
  27.         tvZuQuyu.setOnClickListener(this);  
  28.     }  

  29.     @Override  
  30.     public void onClick(View v) {  
  31.         switch (v.getId()) {  
  32.         case R.id.tvZuQuyu:  
  33.             if(mPopupWindow == null){  
  34.                 mPopupWindow = new SelectPopupWindow(parentStrings,childrenStrings,this,selectCategory);  
  35.             }  
  36.             mPopupWindow.showAsDropDown(tvZuQuyu, -5, 10);  
  37.             break;  
  38.         }  
  39.     }  

  40.     /** 
  41.      * 选择完成回调接口 
  42.      */  
  43.     private SelectCategory selectCategory=new SelectCategory() {  
  44.         @Override  
  45.         public void selectCategory(int parentSelectposition,int childrenSelectposition) {  
  46.             String parentStr=parentStrings[parentSelectposition];  
  47.             String childrenStr=childrenStrings[parentSelectposition][childrenSelectposition];  

  48.             Toast.makeText(MainActivity.this, "父类别:"+parentStr+"  子类别:"+childrenStr, 0).show();  
  49.         }  
  50.     };  
  51. }  




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.     private String[] parentStrings;  
  9.     private String[][] childrenStrings;  

  10.     private ListView lvParentCategory = null;  
  11.     private ListView lvChildrenCategory= null;  
  12.     private ParentCategoryAdapter parentCategoryAdapter = null;  
  13.     private ChildrenCategoryAdapter childrenCategoryAdapter = null;  

  14.     /** 
  15.      * @param parentStrings   字类别数据 
  16.      * @param childrenStrings 字类别二位数组 
  17.      * @param activity 
  18.      * @param selectCategory  回调接口注入 
  19.      */  
  20.     public SelectPopupWindow(String[] parentStrings,String[][] childrenStrings,Activity activity,SelectCategory selectCategory) {  
  21.             this.selectCategory=selectCategory;  
  22.             this.parentStrings=parentStrings;  
  23.             this.childrenStrings=childrenStrings;  

  24.             View contentView = LayoutInflater.from(activity).inflate(R.layout.layout_quyu_choose_view, null);  
  25.         DisplayMetrics dm = new DisplayMetrics();  
  26.         activity.getWindowManager().getDefaultDisplay().getMetrics(dm); // 获取手机屏幕的大小  

  27.             this.setContentView(contentView);  
  28.             this.setWidth(dm.widthPixels);  
  29.             this.setHeight(dm.heightPixels*7/10);  

  30.         /* 设置背景显示 */  
  31.         setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.pop_bg));  
  32.         /* 设置触摸外面时消失 */  
  33.         setOutsideTouchable(true);  
  34.         setTouchable(true);  
  35.         setFocusable(true); /*设置点击menu以外其他地方以及返回键退出 */  

  36.         /** 
  37.          * 1.解决再次点击MENU键无反应问题 
  38.          */  
  39.         contentView.setFocusableInTouchMode(true);  

  40.         //父类别适配器  
  41.         lvParentCategory= (ListView) contentView.findViewById(R.id.lv_parent_category);  
  42.         parentCategoryAdapter = new ParentCategoryAdapter(activity,parentStrings);  
  43.         lvParentCategory.setAdapter(parentCategoryAdapter);  

  44.         //子类别适配器  
  45.         lvChildrenCategory= (ListView) contentView.findViewById(R.id.lv_children_category);  
  46.         childrenCategoryAdapter = new ChildrenCategoryAdapter(activity);  
  47.         lvChildrenCategory.setAdapter(childrenCategoryAdapter);  

  48.         lvParentCategory.setOnItemClickListener(parentItemClickListener);  
  49.         lvChildrenCategory.setOnItemClickListener(childrenItemClickListener);  
  50.     }  

  51.     /** 
  52.      * 子类别点击事件 
  53.      */  
  54.     private OnItemClickListener childrenItemClickListener=new OnItemClickListener(){  
  55.         @Override  
  56.         public void onItemClick(AdapterView<?> parent, View view, int position,long id) {  
  57.             if(selectCategory!=null){  
  58.                 selectCategory.selectCategory(parentCategoryAdapter.getPos(),position);  
  59.             }  
  60.             dismiss();  
  61.         }  
  62.     };  

  63.     /** 
  64.      * 父类别点击事件 
  65.      */  
  66.     private OnItemClickListener parentItemClickListener=new OnItemClickListener() {  
  67.         @Override  
  68.         public void onItemClick(AdapterView<?> parent, View view, int position,long id) {  
  69.             childrenCategoryAdapter.setDatas(childrenStrings[position]);  
  70.             childrenCategoryAdapter.notifyDataSetChanged();  

  71.             parentCategoryAdapter.setSelectedPosition(position);  
  72.             parentCategoryAdapter.notifyDataSetChanged();  
  73.         }  
  74.     };  

  75.     /** 
  76.      * 选择成功回调 
  77.      * @author apple 
  78.      * 
  79.      */  
  80.     public interface SelectCategory{  
  81.         /** 
  82.          * 把选中的下标通过方法回调回来 
  83.          * @param parentSelectposition  父类别选中下标 
  84.          * @param childrenSelectposition 子类别选中下标 
  85.          */  
  86.         public void selectCategory(int parentSelectposition,int childrenSelectposition);  
  87.     }  

  88. }  



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.     <ListView  
  8.         android:id="@+id/lv_parent_category"  
  9.         android:layout_width="0dp"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_weight="3"  
  12.         android:background="@color/zu_choose_left_item_bg"  
  13.         android:cacheColorHint="@android:color/transparent"  
  14.         android:divider="@color/zu_choose_left_item_diveder"  
  15.         android:dividerHeight="1dp"  
  16.         android:scrollbars="none"/>  

  17.     <View  
  18.         android:layout_width="1dp"  
  19.         android:layout_height="fill_parent"  
  20.         android:background="@color/zu_choose_left_item_diveder"/>  

  21.     <ListView  
  22.         android:id="@+id/lv_children_category"  
  23.         android:layout_width="0dp"  
  24.         android:layout_height="fill_parent"  
  25.         android:layout_weight="4"  
  26.         android:background="@color/zu_choose_right_item_bg"  
  27.         android:cacheColorHint="@android:color/transparent"  
  28.         android:divider="@color/zu_choose_left_item_diveder"  
  29.         android:dividerHeight="1dp"  
  30.         android:scrollbars="none" />  

  31. </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.     public ParentCategoryAdapter(Context context,String[] str) {  
  11.         mContext = context;  
  12.         this.str = str;  
  13.     }  

  14.     @Override  
  15.     public int getCount() {  
  16.         return str.length;  
  17.     }  

  18.     @Override  
  19.     public Object getItem(int position) {  
  20.         return null;  
  21.     }  

  22.     @Override  
  23.     public long getItemId(int position) {  
  24.         return position;  
  25.     }  

  26.     @Override  
  27.     public View getView(int position, View convertView, ViewGroup parent) {  
  28.         ViewHolder holder = null;  
  29.         if (convertView == null) {  
  30.             holder = new ViewHolder();  
  31.             convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_parent_category_item, null);  
  32.             holder.tvParentCategoryName = (TextView) convertView.findViewById(R.id.tv_parent_category_name);  
  33.             convertView.setTag(holder);  
  34.         } else {  
  35.             holder = (ViewHolder) convertView.getTag();  
  36.         }  

  37.         holder.tvParentCategoryName.setText(str[position]);  

  38.         if(pos==position){  
  39.             holder.tvParentCategoryName.setTextColor(mContext.getResources().getColor(R.color.list_text_select_color));  
  40.             convertView.setBackgroundColor(mContext.getResources().getColor(R.color.zu_choose_right_item_bg));  
  41.         }else{  
  42.             holder.tvParentCategoryName.setTextColor(mContext.getResources().getColor(android.R.color.black));  
  43.             convertView.setBackgroundColor(mContext.getResources().getColor(R.color.zu_choose_left_item_bg));  
  44.         }  
  45.         return convertView;  
  46.     }  

  47.     private class ViewHolder {  
  48.         private TextView tvParentCategoryName;  
  49.     }  

  50.     public void setSelectedPosition(int pos) {  
  51.         this.pos = pos;  
  52.     }  

  53.     public int getPos() {  
  54.         return pos;  
  55.     }  
  56. }  



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


举报

相关推荐

0 条评论