[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>
[代码运行效果截图]
by: 发表于:2017-09-27 09:59:46 顶(0) | 踩(0) 回复
??
回复评论