请支持原创: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 第三方开源下拉框:NiceSpinner

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

  4. spinner 下拉框控件

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

  5. spinner下拉框组件

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

  6. android下拉框

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

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

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

  8. jQuery自定义多选下拉框

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

  9. Android实现三级联动下拉框 下拉列表spinner

    Android实现(省.市.县)三级联动下拉框 下拉列表spinner 转载请注明出处: http://www.goteny.com/articles/2013/11/46.html http://w ...

随机推荐

  1. android 指示器 tablatyout

    <android.support.design.widget.TabLayout/>android 材料设计中新出的控件 package com.weavey.loadinglayout; ...

  2. CSS3实现边框锯齿效果

    通过CSS3的linear-gradient实现的 <div class="bg"></div> .bg{ width:300px; height:50px ...

  3. LeetCode(115) Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  4. PHP内核研究(内存管理1)

    PHP内存管理 PHP在5.3之前采用的是引用计数法 PHP在5.3之后采用了新的垃圾回收机制 操作系统在申请内存空间的时候回引发系统调用 在操作系统申请内存空间的时候,会将CPU从用户态切换到内核态 ...

  5. 终于把HDU的第一页做完了

    Today is worth to be commemorate for that I have eventually worked out all the questions in the firs ...

  6. webpack.config.js

    var webpack = require('webpack'); module.exports = { //插件项 plugins: [ new webpack.optimize.CommonsCh ...

  7. EMVTag系列3《持卡人基本信息数据》

    Ø  9F61    持卡人证件号 L:2–26 R(需求):数据应存在,在读应用数据过程中,终端不检查: (PBOC2.0第五部分中规定)芯片中持卡人姓名 5F20与持卡人姓名扩展9F0B只能使用一 ...

  8. [转]强悍的跨平台开源多媒体中心XBMC介绍

    [转]强悍的跨平台开源多媒体中心XBMC介绍 http://www.cnblogs.com/mythou/p/3220898.html 最近都在了解Android下的音视频,因为最近需要做一个多媒体中 ...

  9. PHP图形图像处理之初识GD库

    d=====( ̄▽ ̄*)b 引语 php不仅仅局限于html的输出,还可以创建和操作各种各样的图像文件,如GIF.PNG.JPEG.WBMP.XBM等. php还可以将图像流直接显示在浏览器中. 要处 ...

  10. 《我是IT一只小小鸟》读后感

    <我是IT一只小小鸟>读后感 首先,非常感谢我的老师给我推荐了这么一本书,虽然刚开始因为这门课学分太低,所以我对老师布置了字数这么多的作业存在有很大的不满,但在看了这本书后我的不满立马得到 ...