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

目录

一:效果图

二:定义Listview和Adapter

三:model

四:数据初始化

五:最后就是Adapter的布局

六:备注


一:效果图

二:定义Listview和Adapter

首先我们需要一个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>

三:model

我们继续,有了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

看了这篇文章觉得挺适用自己项目的,觉得很好记录下次的,本文原链接:https://www.jianshu.com/p/6af0a7a59756
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值