安卓自定义下拉框---Spinner的实现

效果图:


实现思路:

1.定义下拉控件布局(ListView及子控件布局)

2.自定义SpinerPopWindow类

3.定义填充数据的Adapter

一、定义控件布局:

1.  activity_main.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#f2f2f2"  
  6.     android:orientation="vertical"  
  7.     android:padding="5dp" >  
  8.   
  9.     <LinearLayout  
  10.         android:id="@+id/layout"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_margin="5dp"  
  14.         android:background="@drawable/bg_linearlayout"  
  15.         android:orientation="vertical"  
  16.         android:padding="0.2px" >  
  17.   
  18.         <RelativeLayout  
  19.             android:id="@+id/relativelayout"  
  20.             android:padding="2dp"  
  21.             android:onClick="onClick"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="42dp" >  
  24.   
  25.             <TextView  
  26.                 android:id="@+id/tv_value"  
  27.                 android:layout_width="match_parent"  
  28.                 android:layout_height="match_parent"  
  29.                 android:layout_centerVertical="true"  
  30.                 android:layout_marginRight="50dp"  
  31.                 android:background="#fff"  
  32.                 android:ellipsize="end"  
  33.                 android:gravity="left|center"  
  34.                 android:hint="请选择"  
  35.                 android:paddingLeft="10dp"  
  36.                 android:singleLine="true"  
  37.                 android:textColor="#ff000000"  
  38.                 android:textSize="18sp" >  
  39.             </TextView>  
  40.   
  41.             <LinearLayout  
  42.                 android:id="@+id/bt_dropdown"  
  43.                 android:layout_width="50dp"  
  44.                 android:layout_height="match_parent"  
  45.                 android:layout_alignParentRight="true"  
  46.                 android:background="#fff"  
  47.                 android:gravity="center"  
  48.                 >  
  49.   
  50.                 <ImageView  
  51.                     android:layout_width="20dp"  
  52.                     android:layout_height="20dp"  
  53.                     android:background="@drawable/arrow_down" />  
  54.             </LinearLayout>  
  55.         </RelativeLayout>  
  56.     </LinearLayout>  
  57.   
  58. </RelativeLayout>  
2. spiner_window_layout.xml

