请支持原创:http://blog.csdn.NET/geniuseoe2012/article/details/8723702

说到Android下拉框spineer,框架中虽有现成的控件,但实际效果可能并不是我们所需要的那种,如下图:

其实我们更需要的是像WEB那种风格,如图所示:

其实实现也很简单,就是自定义个popwindow就可以了

下面贴上代码片段:

public class SpinerPopWindow extends PopupWindow implements OnItemClickListener{

    private Context mContext;
    private ListView mListView;
    private NormalSpinerAdapter mAdapter;
    private IOnItemSelectListener mItemSelectListener;

    public SpinerPopWindow(Context context)
    {
        super(context);

        mContext = context;
        init();
    }

    public void setItemListener(IOnItemSelectListener listener){
        mItemSelectListener = listener;
    }

    private void init()
    {
        View view = LayoutInflater.from(mContext).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);

        mAdapter = new NormalSpinerAdapter(mContext);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
    }

    public void refreshData(List<String> list, int selIndex)
    {
        )
        {
            mAdapter.refreshData(list, selIndex);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
        dismiss();
        if (mItemSelectListener != null){
            mItemSelectListener.onItemClick(pos);
        }
    }

}

主界面调用代码:

public class SpinerWindowDemoActivity extends Activity implements OnClickListener, AbstractSpinerAdapter.IOnItemSelectListener{
    /** Called when the activity is first created. */

    private View mRootView;
    private TextView mTView;
    private ImageButton mBtnDropDown;
    private List<String> nameList = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setupViews();
    }

    private void setupViews(){
        mRootView = findViewById(R.id.rootView);

        mTView = (TextView) findViewById(R.id.tv_value);
        mBtnDropDown = (ImageButton) findViewById(R.id.bt_dropdown);
        mBtnDropDown.setOnClickListener(this);

        String[] names = getResources().getStringArray(R.array.hero_name);
        ; i < names.length; i++){
            nameList.add(names[i]);
        }

        mSpinerPopWindow = new SpinerPopWindow(this);
        mSpinerPopWindow.refreshData(nameList, );
        mSpinerPopWindow.setItemListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
        case R.id.bt_dropdown:
            showSpinWindow();
            break;
        }
    }

    private void setHero(int pos){
         && pos <= nameList.size()){
            String value = nameList.get(pos);

            mTView.setText(value);
        }
    }

    private SpinerPopWindow mSpinerPopWindow;
    private void showSpinWindow(){
        Log.e("", "showSpinWindow");
        mSpinerPopWindow.setWidth(mTView.getWidth());
        mSpinerPopWindow.showAsDropDown(mTView);
    }

    @Override
    public void onItemClick(int pos) {
        setHero(pos);
    }

}

就这么简单,具体UI美化大家再修改下xml就可以了

下面附上工程链接:

http://download.csdn.net/detail/geniuseoe2012/5184664

welcome to join android-develop group:298044305

more brilliant,Please pay attention to my CSDN blog -->http://blog.csdn.net/geniuseoe2012

上一课:Lance老师UI系列教程第六课->微信聊天气泡界面的实现

/*=============================================================================*/

为使SpinerPopWindow更为通用,特修改或增加以下几个类

AbstractSpinerAdapter

 public abstract class AbstractSpinerAdapter<T> extends BaseAdapter {

    public static interface IOnItemSelectListener{
        public void onItemClick(int pos);
    };

     private Context mContext;
     private List<T> mObjects = new ArrayList<T>();
     ;

     private LayoutInflater mInflater;

     public  AbstractSpinerAdapter(Context context){
         init(context);
     }

     public void refreshData(List<T> objects, int selIndex){
         mObjects = objects;
         ){
             selIndex = ;
         }
         if (selIndex >= mObjects.size()){
             selIndex = mObjects.size() - ;
         }

         mSelectItem = selIndex;
     }

     private void init(Context context) {
            mContext = context;
            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }

    @Override
    public int getCount() {

        return mObjects.size();
    }

    @Override
    public Object getItem(int pos) {
        return mObjects.get(pos).toString();
    }

    @Override
    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup arg2) {
         ViewHolder viewHolder;

         if (convertView == null) {
             convertView = mInflater.inflate(R.layout.spiner_item_layout, null);
             viewHolder = new ViewHolder();
             viewHolder.mTextView = (TextView) convertView.findViewById(R.id.textView);
             convertView.setTag(viewHolder);
         } else {
             viewHolder = (ViewHolder) convertView.getTag();
         }

         Object item =  getItem(pos);
         viewHolder.mTextView.setText(item.toString());

         return convertView;
    }

    public static class ViewHolder
    {
        public TextView mTextView;
    }

}

