Android中自定义下拉样式Spinner

Android中自定义下拉样式Spinner


本文继续介绍android自定义控件系列,自定义Spinner控件的使用


实现思路

1.定义下拉控件布局(ListView及子控件布局)

2.自定义SpinerPopWindow类

3.定义填充数据的Adapter


效果图

技术分享


技术分享


一、定义控件布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f2f2f2"
    android:orientation="vertical"
    android:padding="5dp" >

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="#cbcbcb"
        android:orientation="vertical"
        android:padding="0.2px" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="42dp" >

            <TextView
                android:id="@+id/tv_value"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerVertical="true"
                android:layout_marginRight="50dp"
                android:background="#fff"
                android:ellipsize="end"
                android:gravity="left|center"
                android:hint="请选择"
                android:paddingLeft="10dp"
                android:singleLine="true"
                android:textColor="#ff000000"
                android:textSize="18dp" >
            </TextView>

            <LinearLayout
                android:id="@+id/bt_dropdown"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:background="#fff"
                android:gravity="center"
                android:onClick="onClick" >

                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="15dp"
                    android:background="@drawable/down_arrow" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

二、定义SpinerPopWindow类

package org.gaochun.widget;

import java.util.List;

import org.gaochun.R;
import org.gaochun.adapter.SpinerAdapter;
import org.gaochun.adapter.SpinerAdapter.IOnItemSelectListener;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
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);

    }


    //设置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自定义底部Tab,项目整体界面框架:http://blog.csdn.net/gao_chun/article/details/37903673

自定义AlertDialog提示框http://blog.csdn.net/gao_chun/article/details/37757571


转载注明出处.

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。