Android开发自定义下拉框下拉列表
来源:互联网 发布:编程入门视频 编辑:程序博客网 时间:2024/04/29 16:58
开发中难免有各种需求,近期项目中需要自定义下拉框的使用。实现效果如图,展开状态箭头向上,收起的时候箭头向下,选中状态选中条目颜色可变,可设置下拉框高度。效果如下
自定义CustomSpinner.java
public class CustomSpinner extends LinearLayout{ private View view; private TextView tv_name; private ImageView ib; //界面控件 private ImageView spinner; //构造qq号用到的集合 private List<String> list = new ArrayList<String>(); //布局加载器 //自定义适配器 private MyAdapter mAdapter; //PopupWindow private PopupWindow pop; //是否显示PopupWindow,默认不显示 private boolean isPopShow = true; private ListView listView; private LayoutInflater mInflater; private OnItemSelectedListenerSpinner onItemSelectedListener; private int heiht; private int postion = 0; public CustomSpinner(Context context) { super(context); initView(context); } public CustomSpinner(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); } public CustomSpinner(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } public CustomSpinner(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); initView(context); } private void initView(final Context context) { mInflater = LayoutInflater.from(context); view = mInflater.inflate(R.layout.layout_customspinner, null); mAdapter = new MyAdapter(); tv_name = (TextView) view.findViewById(R.id.et_name); ib = (ImageView) view.findViewById(R.id.spinner); tv_name.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { L.e("isshow--"+isPopShow); if (null != list){ if(pop == null){ listView = new ListView(context); listView.setCacheColorHint(0x00000000); listView.setDividerHeight(0); listView.setBackgroundColor(Color.rgb(255,255,255)); listView.setAdapter(mAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { postion = i; mAdapter.notifyDataSetChanged(); tv_name.setText(list.get(i)); ib.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp); pop.dismiss(); isPopShow = true; CustomSpinner.this.view.setTag(getId()); onItemSelectedListener.onItemSelected(CustomSpinner.this.view,view,i,l); } }); if (heiht == 0){ int hei = setListViewHeightBasedOnChildren(listView); //这里设置下拉框的高度 if (hei >= 550){ pop = new PopupWindow(listView, CustomSpinner.this.view.getWidth(), 550, true); }else{ pop = new PopupWindow(listView, CustomSpinner.this.view.getWidth(), hei, true); } }else{ pop = new PopupWindow(listView, CustomSpinner.this.view.getWidth(),heiht, true); } pop.setBackgroundDrawable(new ColorDrawable(0x00000000)); pop.setFocusable(true); pop.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { isPopShow = true; ib.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp); } }); ib.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp_right); pop.showAsDropDown(view,0, 0); isPopShow = false; }else{ if(isPopShow){ ib.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp_right); //向上的箭头 pop.showAsDropDown(view, 0, 0); isPopShow = false; }else{ ib.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp); //向下的箭头 pop.dismiss(); isPopShow = true; } } } onClickCustom(); } }); if (list == null || list.size() == 0){ tv_name.setText(""); }else{ tv_name.setText(list.get(0)); } addView(view); } public static int setListViewHeightBasedOnChildren(ListView listView) { // 获取ListView对应的Adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return 0; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目 View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 计算子项View 的宽高 totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度 } int ff = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); return ff; } public void onClickCustom(){ } public void attachDataSource(List<String> list){ this.list = list; tv_name.setText(list.get(0)); } public void setOnItemSelectedListener(OnItemSelectedListenerSpinner onItemSelectedListener){ this.onItemSelectedListener = onItemSelectedListener; Log.e("www","走了"); } public void setSpinnerHeiht(int heiht){ this.heiht = heiht; } public void setSelectedIndex(int index){ tv_name.setText(list.get(index)); onItemSelectedListener.onItemSelected(null,null,index,index); } private class MyAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = mInflater.inflate(R.layout.item, null); if (position == CustomSpinner.this.postion){ //选中条目的背景色 view.setBackgroundColor(Color.rgb(26,208,189)); } final TextView tv_name = (TextView) view.findViewById(R.id.tv_name); tv_name.setText(list.get(position)); //设置按钮的监听事件 view.setTag(tv_name); return view; } } @Override public void destroyDrawingCache() { if (pop != null && pop.isShowing()){ pop.dismiss(); } super.destroyDrawingCache(); }}
//布局文件layout_customspinner
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center_vertical" android:background="@color/white"> <ImageView android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="match_parent" android:src="@drawable/ic_arrow_drop_down_black_24dp" android:paddingRight="2dp" android:layout_alignParentRight="true" /> <TextView android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="match_parent" android:textColor="@color/black" android:gravity="center" android:clickable="true" android:layout_toLeftOf="@id/spinner" android:hint="数据加载中" /></RelativeLayout>
//布局item.xml
<?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:orientation="horizontal" android:gravity="center"> <TextView android:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="30dp" android:textSize="14sp" android:gravity="center" android:layout_marginRight="14dp" android:text="aaaaa" /></LinearLayout>
2 0
- Android开发自定义下拉框下拉列表
- Android 自定义下拉列表
- Android 自定义下拉列表
- android自定义Spinner下拉菜单(下拉列表框)样式
- android自定义Spinner下拉菜单(下拉列表框)样式
- android自定义Spinner下拉列表框样式
- Android自定义点击下拉列表
- Android自定义下拉列表PopupWindow
- Android自定义下拉刷新列表
- Android自定义单选多选下拉列表
- Android下拉列表框
- Android下拉列表框
- Android下拉列表框
- Android开发:Spinner下拉列表
- 自定义下拉列表框CustomSpinner
- 前端 自定义下拉列表框
- Android自定义下拉框
- Android Android自定义的下拉列表框控件
- 解决高德地图key与sha1不匹配或MD5安全码未通过问题
- 杭电4801 PocKet Cube DFS
- java 获取路径的各种方法
- PHP图片处理之图片旋转和图片翻转
- System.load 和 System.loadLibrary详解
- Android开发自定义下拉框下拉列表
- 【Android】自定义Style或Theme及自定义ActionBar
- 剑指Offer面试题10 & Leetcode191
- js 日期格式化
- JS 判断字符串中是否包含中文
- 数据库线程死锁
- JDK安装(E盘)
- 如何保存Keras模型?
- camel自定义组件