请支持原创: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. Lua的string和string库总结

    Lua有7种数据类型,分别是nil.boolean.number.string.table.function.userdata.这里我总结一下Lua的string类型和string库,复习一下,以便加 ...

  2. WPF之命令浅谈

    一.认识命令 1.1命令的特点 提到“命令”,我们应该想到命令的发出者,命令的接受者,命令的内容,准备工作,完成任务,回报工作...与事件中的发送者,接受者,消息,处理,处理,处理一一对应,如果是单纯 ...

  3. android部分开发摘要

    Async 异步  不会阻塞当前线程sync  同步 数据库是应用软件|结构化数据存储  JDBC  SQL ellipsis 省略 content provider   URI thread--lo ...

  4. 在Salesforce中进行Report和Dashboard的配置

    用Report和Dashboard去图形化比较不同Object的信息是一个十分普遍的需求,当然我们可以完全用Visual Page和Classes去自定义对应的Report和Dashboard的功能. ...

  5. 【jQuery 分页】jQuery分页功能的实现

    自写的jQuery实现分页功能的分页组件: 功能效果如下: 分页组件就是上图中的三部分, 分别放在表格上部  和下部 . 其中, 1>>>页面的代码如下: product.jsp 其 ...

  6. Oulipo (kmp)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26857   Accepted: 10709 Descript ...

  7. Fedora 防火墙关闭与开启

    重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off   或者 /sbin/chkconfig --level 2345 iptable ...

  8. 跨域请求之JSONP 一

    跨域请求之JSONP 一 跨域请求的方式有很多种, iframe document.domain window.name script XDomainRequest (IE8+) XMLHTTPReq ...

  9. javamail模拟邮箱功能发送电子邮件-基础实战篇(javamail API电子邮件实例)

    引言: JavaMail 是一种可选的.能用于读取.编写和发送电子消息的包 JavaMail jar包下载地址:http://java.sun.com/products/javamail/downlo ...

  10. AI 行为树

    by AKara 2010-12-09 @ http://blog.csdn.net/akara @ akarachen(at)gmail.com @weibo.com/akaras 谈到游戏AI,很 ...