條目的全選-反選_Demo

主Activity代碼

package com.example.allandreverse;

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



import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ListView list_view;
    private CheckAdapter checkAdapter;
    private CheckBox allBox;
    private Button reverse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取控件
        list_view = (ListView)findViewById(R.id.list_view);
        allBox = (CheckBox)findViewById(R.id.all);
        reverse = (Button)findViewById(R.id.reverse);
        //條目集合
        List<String> list = new ArrayList<String>();

        //添加(測試)數據
        for (int i = 0; i < 15; i++) {
            list.add("這是條目" + i);
        }
        //創建適配器、設置適配器
        checkAdapter = new CheckAdapter(getApplicationContext(), list);
        list_view.setAdapter(checkAdapter);

        //設置全選
        allBox.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                boolean flag = allBox.isChecked();

                for (int i = 0; i < checkAdapter.getSelect().size(); i++) {
                    checkAdapter.getSelect().set(i, flag);
                }
                checkAdapter.notifyDataSetChanged();

            }
        });

        //反選
        reverse.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (! checkAdapter.getSelect().contains(true)) {
                    Toast.makeText(MainActivity.this, "qingxuanz", 0).show();
                }else {
                    for (int i = 0; i < checkAdapter.getSelect().size(); i++) {
                        if (checkAdapter.getSelect().get(i)) {
                            checkAdapter.getSelect().set(i, false);
                        }else {
                            checkAdapter.getSelect().set(i, true);
                        }
                    }

                    if (checkAdapter.getSelect().contains(false)) {
                        allBox.setChecked(false);
                    }

                    checkAdapter.notifyDataSetChanged();
                }
            }
        });

    }
//創建的適配器
    private class CheckAdapter extends BaseAdapter {
        private List<String> list;
        private Context context;

        //創建一個集合 去記錄選中與未選中的狀態 
        LinkedList<Boolean> linkedList = new LinkedList<Boolean>();

        public CheckAdapter(Context context, List<String> list) {
            // TODO Auto-generated constructor stub
            this.list = list;
            this.context = context;

            for (int i = 0; i < list.size(); i++) {
                linkedList.add(false);
            }

        }
        //對外提供一個方法 獲取這個集合
        private List<Boolean> getSelect() {
            return linkedList;
        }


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

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

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

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.item, null);
            }
            CheckBox ck = (CheckBox) convertView.findViewById(R.id.ck);
            TextView tView = (TextView) convertView.findViewById(R.id.tv);

            tView.setText(list.get(position));

            ck.setChecked(linkedList.get(position));
            //對checkBox點擊進行監聽
            ck.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    //點中的時候改成相反的值
                    linkedList.set(position, ! linkedList.get(position));

                    //點擊的同時還要去判斷全選
                    if (linkedList.contains(false)) {
                        allBox.setChecked(false);
                    }else {
                        allBox.setChecked(true);
                    }

                    notifyDataSetChanged();
                }
            });

            return convertView;
        }

    }

}

主類XML佈局:

<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" >

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linear" >
    </ListView>

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全選" />

        <Button
            android:text="反選"
            android:id="@+id/reverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

適配器的item佈局:

<?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="horizontal" >

    <CheckBox 
        android:id="@+id/ck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView 
        android:id="@+id/tv"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章