用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

ListView的全选,反选,隐藏和显示展示

2017-03-13 作者: 云代码会员举报

[android]代码库

public class MainActivity extends AppCompatActivity {

    private ArrayList<ListItem> listItems;
    private Button button1;
    private Button button2;
    private Button button3;
    boolean flag = false;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.lv);
        listItems = new ArrayList<ListItem>();
        for (int i = 0; i < 20; i++) {
            listItems.add(new ListItem(false,false,"item"+i+""));
        }
        final MyAdapter myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < listItems.size(); i++) {
                    listItems.get(i).setCheck(true);
                }
                myAdapter.notifyDataSetChanged();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < listItems.size(); i++) {
                    listItems.get(i).setCheck(false);
                }
                myAdapter.notifyDataSetChanged();
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flag = !flag;
                if(flag) {
                    button2.setText("取消隐藏");
                }else {
                    button2.setText("隐藏选中");
                }
                myAdapter.notifyDataSetChanged();
            }
        });
    }

    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return listItems.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
           if(convertView == null) {
                convertView = View.inflate(MainActivity.this,R.layout.list_item,null);
            }
            final TextView textView_list_item = (TextView) convertView.findViewById(R.id.textView_list_item);
            final CheckBox checkBox_list_item = (CheckBox) convertView.findViewById(R.id.checkBox_list_item);
            textView_list_item.setText(listItems.get(position).getText());

                checkBox_list_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        listItems.get(position).setCheck(isChecked);
                        listItems.get(position).setHide(isChecked);
                        if(flag) {
                            notifyDataSetChanged();
                        }
                    }
                });


            checkBox_list_item.setChecked(listItems.get(position).isCheck());

            if(listItems.get(position).isHide() && flag) {
                checkBox_list_item.setVisibility(View.INVISIBLE);
                textView_list_item.setVisibility(View.INVISIBLE);
            }else {
                checkBox_list_item.setVisibility(View.VISIBLE);
                textView_list_item.setVisibility(View.VISIBLE);
            }
            return convertView;
        }
    }
}
package baway.com.chcekboxdemo;

/**
 * Created by hasee bean.
 */

public class ListItem {
    private boolean isHide;
    private boolean isCheck;
    private String text;

    public void setHide(boolean hide) {
        isHide = hide;
    }

    public void setCheck(boolean check) {
        isCheck = check;
    }

    public boolean isHide() {

        return isHide;
    }

    public boolean isCheck() {
        return isCheck;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public ListItem(boolean isHide, boolean isCheck, String text) {

        this.isHide = isHide;
        this.isCheck = isCheck;
        this.text = text;
    }
}
。。。。。
<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >


    <Button
        android:text="全选"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"

        />
    <Button
        android:text="点击隐藏"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_toRightOf="@id/button1"/>
    <Button
        android:text="反选"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:layout_toRightOf="@id/button2" />
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lv"
        android:layout_below="@id/button2"></ListView>
</RelativeLayout>

 08:03:32
<?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">

    <CheckBox
        android:text="CheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox_list_item"
        android:layout_weight="1" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView_list_item"
        android:layout_weight="1" />
</LinearLayout>





[代码运行效果截图]


ListView的全选,反选,隐藏和显示展示


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...