最新消息:欢迎访问Android开发中文站!商务联系微信:loading_in

Spinner的三种用法

新手入门 loading 4112浏览 0评论

方式一、如果是固定的数据,直接在values下创建arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="letter">
        <item>A</item>
        <item>B</item>
        <item>C</item>
        <item>D</item>
        <item>E</item>
        <item>F</item>
        <item>G</item>
    </string-array>
</resources>

然后在布局文件里通过android:entries引用就可以了

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:spinnerMode="dialog"
    android:entries="@array/letter"
    android:prompt="@string/spinner_title"/>
  1. android:spinnerMode=”dialog”表示Spinner的样式是dialog,android:prompt可以设置dialog的标题(注意必须在string资源下引用,不然程序会崩掉)

方式二、通过适配器来填充参数,继承BaseAdapter,这种方式比较简单,网上也有很多参考,就不介绍了。

我们主要来介绍方式三:

实现SpinnerAdaper接口,其实跟继承BaseAdapter差不多,只不过有几个坑的地方需要注意:

先贴代码

public class MyAdapter implements SpinnerAdapter {

    private ArrayList<String> list;
    private Context context;

    public MyAdapter(ArrayList<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView== null){
            holder= new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item,null);
            holder.textView= convertView.findViewById(R.id.tv_name);
            holder.imageView= convertView.findViewById(R.id.iv_image);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(list.get(position));
        holder.imageView.setImageResource(R.mipmap.ic_launcher);
        return convertView;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView== null){
            holder= new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.item,null);
            holder.textView= convertView.findViewById(R.id.tv_name);
            holder.imageView= convertView.findViewById(R.id.iv_image);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }

        holder.textView.setText(list.get(position));
        holder.imageView.setImageResource(R.mipmap.ic_launcher);
        return convertView;
    }

    private class ViewHolder{
        TextView textView;
        ImageView imageView;
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

需要注意:

注意1:

@Override
public int getItemViewType(int position) {
    return 0;
}

@Override
public int getViewTypeCount() {
    return 0;
}

getItemViewType和getViewTypeCount这两个方法,默认返回的是0,需要改为1。不然在setAdapter的时候就会报Caused by: java.lang.IllegalArgumentException: Spinner adapter view type count must be 1。

注意2:

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    return null;
}

相比BaseAdapter,最大的不同是SpinnerAdapter多了getDropDownView方法,这里必须跟getView方法里写得一样,不然当你点击Spinner控件的时候,程序也会崩溃。

好了,下面贴上MainActivity的代码:

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.spinner)
    Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        ArrayList<String> list= new ArrayList<>();
        for (int i=0;i<30;i++){
            list.add("Name"+i);
        }
        MyAdapter myAdapter= new MyAdapter(list,this);
        spinner.setAdapter(myAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String str = (String) spinner.getSelectedItem();
                Toast.makeText(MainActivity.this, "选择了" + str, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Log.e("TAG_MainActivity", parent.toString());
            }
        });
    }

}

下面分别是使用arrays.xml和SpinnerAdapter的效果图:

github地址:https://github.com/HeJiaomy/SpinnerDemo

转载请注明:Android开发中文站 » Spinner的三种用法

您必须 登录 才能发表评论!