请支持原创: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 != -)
{
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 = ; 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){
if (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 int mSelectItem = ; private LayoutInflater mInflater; public AbstractSpinerAdapter(Context context){
init(context);
} public void refreshData(List<T> objects, int selIndex){
mObjects = objects;
if (selIndex < ){
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 (list != null && 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);
for(int i = ; 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){
if (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. spinner控件需要向xml资源文件中添加spinner标签,如下: <Spinner android:id="@+ ...

  4. Android UI自定义Spinner下拉框(用popuwindow实现)-转

    定义出第一个图片的布局和弹出框(一个listView)的布局,,这里就不在多说了~ListView需要自己定义一个MyspinnerAdapter~做好这些准备之后,就是弹出框的实现了~  prote ...

  5. 030 Android 第三方开源下拉框:NiceSpinner的使用+自定义Button样式+shape绘制控件背景图+图片选择器(selector)

    1.NiceSpinner下拉框控件介绍 Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Android原生提供的下拉框 ...

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

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

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

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

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

     Android第三方开源下拉框:NiceSpinner Android原生的下拉框Spinner基本上可以满足Android开发对于下拉选项的设计需求,但现在越来越流行的下拉框不满足于Andro ...

  9. 自定义SWT控件一之自定义单选下拉框

    一.自定义下拉控件 自定义的下拉框,是自定义样式的,其中的下拉框使用的是独立的window,非复选框的下拉框双击单机其它区域或选择完之后,独立window构成的下拉框会自动消失. package co ...

随机推荐

  1. HDU 2831 (贪心)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2831 题目大意:植物大战僵尸.给定种植植物时间间隔t,以及每个僵尸的到达时间v,生命d.问是否能赢. ...

  2. QtCreator下运行opencv出现realloc():pointer invalid

    解决办法是将qmake换成4.8的,qmake5.2的支持opencv支持的不是很好

  3. CDOJ 1431 不是图论 Label:Tarjan || Kosarajn

    Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Description 给出一个nn个点, ...

  4. 洛谷 P1030 求先序排列 Label:None

    题目描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=8). 输入输出格式 输入格式: 2行,均为大写字母组成的字符串,表示一棵二叉树的中序与后序 ...

  5. AIX日常维护

    1  /etc/security/limits与limit命令 AIX 5.3上 下面是文件/etc/security/limits文件里面有关软限制和硬限制的部分. * * Sizes are in ...

  6. CentOS 6.7安装Mysql 5.7

    Step1: 检测系统是否自带安装mysql # yum list installed | grep mysql Step2: 删除系统自带的mysql及其依赖命令: # yum -y remove ...

  7. Oracle connect by 树查询之二

    先用scott用户下的emp表做实验.emp表有个字段,一个是empno(员工编号),另一个是mgr(上级经理编号)下面是表中所有数据 1 select * from emp start with e ...

  8. Logistic Regression Vs Decision Trees Vs SVM: Part I

    Classification is one of the major problems that we solve while working on standard business problem ...

  9. 科技来电:不容错过的win10轻度美化攻略

    作为微软不择手段采用弹框.限时.警告等方式强推win10的结果, win10市场占有率大增,微软高兴了,可用户苦了,吹洗剪+杀马特般的metro&传统混血开始菜单.鼻青脸肿般的任务栏.单一颜色 ...

  10. js 检查是否为手机端

    let isMobile = function(){ let userAgentInfo = navigator.userAgent; let Agents = new Array("And ...