凭借之前java的基础,在公司有机会一边学习一边进行安卓的开发,这个系列希望能够鞭策自己能够把项目中的一些实用小功能进行总结学习,也加深对Android开发的理解。
进入正题,这个单选小功能我放到了一个小场景下,就是选择自己喜爱的手机品牌。完成后的效果如下:
工程项目用到的工具为谷歌亲儿子Android Studio
Let's coding:
从图中可以看出我们需要一个ListView。再就是给ListView自定义一个Adapter。
代码如下,首先是布局文件:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/id_myList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@android:color/transparent">
</ListView>
</RelativeLayout>
我们继续,有了ListView我们来初始化视图和相应的数据。初始化的数据很简单,就只有一个String类型的变量:
public class Brand {
private String bandname;
public Brand(String bandname){
this.bandname = bandname;
}
public String getBandname() {
return bandname;
}
public void setBandname(String bandname) {
this.bandname = bandname;
}
}
接下来就是在MainAcitivity中初始化视图和Adapter的数据:
public class MainActivity extends AppCompatActivity {
private ListView mListView; //首页的ListView
private List<Brand> namesList; //用于装载数据的集合
private int selectPosition = -1;//用于记录用户选择的变量
private Brand selectBrand; //用户选择的品牌
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initDatas();
}
private void initView(){
mListView = (ListView)findViewById(R.id.id_myList);
}
private void initDatas(){
//初始化ListView适配器的数据
namesList = new ArrayList<>();
Brand brand0 = new Brand("apple");
Brand brand1 = new Brand("sony");
Brand brand2 = new Brand("xiaomi");
Brand brand3 = new Brand("oppo");
Brand brand4 = new Brand("meizu");
Brand brand5 = new Brand("smartisan");
Brand brand6 = new Brand("vivo");
Brand brand7 = new Brand("samsung");
Brand brand8 = new Brand("letv");
Brand brand9 = new Brand("nubia");
Brand brand10 = new Brand("lg");
Brand brand11 = new Brand("qiku");
Brand brand12 = new Brand("huawei");
namesList.add(brand0);
namesList.add(brand1);
namesList.add(brand2);
namesList.add(brand3);
namesList.add(brand4);
namesList.add(brand5);
namesList.add(brand6);
namesList.add(brand7);
namesList.add(brand8);
namesList.add(brand9);
namesList.add(brand10);
namesList.add(brand11);
final MyAdapter myAdapter = new MyAdapter(this,namesList);
mListView.setAdapter(myAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//获取选中的参数
selectPosition = position;
myAdapter.notifyDataSetChanged();
selectBrand = namesList.get(position);
Toast.makeText(MainActivity.this,"您选中的手机品牌是:"+selectBrand.getBandname(),Toast.LENGTH_SHORT).show();
}
});
}
public class MyAdapter extends BaseAdapter{
Context context;
List<Brand> brandsList;
LayoutInflater mInflater;
public MyAdapter(Context context,List<Brand> mList){
this.context = context;
this.brandsList = mList;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return brandsList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
convertView = mInflater.inflate(R.layout.adapter_item,parent,false);
viewHolder = new ViewHolder();
viewHolder.name = (TextView)convertView.findViewById(R.id.id_name);
viewHolder.select = (RadioButton)convertView.findViewById(R.id.id_select);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
}
viewHolder.name.setText(brandsList.get(position).getBandname());
if(selectPosition == position){
viewHolder.select.setChecked(true);
}
else{
viewHolder.select.setChecked(false);
}
return convertView;
}
}
public class ViewHolder{
TextView name;
RadioButton select;
}
}
最后就是Adapter的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp">
<RadioButton
android:id="@+id/id_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:button="@null"
android:layout_marginLeft="10dp"
android:background="@drawable/radio"
android:checked="false"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="@+id/id_name"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_gravity="center"
android:text="姓名"/>
</LinearLayout>
此处需要说明几点:
1,未来防止ListView item点击后RadioButton不选中的问题,要将clickable属性设置为false,还有设置ListView的android:choiceMode="singleChoice"
按照名称也容易理解就是单选的模式;
2,这里要注意的是convertView = mInflater.inflate(R.layout.adapter_item,parent,false);
与convertView = mInflater.inflate(R.layout.adapter_item,null);
的区别,要设置ListView的行高就要按照前者来初始化convertView
。
3,RadioButton的样式和图片资源请参考源代码。
网友评论
http://blog.csdn.net/ddxxll2008/article/details/49125759