android模仿58筛选下拉框(PopupWindow实现)
前言:前几天用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实现)的更多相关文章
- appium常见问题01_android筛选下拉框无法定位问题
近期用appium做android自动化的过程中,遇到一种筛选下拉框,神奇的是,定位工具定位怎样都定位不到. 首先尝试用uiaotomator工具定位,无法定位到下拉框元素,只能定位到底层元素: 询问 ...
- Android UI自定义Spinner下拉框(用popuwindow实现)-转
定义出第一个图片的布局和弹出框(一个listView)的布局,,这里就不在多说了~ListView需要自己定义一个MyspinnerAdapter~做好这些准备之后,就是弹出框的实现了~ prote ...
- android+myeclipse+mysql自定义控件下拉框的数据绑定
原创作品,允许转载,转载时请务必声明作者信息和本声明.http://www.cnblogs.com/zhu520/p/8031936.html 本人小白,那个大神看到有问题可指出,谢谢.... 这个是 ...
- iOS: 悬浮的条件筛选下拉框的使用
1.介绍 app中条件筛选视图是很常用的功能,一般它搭配着tableView的表头悬浮滚动使用,点击按钮时,就会弹出下拉框显示条件,选择一个条件后,下拉框自动隐藏. 2.效果图如下 从中间点击弹出,然 ...
- Android实现三级联动下拉框 下拉列表spinner
Android实现(省.市.县)三级联动下拉框 下拉列表spinner 转载请注明出处: http://www.goteny.com/articles/2013/11/46.html http://w ...
- Android实现三级联动下拉框下拉列表spinner
原文出处:http://www.cnblogs.com/zjjne/archive/2013/10/03/3350107.html 主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下 ...
- 【Android】5.4 下拉框(Spinner)
分类:C#.Android.VS2015: 创建日期:2016-02-07 下拉列表框Spinner的用法和WinForms中ComboBox的用法非常相似,在Android应用中使用频次也相当高,因 ...
- javaFX笔记----ComboBox模仿qq账号下拉框删除账号
myComboBox.setCellFactory( new Callback<ListView<String>, ListCell<String>>() { @O ...
- android中自定义下拉框(转)
android自带的下拉框好用不?我觉得有时候好用,有时候难有,项目规定这样的效果,自带的控件实现不了,那么只有我们自己来老老实实滴写一个新的了,其实最基本的下拉框就像一些资料填写时,点击的时候出现在 ...
随机推荐
- BZOJ4596: [Shoi2016]黑暗前的幻想乡
Description 四年一度的幻想乡大选开始了,最近幻想乡最大的问题是很多来历不明的妖 怪涌入了幻想乡,扰乱了幻想乡昔日的秩序.但是幻想乡的建制派妖怪(人类) 博丽灵梦和八云紫等人整日高谈所有妖怪 ...
- 第一次IT技术面试经历
一.技术总监面试问题: 1.Hibernate的应用项目例举 2.jsp标签库例举 3.oracle的增删改查 4.关系型数据库的关联关系 5.数据库分页操作 二.技术总监面试问题: 1.for循环中 ...
- Master-Slave通用基础框架
一.设计目的 设计出一个通用的Master-Slave基础框架,然后可以基于这个框架来实现特定的业务需求,比如实现多节点并行计算.分布式处理等. 二.设计理念 基于经典的命令模式,Master和Sla ...
- Txt格式配置表无法解析的问题——BOM
今天再次遇到同一个问题:策划给来一个Txt格式配置表,我用解析类去读取,返回的结果为空.解析类参数是:主键key,文件名fileName,错误提示errorTip. 写读取语句的时候,主键key我是直 ...
- yii笔一----基础,安装,结构,增删改查基本操作
从yii中文站开始http://www.yiichina.com/ Yii 是一个高性能,基于组件的 PHP 框架 一. 1.安装yii方式 composer安装或者下载一份应用程序模板.刚开始学习, ...
- TP5验证规则
系统内置的验证规则如下: 格式验证类 require 验证某个字段必须,例如:'name'=>'require' number 或者 integer 验证某个字段的值是否为数字(采用filter ...
- ubuntu 下emacs 配置
(set-language-environment 'Chinese-GB) (set-keyboard-coding-system 'utf-8) (set-clipboard-coding-sys ...
- datatables服务器端分页要点
背景:当要查询大量数据的时候,有datatables自身的分页,明显查询比较慢,这是要使用服务器端分页 参数:"bServerSide": true, "fnServer ...
- Windows和Linux都有的Copy-on-write技术
Windows和Linux都有的Copy-on-write技术 MySQL技术内幕Innodb存储引擎第2版 P375 SQL Server2008 实现与维护(MCTS教程)P199 LVM快照技术 ...
- ASP.NET SignalR 高可用设计
在 One ASP.NET 的架构图中,微软将 WebAPI 和 SignalR 归类到 Services 类型与 MVC.Web Forms 同列为一等公民,未来的 ASP.NET 5 尽管还在be ...