Android小功能(1):在ListView中实现单选功能

凭借之前java的基础,在公司有机会一边学习一边进行安卓的开发,这个系列希望能够鞭策自己能够把项目中的一些实用小功能进行总结学习,也加深对Android开发的理解。
进入正题,这个单选小功能我放到了一个小场景下,就是选择自己喜爱的手机品牌。完成后的效果如下:

Android小功能(1):在ListView中实现单选功能_第1张图片
单选.jpg

工程项目用到的工具为谷歌亲儿子Android Studio
Let's coding:
从图中可以看出我们需要一个ListView。再就是给ListView自定义一个Adapter。
代码如下,首先是布局文件:
activity_main.xml




    
    


我们继续,有了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 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 brandsList;
        LayoutInflater mInflater;
        public MyAdapter(Context context,List 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的布局



    
    

此处需要说明几点:
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的样式和图片资源请参考源代码。

https://github.com/gangdou91up/MultipleApplication.git

你可能感兴趣的:(Android小功能(1):在ListView中实现单选功能)