SpinerPopWindow

public class SpinerPopWindow extends PopupWindow implements OnItemClickListener{

    private Context mContext;
    private ListView mListView;
    private AbstractSpinerAdapter mAdapter;
    private IOnItemSelectListener mItemSelectListener;

    public SpinerPopWindow(Context context)
    {
        super(context);

        mContext = context;
        init();
    }

    public void setItemListener(IOnItemSelectListener listener){
        mItemSelectListener = listener;
    }

    public void setAdatper(AbstractSpinerAdapter adapter){
        mAdapter = adapter;
        mListView.setAdapter(mAdapter);
    }

    private void init()
    {
        View view = LayoutInflater.from(mContext).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.setOnItemClickListener(this);
    }

    public <T> void refreshData(List<T> list, int selIndex)
    {
        )
        {
            if (mAdapter != null){
                mAdapter.refreshData(list, selIndex);
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
        dismiss();
        if (mItemSelectListener != null){
            mItemSelectListener.onItemClick(pos);
        }
    }

}

CustemObject

public class CustemObject {

    public String data = "";

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return data;
    }

}

CustemSpinerAdapter

public class CustemSpinerAdapter extends AbstractSpinerAdapter<CustemObject>{

    public CustemSpinerAdapter(Context context) {
        super(context);
    }

}

SpinerWindowDemoActivity

public class SpinerWindowDemoActivity extends Activity implements OnClickListener, AbstractSpinerAdapter.IOnItemSelectListener{
    /** Called when the activity is first created. */

    private View mRootView;
    private TextView mTView;
    private ImageButton mBtnDropDown;
    private List<CustemObject> nameList = new ArrayList<CustemObject>();
    private AbstractSpinerAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setupViews();
    }

    private void setupViews(){
        mRootView = findViewById(R.id.rootView);

        mTView = (TextView) findViewById(R.id.tv_value);
        mBtnDropDown = (ImageButton) findViewById(R.id.bt_dropdown);
        mBtnDropDown.setOnClickListener(this);

        String[] names = getResources().getStringArray(R.array.hero_name);
        ; i < names.length; i++){
            CustemObject object = new CustemObject();
            object.data = names[i];
            nameList.add(object);
        }

        mAdapter = new CustemSpinerAdapter(this);
        mAdapter.refreshData(nameList, );

        mSpinerPopWindow = new SpinerPopWindow(this);
        mSpinerPopWindow.setAdatper(mAdapter);
        mSpinerPopWindow.setItemListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
        case R.id.bt_dropdown:
            showSpinWindow();
            break;
        }
    }

    private void setHero(int pos){
         && pos <= nameList.size()){
            CustemObject value = nameList.get(pos);

            mTView.setText(value.toString());
        }
    }

    private SpinerPopWindow mSpinerPopWindow;
    private void showSpinWindow(){
        Log.e("", "showSpinWindow");
        mSpinerPopWindow.setWidth(mTView.getWidth());
        mSpinerPopWindow.showAsDropDown(mTView);
    }

    @Override
    public void onItemClick(int pos) {
        setHero(pos);
    }

}

自定义一个类重写toString方法(CustemObject),自定义一个继承自AbstractSpinerAdapter的类确定对象类型(CustemSpinerAdapter)

构造具体的AbstractSpinerAdapter类对象(CustemSpinerAdapter)

通过refreshData填充数据,然后调用SpinerPopWindow的setAdatper设置数据源,OK大功告成!

新工程链接如下:http://download.csdn.net/detail/geniuseoe2012/5412647

OK,本节课到此为止!欲知更多Android-UI技巧,请关注窝的下一堂课~

更多精彩尽在http://blog.csdn.net/geniuseoe2012

welcome to join android develop group:298044305

