写点什么

Android 中自定义下拉样式 Spinner

用户头像
Android架构
关注
发布于: 2021 年 11 月 07 日

import android.widget.AdapterView.OnItemClickListener;


import android.widget.ListView;


import android.widget.PopupWindow;


/**


  • 自定义 SpinerPopWindow 类

  • @author gao_chun


*/


public class SpinerPopWindow extends PopupWindow implements OnItemClickListener{


private Context mContext;


private ListView mListView;


private SpinerAdapter mAdapter;


private IOnItemSelectListener mItemSelectListener;


public SpinerPopWindow(Context context)


{


super(context);


mContext = context;


init();


}


public void setItemListener(IOnItemSelectListener listener){


mItemSelectListener = listener;


}


public void setAdatper(SpinerAdapter 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 void refreshData(List<String> 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);


}


}


}


三、定义 Adapter




package org.gaochun.adapter;


import java.util.List;


import org.gaochun.R;


import android.content.Context;


import android.view.LayoutInflater;


import android.view.View;


import android.view.ViewGroup;


import android.widget.BaseAdapter;


import android.widget.TextView;


public class SpinerAdapter extends BaseAdapter {


public static interface IOnItemSelectListener{


public void onItemClick(int pos);


};


private List<String> mObjects;


private LayoutInflater mInflater;


public SpinerAdapter(Context context,List<String> mObjects){


this.mObjects = mObjects;


mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


}


public void refreshData(List<String> objects, int selIndex){


mObjects = objects;


if (selIndex < 0){


selIndex = 0;


}


if (selIndex >= mObjects.size()){


selIndex = mObjects.size() - 1;


}


}


@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(mObjects.get(pos));


return convertView;


}


public static class ViewHolder


{


public TextView mTextView;


}


}


四、调用示例




public class MainActivity extends Activity implements OnClickListener,SpinerAdapter.IOnItemSelectListener{


private List<String> mListType = new ArrayList<String>(); //类型列表


private TextView mTView;


private SpinerAdapter mAdapter;


@Override


protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);


setContentView(R.layout.activity_search);


mTView = (TextView) findViewById(R.id.tv_value);


//初始化数据


String[] names = getResources().getStringArray(R.array.array_name);


for(int i = 0; i < names.length; i++){


mListType.add(names[i]);


}


mAdapter = new SpinerAdapter(this,mListType);


mAdapter.refreshData(mListType,0);


//显示第一条数据


//mTView.setText(names[0]);


//初始化 PopWindow


mSpinerPopWindow = new SpinerPopWindow(this);


mSpinerPopWindow.setAdatper(mAdapter);


mSpinerPopWindow.setItemListener(this


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


);


}


//设置 PopWindow


private SpinerPopWindow mSpinerPopWindow;


private void showSpinWindow(){


Log.e("", "showSpinWindow");


mSpinerPopWindow.setWidth(mTView.getWidth());


mSpinerPopWindow.showAsDropDown(mTView);


}


@Override


public void onClick(View v) {


switch (v.getId()) {


case R.id.bt_dropdown:


showSpinWindow();


break;


}


}


/* (non-Javadoc)


  • @see org.gaochun.adapter.SpinerAdapter.IOnItemSelectListener#onItemClick(int)


*/


@Override


public void onItemClick(int pos) {


// TODO Auto-generated method stub


if (pos >= 0 && pos <= mListType.size()){


String value = mListType.get(pos);


mTView.setText(value.toString());


}


}


}


源码下载:http://download.csdn.net/download/gao_chun/8653069


五、自定义控件系列




Android 项目中使用自定义进度加载 Dialog:http://blog.csdn.net/gao_chun/article/details/45270031


ListView中按钮监听器 设置 及 优化http://blog.csdn.net/gao_chun/article/details/41249131


Android 项目中自定义顶部标题栏:http://blog.csdn.net/gao_chun/article/details/45255929


Android自定义设置圆形图片控件http://blog.csdn.net/gao_chun/article/details/39207557

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Android中自定义下拉样式Spinner