zoukankan      html  css  js  c++  java
  • Android 自定义spinner下拉框实现的实现

    请支持原创: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)
        {
            if (list != null && selIndex  != -1)
            {
                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);
            for(int i = 0; i < names.length; i++){
                nameList.add(names[i]);
            }
            
    
            mSpinerPopWindow = new SpinerPopWindow(this);
            mSpinerPopWindow.refreshData(nameList, 0);
            mSpinerPopWindow.setItemListener(this);
        }
    
    
        @Override
        public void onClick(View view) {
            switch(view.getId()){
            case R.id.bt_dropdown:
                showSpinWindow();
                break;
            }
        }
        
    
        private void setHero(int pos){
            if (pos >= 0 && 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 int mSelectItem = 0;
            
         private LayoutInflater mInflater;
        
         public  AbstractSpinerAdapter(Context context){
             init(context);
         }
         
         public void refreshData(List<T> objects, int selIndex){
             mObjects = objects;
             if (selIndex < 0){
                 selIndex = 0;
             }
             if (selIndex >= mObjects.size()){
                 selIndex = mObjects.size() - 1;
             }
             
             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 (list != null && selIndex  != -1)
            {
                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);
            for(int i = 0; i < names.length; i++){
                CustemObject object = new CustemObject();
                object.data = names[i];
                nameList.add(object);
            }
            
            
            mAdapter = new CustemSpinerAdapter(this);
            mAdapter.refreshData(nameList, 0);
    
            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){
            if (pos >= 0 && 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

  • 相关阅读:
    20080619 SQL SERVER 输入 NULL 的快捷键
    20090406 Adobe的“此产品的许可已停止工作”错误的解决办法
    20080908 Office Powerpoint 2007 不能输入中文的解决办法
    20080831 ClearGertrude Blog Skin 's cnblogs_code class
    20080603 Facebook 平台正式开放
    20080519 安装 Microsoft SQL Server 2000 时提示 创建挂起的文件操作
    test
    Linux—fork函数学习笔记
    SOA的设计理念
    Why BCP connects to SQL Server instance which start with account of Network Service fail?
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/6233518.html
Copyright © 2011-2022 走看看