全选、反选

XML布局

activity_main.xml布局

<LinearLayout 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"
    android:orientation="vertical" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button"
        android:text="选中" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</LinearLayout>

items.xml布局

<?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="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp" />

</LinearLayout>

MainActivity

package com.example.day02_select;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.example.day02_select.adapter.MyAdapter;
import com.example.day02_select.bean.Person;

public class MainActivity extends Activity implements OnItemClickListener {

    List<Person> al = new ArrayList<Person>();
    private MyAdapter adapter;

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

        //寻找控件
        ListView listView = (ListView) findViewById(R.id.listView);
        //设置适配器
        adapter = new MyAdapter(this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);


        for (int i = 0; i < 10; i++) {
            Person p = new Person();
            p.age = i;
            p.name = "张三" + i;
            p.statue = Person.NOT_SELECT;
            al.add(p);
        }
        adapter.addrest(al);
    }

    //listView点击条目监听
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        int statue = al.get(arg2).statue;
        if (statue == 0) {
            al.get(arg2).statue = Person.SELECT;
        } else {
            al.get(arg2).statue = Person.NOT_SELECT;
        }
        adapter.addrest(al);
    }

    //选中按钮监听
    public void button(View v) {
        for (Person p : al) {
            if (p.statue == Person.SELECT) {
                p.statue = Person.NOT_SELECT;
            } else {
                p.statue = Person.SELECT;
            }
        }
        adapter.addrest(al);
    }

}

MyAdapter适配器中

package com.example.day02_select.adapter;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.day02_select.MainActivity;
import com.example.day02_select.R;
import com.example.day02_select.bean.Person;

public class MyAdapter extends BaseAdapter {

    MainActivity context;

    List<Person> al = new ArrayList<Person>();

    public MyAdapter(MainActivity context) {
        super();
        this.context = context;
    }

    public void addrest(List<Person> al) {
        this.al.clear();
        this.al.addAll(al);
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return al.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //设置布局
        View view = View.inflate(context, R.layout.items, null);
        //寻找ImageView控件
        ImageView image = (ImageView) view.findViewById(R.id.image);
        //对集合里面的状态statue进行判断
        switch (al.get(position).statue) {
        case Person.SELECT:
            image.setImageResource(R.drawable.hookicon_repost_pressed);
            break;
        case Person.NOT_SELECT:
            image.setImageResource(R.drawable.ic_checkbox_normal);
            break;
        /*case Person.DEFAULT:
            image.setImageResource(R.drawable.hookicon_repost_pressed);
            break;*/

        default:
            break;
        }
        return view;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值