前言:前几天用58同城APP找房子的时候,看到筛选下拉框蛮不错的,然后也有很多朋友需要实现这个功能,于是从网上下载了一个demo,在他的基础上进行修改,花了几个小时对他的代码进行修改,重构,封装.把一些公共的东西抽取出来,选择下拉框那块做成一个工具类,然后通过接口回调回来.

效果图如下:

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

/**
* 主Activity
* @author ansen
* @create time 2015-09-25
*/
public class MainActivity extends Activity implements OnClickListener{
private SelectPopupWindow mPopupWindow = null; private TextView tvZuQuyu; private String[] parentStrings = {"全城","中原区","二七区","管城区","金水区","上街区","惠济区","郑东新区","高新区","经开区","郑州周边"};
private String[][] childrenStrings={{},
{"中原1","中原2","中原3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},
{"二七1","二七2","二七3","二七4","二七5","二七6","二七7","二七8","二七9","二七10","二七11","二七12","二七13","二七14","二七15"},
{"管城1","管城2","管城3","管城4","管城5","管城6","管城7","管城8","管城9","管城10","管城11","管城12","管城13","管城14","管城15"},
{"金水1","金水2","金水3","金水4","金水5","金水6","金水7","金水8","金水9","金水10","金水11","金水12","金水13","金水14","金水15"},
{"上街1","上街2","上街3","上街4","上街5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},
{"中原1","中原2","中原3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},
{"郑东新区1","郑东新区2","郑东新区3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},
{"高新区1","高新区2","高新区3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},
{"经开区1","经开区2","经开区3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},
{"周边1","周边2","周边3","中原4","中原5","中原6","中原7","中原8","中原9","中原10","中原11","中原12","中原13","中原14","中原15"},
}; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chuzu_city_main); tvZuQuyu = (TextView) findViewById(R.id.tvZuQuyu);
tvZuQuyu.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tvZuQuyu:
if(mPopupWindow == null){
mPopupWindow = new SelectPopupWindow(parentStrings,childrenStrings,this,selectCategory);
}
mPopupWindow.showAsDropDown(tvZuQuyu, -5, 10);
break;
}
} /**
* 选择完成回调接口
*/
private SelectCategory selectCategory=new SelectCategory() {
@Override
public void selectCategory(int parentSelectposition,int childrenSelectposition) {
String parentStr=parentStrings[parentSelectposition];
String childrenStr=childrenStrings[parentSelectposition][childrenSelectposition]; Toast.makeText(MainActivity.this, "父类别:"+parentStr+" 子类别:"+childrenStr, 0).show();
}
};
}

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

/**
* 选择PopupWindow
* @author ansen
* @create time 2015-10-09
*/
public class SelectPopupWindow extends PopupWindow{
private SelectCategory selectCategory; private String[] parentStrings;
private String[][] childrenStrings; private ListView lvParentCategory = null;
private ListView lvChildrenCategory= null;
private ParentCategoryAdapter parentCategoryAdapter = null;
private ChildrenCategoryAdapter childrenCategoryAdapter = null; /**
* @param parentStrings 字类别数据
* @param childrenStrings 字类别二位数组
* @param activity
* @param selectCategory 回调接口注入
*/
public SelectPopupWindow(String[] parentStrings,String[][] childrenStrings,Activity activity,SelectCategory selectCategory) {
this.selectCategory=selectCategory;
this.parentStrings=parentStrings;
this.childrenStrings=childrenStrings; View contentView = LayoutInflater.from(activity).inflate(R.layout.layout_quyu_choose_view, null);
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm); // 获取手机屏幕的大小 this.setContentView(contentView);
this.setWidth(dm.widthPixels);
this.setHeight(dm.heightPixels*7/10); /* 设置背景显示 */
setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.pop_bg));
/* 设置触摸外面时消失 */
setOutsideTouchable(true);
setTouchable(true);
setFocusable(true); /*设置点击menu以外其他地方以及返回键退出 */ /**
* 1.解决再次点击MENU键无反应问题
*/
contentView.setFocusableInTouchMode(true); //父类别适配器
lvParentCategory= (ListView) contentView.findViewById(R.id.lv_parent_category);
parentCategoryAdapter = new ParentCategoryAdapter(activity,parentStrings);
lvParentCategory.setAdapter(parentCategoryAdapter); //子类别适配器
lvChildrenCategory= (ListView) contentView.findViewById(R.id.lv_children_category);
childrenCategoryAdapter = new ChildrenCategoryAdapter(activity);
lvChildrenCategory.setAdapter(childrenCategoryAdapter); lvParentCategory.setOnItemClickListener(parentItemClickListener);
lvChildrenCategory.setOnItemClickListener(childrenItemClickListener);
} /**
* 子类别点击事件
*/
private OnItemClickListener childrenItemClickListener=new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
if(selectCategory!=null){
selectCategory.selectCategory(parentCategoryAdapter.getPos(),position);
}
dismiss();
}
}; /**
* 父类别点击事件
*/
private OnItemClickListener parentItemClickListener=new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
childrenCategoryAdapter.setDatas(childrenStrings[position]);
childrenCategoryAdapter.notifyDataSetChanged(); parentCategoryAdapter.setSelectedPosition(position);
parentCategoryAdapter.notifyDataSetChanged();
}
}; /**
* 选择成功回调
* @author apple
*
*/
public interface SelectCategory{
/**
* 把选中的下标通过方法回调回来
* @param parentSelectposition 父类别选中下标
* @param childrenSelectposition 子类别选中下标
*/
public void selectCategory(int parentSelectposition,int childrenSelectposition);
} }

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/pop_bg"
android:orientation="horizontal"> <ListView
android:id="@+id/lv_parent_category"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:background="@color/zu_choose_left_item_bg"
android:cacheColorHint="@android:color/transparent"
android:divider="@color/zu_choose_left_item_diveder"
android:dividerHeight="1dp"
android:scrollbars="none"/> <View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:background="@color/zu_choose_left_item_diveder"/> <ListView
android:id="@+id/lv_children_category"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="4"
android:background="@color/zu_choose_right_item_bg"
android:cacheColorHint="@android:color/transparent"
android:divider="@color/zu_choose_left_item_diveder"
android:dividerHeight="1dp"
android:scrollbars="none" /> </LinearLayout>

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

