转载地址:http://blog.csdn.net/lancees/article/details/8723702
UI系列教程第七课:自定义spinner下拉框实现的实现
说道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
- <span style="color:#3366ff;">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;
- }
- }</span>
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
- <span style="color:#3366ff;">public class CustemObject {
- public String data = "";
- @Override
- public String toString() {
- // TODO Auto-generated method stub
- return data;
- }
- }</span>
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