关闭

全选、反选

73人阅读 评论(0) 收藏 举报

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;
    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4706次
    • 积分:359
    • 等级:
    • 排名:千里之外
    • 原创:32篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类