Android Spinner的简单使用
时间:2018-03-12作者:华清远见
Spinner这个控件的简单使用,这个控件其实很像listview,加载的数据和listview很相似,只是展示的效果不一样,其实他们的父类都是 AdapterView,所以使用就比较类似了。Spinner就是一个弹出的下拉选择菜单。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="//schemas.android.com/apk/res/android" android:layout_width="match_parent" android:gravity="center_horizontal" android:padding="10dp" android:layout_height="match_parent"> <Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout> MainActivity的代码 package org.skxy.www.sourceanalyze; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.Spinner; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /**Spinner主要用于下来菜单的选项,数据加载和绑定很类似Listview,但是应用场景不同*/ Spinner spinner = (Spinner) findViewById(R.id.spinner); 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) { } }); } } 适配器 package org.skxy.www.sourceanalyze; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * ClassName : MyAdapter * Created by: skxy on 2016/11/22. * DES :适配器 */ 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; } } 条目布局 <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="//schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
</TextView> 最终效果
相关资讯
发表评论
|