Spinner的简单使用

Posted 钱大少丶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spinner的简单使用相关的知识,希望对你有一定的参考价值。

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.lantianbaiyunyk.asus.spnner.MainActivity">

   <Spinner
       android:id="@+id/spinner"
       android:layout_width="match_parent"
       android:layout_height="50dp">

   </Spinner>
</LinearLayout>

 

 

MainActivity
public class MainActivity extends AppCompatActivity {

    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ilit();
    }

    private void ilit() {
        final List<String> datas = new ArrayList<>();
        for (int i = 0; i <10 ; i++) {
            datas.add("数据"+i);
        }
        MyAdapter adapter = new MyAdapter(this);
        spinner.setAdapter(adapter);
        adapter.setDatas(datas);
        /**选项选择监听*/
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "点击了" + datas.get(position), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void initView() {
        spinner = (Spinner) findViewById(R.id.spinner);
    }
}

 

Adapter:

public class MyAdapter extends BaseAdapter{
    List<String> datas = new ArrayList<>();
    Context mContext;
    public MyAdapter(Context context) {
        this.mContext = context;
    }

    public void setDatas(List<String> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas==null?0:datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas==null?null:datas.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodler hodler = null;
        if (convertView == null) {
            hodler = new ViewHodler();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null);
            hodler.mTextView = (TextView) convertView;
            convertView.setTag(hodler);
        } else {
            hodler = (ViewHodler) convertView.getTag();
        }

        hodler.mTextView.setText(datas.get(position));

        return convertView;
    }

    private static class ViewHodler{
        TextView mTextView;
    }
}

 

item:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp">


</TextView>

 

以上是关于Spinner的简单使用的主要内容,如果未能解决你的问题,请参考以下文章

根据在 Spinner 中选择的数字,相同的片段应该出现在另一个之下。如何?

怎么用Bootstrap 写出加减的符号

Android Spinner 下拉箭头不显示

根据另一个 Spinner 选择 (LiveData) 过滤 Spinner 的 Observed ViewModel 数据

Android Spinner简易式使用小结

Android控件之Spinner简单使用