[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="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_alignParentRight="true"  
  6.     android:background="#ffffff"  
  7.     android:orientation="vertical">  
  8.   
  9.     <ListView  
  10.         android:id="@+id/listview"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:background="@drawable/bg_linearlayout"  
  14.         android:cacheColorHint="#00000000"  
  15.         android:scrollbars="none"></ListView>  
  16.   
  17. </LinearLayout>  

3. spiner_item_layout.xml

[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="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#00000000"  
  6.     android:gravity="center_vertical"  
  7.     android:minHeight="40dp">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textView"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:background="#00000000"  
  14.         android:gravity="center"  
  15.         android:textColor="@color/black"  
  16.         android:textSize="18sp" />  
  17.   
  18. </LinearLayout>  

二、定义SpinerPopWindow类

  SpinerPopWindow.java 类

[java]  view plain  copy
  1. package com.hykjsjkj.customspiner;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.ColorDrawable;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup.LayoutParams;  
  8. import android.widget.AdapterView;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10. import android.widget.ListView;  
  11. import android.widget.PopupWindow;  
  12. import java.util.List;  
  13. /** 
  14.  * Copyright: Copyright (c) 2017-2025 
  15.  * Company: 
  16.  * 
  17.  * @author: 赵小贱 
  18.  * @date: 2017/8/23 
  19.  * describe: 
  20.  */  
  21. public class SpinerPopWindow extends PopupWindow implements OnItemClickListener {  
  22.   
  23.     private Context mContext;  
  24.     private ListView mListView;  
  25.     private SpinerAdapter mAdapter;  
  26.     private SpinerAdapter.IOnItemSelectListener mItemSelectListener;  
  27.       
  28.     public SpinerPopWindow(Context context) {  
  29.         super(context);  
  30.   
  31.         mContext = context;  
  32.         init();  
  33.     }  
  34.       
  35.     public void setItemListener(SpinerAdapter.IOnItemSelectListener listener) {  
  36.         mItemSelectListener = listener;  
  37.     }  
  38.   
  39.     public void setAdatper(SpinerAdapter adapter) {  
  40.         mAdapter = adapter;  
  41.         mListView.setAdapter(mAdapter);  
  42.     }  
  43.   
  44.   
  45.     private void init() {  
  46.         View view = LayoutInflater.from(mContext).inflate(R.layout.spiner_window_layout, null);  
  47.         setContentView(view);  
  48.         setWidth(LayoutParams.WRAP_CONTENT);  
  49.         setHeight(LayoutParams.WRAP_CONTENT);  
  50.   
  51.         setFocusable(true);  
  52.         ColorDrawable dw = new ColorDrawable(0x00);  
  53.         setBackgroundDrawable(dw);  
  54.   
  55.   
  56.         mListView = (ListView) view.findViewById(R.id.listview);  
  57.         mListView.setOnItemClickListener(this);  
  58.     }  
  59.   
  60.   
  61.     public void refreshData(List<String> list, int selIndex) {  
  62.         if (list != null && selIndex != -1) {  
  63.             if (mAdapter != null) {  
  64.                 mAdapter.refreshData(list, selIndex);  
  65.             }  
  66.         }  
  67.     }  
  68.       
  69.     @Override  
  70.     public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {  
  71.         dismiss();  
  72.         if (mItemSelectListener != null) {  
  73.             mItemSelectListener.onItemClick(pos);  
  74.         }  
  75.     }  
  76.       
  77. }  

三、定义Adapter

SpinerAdapter.java

[java]  view plain  copy
  1. package com.hykjsjkj.customspiner;  
  2.   
  3. import android.content.Context;  
  4. import android.view.LayoutInflater;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7. import android.widget.BaseAdapter;  
  8. import android.widget.TextView;  
  9.   
  10. import java.util.List;  
  11.   
  12. /** 
  13.  * Copyright: Copyright (c) 2017-2025 
  14.  * Company: 
  15.  * 
  16.  * @author: 赵小贱 
  17.  * @date: 2017/8/23 
  18.  * describe: 
  19.  */  
  20. public class SpinerAdapter extends BaseAdapter {  
  21.   
  22.     public static interface IOnItemSelectListener {  
  23.         public void onItemClick(int pos);  
  24.     }  
  25.   
  26.     ;  
  27.   
  28.     private List<String> mObjects;  
  29.   
  30.     private LayoutInflater mInflater;  
  31.   
  32.     public SpinerAdapter(Context context, List<String> mObjects) {  
  33.         this.mObjects = mObjects;  
  34.         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  35.     }  
  36.   
  37.   
  38.     public void refreshData(List<String> objects, int selIndex) {  
  39.         mObjects = objects;  
  40.         if (selIndex < 0) {  
  41.             selIndex = 0;  
  42.         }  
  43.         if (selIndex >= mObjects.size()) {  
  44.             selIndex = mObjects.size() - 1;  
  45.         }  
  46.     }  
  47.   
  48.   
  49.     @Override  
  50.     public int getCount() {  
  51.   
  52.         return mObjects.size();  
  53.     }  
  54.   
  55.     @Override  
  56.     public Object getItem(int pos) {  
  57.         return mObjects.get(pos).toString();  
  58.     }  
  59.   
  60.     @Override  
  61.     public long getItemId(int pos) {  
  62.         return pos;  
  63.     }  
  64.   
  65.     @Override  
  66.     public View getView(int pos, View convertView, ViewGroup arg2) {  
  67.         ViewHolder viewHolder;  
  68.   
  69.         if (convertView == null) {  
  70.             convertView = mInflater.inflate(R.layout.spiner_item_layout, null);  
  71.             viewHolder = new ViewHolder();  
  72.             viewHolder.mTextView = (TextView) convertView.findViewById(R.id.textView);  
  73.             convertView.setTag(viewHolder);  
  74.         } else {  
  75.             viewHolder = (ViewHolder) convertView.getTag();  
  76.         }  
  77.   
  78.         //Object item =  getItem(pos);  
  79.         viewHolder.mTextView.setText(mObjects.get(pos));  
  80.   
  81.         return convertView;  
  82.     }  
  83.   
  84.   
  85.     public static class ViewHolder {  
  86.         public TextView mTextView;  
  87.     }  
  88.   
  89. }  

四、调用示例

MainActivity
[java]  view plain  copy
  1. package com.hykjsjkj.customspiner;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.RelativeLayout;  
  7. import android.widget.TextView;  
  8. import java.util.ArrayList;  
  9. import java.util.List;  
  10.   
  11. /** 
  12.  * Copyright: Copyright (c) 2017-2025 
  13.  * Company: 
  14.  * 
  15.  * @author: 赵小贱 
  16.  * @date: 2017/8/23 
  17.  * describe: 
  18.  */  
  19. public class MainActivity extends Activity implements View.OnClickListener, SpinerAdapter.IOnItemSelectListener {  
  20.   
  21.     private List<String> mListType = new ArrayList<String>();  //类型列表  
  22.     private TextView mTView;  
  23.     private SpinerAdapter mAdapter;  
  24.     private RelativeLayout relativeLayout;  
  25.     private SpinerPopWindow mSpinerPopWindow;  
  26.   
  27.     @Override  
  28.     protected void onCreate(Bundle savedInstanceState) {  
  29.   
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_main);  
  32.         mTView = (TextView) findViewById(R.id.tv_value);  
  33.         relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);  
  34.   
  35.   
  36.         //初始化数据  
  37.         mListType.add("大家好!");  
  38.         mListType.add("老司机开车啦");  
  39.         mListType.add("快上车");  
  40.         mListType.add("快下班了");  
  41.         mListType.add("下班回家做好吃的");  
  42.         mListType.add("啦啦啦啦···");  
  43.   
  44.         mAdapter = new SpinerAdapter(this, mListType);  
  45.         mAdapter.refreshData(mListType, 0);  
  46.   
  47.   
  48.         //初始化PopWindow  
  49.         mSpinerPopWindow = new SpinerPopWindow(this);  
  50.         mSpinerPopWindow.setAdatper(mAdapter);  
  51.         mSpinerPopWindow.setItemListener(this);  
  52.   
  53.     }  
  54.   
  55.   
  56.   
  57.     //设置PopWindow  
  58.     private void showSpinWindow() {  
  59.         //设置mSpinerPopWindow显示的宽度  
  60.         mSpinerPopWindow.setWidth(relativeLayout.getWidth());  
  61.         //设置显示的位置在哪个控件的下方  
  62.         mSpinerPopWindow.showAsDropDown(relativeLayout);  
  63.     }  
  64.   
  65.   
  66.     @Override  
  67.     public void onClick(View v) {  
  68.         switch (v.getId()) {  
  69.             case R.id.relativelayout:  
  70.                 showSpinWindow();//显示SpinerPopWindow  
  71.                 break;  
  72.         }  
  73.     }  
  74.   
  75.   
  76.     /** 
  77.      * SpinerPopWindow中的条目点击监听 
  78.      * @param pos 
  79.      */  
  80.     @Override  
  81.     public void onItemClick(int pos) {  
  82.   
  83.         String value = mListType.get(pos);  
  84.         mTView.setText(value.toString());  
  85.   
  86.     }  
  87.   
  88. }  
以上就是所有的代码了,希望对大家有所帮助,下面会附上源码,有需要的可以下载来参考:



示例代码下载:http://download.csdn.net/download/zhaoxiaojian1213/9949348

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值