一:前言
本人参考博客:http://blog.csdn.net/jdsjlzx/article/details/41316417 最近在弄一个下拉框,发现Android自带的很难实现我的功能,于是去网上找到一份Demo,但是发现没有封装的好,并且还有很多重复的代码,于是我在这位前辈的基础上进行了修改.并且重新进行了封装,代码变得简单,并且具有可读性.
二:实现原理
1.就是一个TextView 弄一个弧形的背景图片,然后设置一个点击事件.
2.点击TextView之后显示一个PopupWindow,Popupwindow是自定义的,里面显示一个ListView.ListView也设置了左边.右边.底部的边框.
三:效果图如下
四:代码实现:
1).MainActivity.java 程序入口,初始化数据,初始化自定义PopupWIndow,TextView点击之后显示PopupWIndow,处理ListView的点击事件.
/** * 主Activity 用来实现popupwindow * @author ansen */ public class MainActivity extends Activity { private SpinerPopWindow<String> mSpinerPopWindow; private List<String> list; private TextView tvValue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); tvValue = (TextView) findViewById(R.id.tv_value); tvValue.setOnClickListener(clickListener); mSpinerPopWindow = new SpinerPopWindow<String>(this, list,itemClickListener); mSpinerPopWindow.setOnDismissListener(dismissListener); } /** * 监听popupwindow取消 */ private OnDismissListener dismissListener=new OnDismissListener() { @Override public void onDismiss() { setTextImage(R.drawable.icon_down); } }; /** * popupwindow显示的ListView的item点击事件 */ private OnItemClickListener itemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position,long id) { mSpinerPopWindow.dismiss(); tvValue.setText(list.get(position)); Toast.makeText(MainActivity.this, "点击了:" + list.get(position),Toast.LENGTH_LONG).show(); } }; /** * 显示PopupWindow */ private OnClickListener clickListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_value: mSpinerPopWindow.setWidth(tvValue.getWidth()); mSpinerPopWindow.showAsDropDown(tvValue); setTextImage(R.drawable.icon_up); break; } } }; /** * 初始化数据 */ private void initData() { list = new ArrayList<String>(); for (int i = 0; i < 5; i++) { list.add("test:" + i); } } /** * 给TextView右边设置图片 * @param resId */ private void setTextImage(int resId) { Drawable drawable = getResources().getDrawable(resId); drawable.setBounds(0, 0, drawable.getMinimumWidth(),drawable.getMinimumHeight());// 必须设置图片大小,否则不显示 tvValue.setCompoundDrawables(null, null, drawable, null); } }
2).activity_main.xml 这个文件也没啥好说的 就是两个TextView。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_value" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shape_help_category_tv_bg" android:drawableRight="@drawable/icon_down" android:padding="10dp" android:textColor="@color/content_color" android:text="请选择父类别" android:textSize="20sp"/> <TextView android:layout_marginLeft="5dp" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/shape_help_category_tv_bg" android:drawableRight="@drawable/icon_down" android:textColor="@color/content_color" android:padding="10dp" android:text="请选择子类别" android:textSize="20sp" /> </LinearLayout> </LinearLayout>
3).SpinerPopWindow.java 自定义Popupwindow类 初始化PopupWindow显示的布局,以及一些参数,并且给listView设置适配器
* 自定义PopupWindow 主要用来显示ListView * @author Ansen * @param <T> * @param <T> * @create time 2015-11-3 */ public class SpinerPopWindow<T> extends PopupWindow { private LayoutInflater inflater; private ListView mListView; private List<T> list; private MyAdapter mAdapter; public SpinerPopWindow(Context context,List<T> list,OnItemClickListener clickListener) { super(context); inflater=LayoutInflater.from(context); this.list=list; init(clickListener); } private void init(OnItemClickListener clickListener){ View view = inflater.inflate(R.layout.spiner_window_layout, null); setContentView(view); setWidth(LayoutParams.WRAP_CONTENT); setHeight(LayoutParams.WRAP_CONTENT); setFocusable(true); ColorDrawable dw = new ColorDrawable(0x00); setBackgroundDrawable(dw); mListView = (ListView) view.findViewById(R.id.listview); mListView.setAdapter(mAdapter=new MyAdapter()); mListView.setOnItemClickListener(clickListener); } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @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=inflater.inflate(R.layout.spiner_item_layout, null); holder.tvName=(TextView) convertView.findViewById(R.id.tv_name); convertView.setTag(holder); }else{ holder=(ViewHolder) convertView.getTag(); } holder.tvName.setText(getItem(position).toString()); return convertView; } } private class ViewHolder{ private TextView tvName; } }
4).spiner_window_layout.xml PopupWindow显示的布局文件 里面就一个ListView控件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:orientation="vertical"> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:background="@drawable/shape_popupwindow_list_bg" android:scrollbars="none" > </ListView> </LinearLayout>
还有一些其他布局,我就不一一贴出来了,有兴趣的可以去看看源码.这个Demo还是比较简单的,相信大家都能看懂.
推荐下自己创建的android QQ群:202928390 欢迎大家的加入.