/**
* 父类别 适配器
* @author ansen
* @create time 2015-09-25
*/
public class ParentCategoryAdapter extends BaseAdapter {
private Context mContext;
private String[] str;
private int pos; public ParentCategoryAdapter(Context context,String[] str) {
mContext = context;
this.str = str;
} @Override
public int getCount() {
return str.length;
} @Override
public Object getItem(int position) {
return null;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_parent_category_item, null);
holder.tvParentCategoryName = (TextView) convertView.findViewById(R.id.tv_parent_category_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
} holder.tvParentCategoryName.setText(str[position]); if(pos==position){
holder.tvParentCategoryName.setTextColor(mContext.getResources().getColor(R.color.list_text_select_color));
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.zu_choose_right_item_bg));
}else{
holder.tvParentCategoryName.setTextColor(mContext.getResources().getColor(android.R.color.black));
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.zu_choose_left_item_bg));
}
return convertView;
} private class ViewHolder {
private TextView tvParentCategoryName;
} public void setSelectedPosition(int pos) {
this.pos = pos;
} public int getPos() {
return pos;
}
}

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

推荐下自己创建的android QQ群:   欢迎大家的加入


点击下载源码

android模仿58筛选下拉框(PopupWindow实现)的更多相关文章

  1. appium常见问题01_android筛选下拉框无法定位问题

    近期用appium做android自动化的过程中,遇到一种筛选下拉框,神奇的是,定位工具定位怎样都定位不到. 首先尝试用uiaotomator工具定位,无法定位到下拉框元素,只能定位到底层元素: 询问 ...

  2. Android UI自定义Spinner下拉框(用popuwindow实现)-转

    定义出第一个图片的布局和弹出框(一个listView)的布局,,这里就不在多说了~ListView需要自己定义一个MyspinnerAdapter~做好这些准备之后,就是弹出框的实现了~  prote ...

  3. android+myeclipse+mysql自定义控件下拉框的数据绑定

    原创作品,允许转载,转载时请务必声明作者信息和本声明.http://www.cnblogs.com/zhu520/p/8031936.html 本人小白,那个大神看到有问题可指出,谢谢.... 这个是 ...

  4. iOS: 悬浮的条件筛选下拉框的使用

    1.介绍 app中条件筛选视图是很常用的功能,一般它搭配着tableView的表头悬浮滚动使用,点击按钮时,就会弹出下拉框显示条件,选择一个条件后,下拉框自动隐藏. 2.效果图如下 从中间点击弹出,然 ...

  5. Android实现三级联动下拉框 下拉列表spinner

    Android实现(省.市.县)三级联动下拉框 下拉列表spinner 转载请注明出处: http://www.goteny.com/articles/2013/11/46.html http://w ...

  6. Android实现三级联动下拉框下拉列表spinner

    原文出处:http://www.cnblogs.com/zjjne/archive/2013/10/03/3350107.html 主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下 ...

  7. 【Android】5.4 下拉框(Spinner)

    分类:C#.Android.VS2015: 创建日期:2016-02-07 下拉列表框Spinner的用法和WinForms中ComboBox的用法非常相似,在Android应用中使用频次也相当高,因 ...

  8. javaFX笔记----ComboBox模仿qq账号下拉框删除账号

    myComboBox.setCellFactory( new Callback<ListView<String>, ListCell<String>>() { @O ...

  9. android中自定义下拉框(转)

    android自带的下拉框好用不?我觉得有时候好用,有时候难有,项目规定这样的效果,自带的控件实现不了,那么只有我们自己来老老实实滴写一个新的了,其实最基本的下拉框就像一些资料填写时,点击的时候出现在 ...

随机推荐

  1. 原生js封装二级城市下拉列表

    闲的蛋疼,封装了个二级城市下拉 先保证html里有 <select id="province" size=1 > </select> <select ...

  2. asp.net 页面上的点击事件

    asp.net 页面上 服务器端控件Button 有两个click事件如 <asp:Button ID="Button1" runat="server" ...

  3. 转载:Chrome调试折腾记_(1)调试控制中心快捷键详解!!!

    转载:http://blog.csdn.net/crper/article/details/48098625 大多浏览器的调试功能的启用快捷键都一致…按下F12;还是熟悉的味道;  或者直接 Ctrl ...

  4. cocoapods安装及使用

    最近新换了电脑,重新安装cocoapods遇到了很多问题,在这里把问题还有解决方案记录一下 一.安装Cocoapods 在安装CocoaPods之前,首先要在本地安装好Ruby环境,一般Mac下都自带 ...

  5. Ubuntu install codeblocks by ppa

    sudo add-apt-repository ppa:damien-moore/codeblocks-stable sudo apt-get update sudo apt-get install ...

  6. dell笔记本三个系统,ubuntu16.04更新,boot分区容量不足解决办法

    本人自己dell物理机上安装windows 7 .centos 1704 和ubuntu1604 三个系统的,分区当时没有使用lVM,boot单独挂/dev/sda7 分区,只有200M,随着2次li ...

  7. [IOS]cocoapos 两个ruby源的对比

    最近需要使用一些动态类库,cocoapods比较好用,能帮助管理这些类库,百度一下也能找到很多cocoapods配置方法,这里不赘述,我想要讲的是在配置的时候一般都会推荐这样做 $ gem sourc ...

  8. >xx.hbm.xml的一些简单配置

    1.在hibernate-mapping的属性里有一个package,它的意思是以下的类都是在这个包下的,下面写类路径的时候,可以不写包名 2.class标签 name属性指的是类 table属性指的 ...

  9. Allegro之Enhance pad Entry(增强焊盘进入约束功能)的使用

    pcb布线时,有时候会从器件的焊盘往外拉线,为了避免出现类似情况 出现锐角焊盘内绕线等等 可在add connect操作下,右键勾选Enhance pad Entry来增强焊盘进入的约束,可有效防止上 ...

  10. Oracle查询和问题简记

    现在做两个版本的系统,一个用的数据库是Access,另一个就是Oracle了.每个数据库支持的的查询SQL语句都有所区别,这里主要针对Access和Oracle做 记录. 首先贴出遇到问题最多的一条语 ...