Android 自定义spinner下拉框实现的实现的更多相关文章

  1. Android自定义spinner下拉框实现的实现

    一:前言 本人参考博客:http://blog.csdn.net/jdsjlzx/article/details/41316417 最近在弄一个下拉框,发现Android自带的很难实现我的功能,于是去 ...

  2. Android 自定义spinner下拉框实现

    一:前言本人参考博客:http://blog.csdn.net/jdsjlzx/article/details/41316417 最近在弄一个下拉框,发现Android自带的很难实现我的功能,于是去网 ...

  3. 一分钟掌握Android spinner下拉框

    Android 自带的spinner下拉框控件是一个不错的系统控件.主要有两种实现方式: 1.静态的spinner 在res\values中加入一个city资源数组文件 2 <resources ...

  4. Android 第三方开源下拉框:NiceSpinner

    Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框Spinner所提供的设计样式,而改用自定 ...

  5. spinner 下拉框控件

    spinnerMode=dropdown时,为下拉模式spinnerMode=dialog时,会在界面中间弹出Android:popupBackground=”#f0000000”,可以去除spinn ...

  6. spinner下拉框组件

    方法一代码如下: <string-array name="city_name"> <item>浙江</item> <item>上海& ...

  7. android下拉框

    XML: <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...

  8. 自定义样式的select下拉框深入探索

    第一个版本: 首先实现自定义select下拉框应该具有的功能,我是选择将原来的select隐藏掉,自己在jquery代码中动态写进去<dl><dd><dt>这样的结 ...

  9. jQuery自定义多选下拉框

    项目中需要自定义一个下拉框多选插件,业务问题还是自己实现比较好 通过$.fn 向jQuery添加新的方法 下拉数据通过参数传递进去,通过调用该插件时接收,选择后的确定与取消事件采用事件传递方式 代码如 ...

随机推荐

  1. 2013 Visual Studio Magazine读者选择奖界面框架类获奖情况

    2013 Visual Studio Magazine读者选择奖已经正式揭晓了!据了解,截至今年此奖项已经评选了21次,非常值得.NET开发人员信赖和参考.此次评选共有400多个产品角逐28个分类的奖 ...

  2. java util包概述

    util是utiliy的缩写,意为多用途的,工具性质的包这个包中主要存放了:集合类(如ArrayList,HashMap等),随机数产生类,属性文件读取类,定时器类等类.这些类极大方便了Java编程, ...

  3. 用andtoid studio获取天气数据并解析适配

    1.申请拿到数据 可以用“聚合数据” 2.在android studio中导入需要的jar包 复制—>app—>libs—>粘贴—>右击—>Add As Library… ...

  4. ajax往后台传json格式数据报415错误

    问题描述: ajax往后台传json格式数据报415错误,如下图所示 页面代码 function saveUser(){ var uuId = document.getElementById(&quo ...

  5. UIExtendedEdge

    在IOS7以后 ViewController 开始使用全屏布局的,而且是默认的行为通常涉及到布局.就离不开这个属性 edgesForExtendedLayout,它是一个类型为UIExtendedEd ...

  6. Error message “Assembly must be registered in isolation” when registering Plugins in Microsoft Dynamics CRM 2011 2013 解决办法

    Error message “Assembly must be registered in isolation” when registering Plugins in Microsoft Dynam ...

  7. 机器学习算法-K-means聚类

    引文: k均值算法是一种聚类算法.所谓聚类.他是一种无监督学习,将类似的对象归到同一个蔟中.蔟内的对象越类似,聚类的效果越好. 聚类和分类最大的不同在于.分类的目标事先已知.而聚类则不一样. 由于其产 ...

  8. django中views中方法的request参数

    知其然亦要知其所以然 views每个方法的参数都是request,那么问题来了,request为何物? 首先,几乎每个方法都是取数据(无论是从数据库,还是从第三方接口),然后进行一定的处理,之后传给前 ...

  9. python正则表达式二[转]

    原文:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一 ...

  10. [No0000167]CPU内部组成结构及指令执行过程

    计算机的基本硬件系统由运算器.控制器.存储器和输入.输出设备五大部件组成.运算器和控制器等部件被集成在一起统称为中央处理单元(Central Processing Unit,CPU). CPU的功能 ...