适配器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:layout_margin="10dp" android:orientation="horizontal"> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name" /> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="age" /> <TextView android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sex" /> </LinearLayout> </LinearLayout>
——————————————————————————————————————————————————————————————————————
mainactivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/background" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listview" ></ListView> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/wcheck" android:layout_alignParentBottom="true" /> </RelativeLayout>
____________________________________________________________________________________________________
mainactivity
package com.sf.quanxuan; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.CheckBox; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView listView; ArrayList<bean> list=new ArrayList<>(); Adapter adapter; CheckBox check; Boolean checkflag=false; int flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)findViewById(R.id.listview); check=findViewById(R.id.wcheck); list.add(new bean("张三","18","男")); list.add(new bean("张三","18","男")); list.add(new bean("张三","18","男")); list.add(new bean("张三","18","男")); list.add(new bean("张三","18","男")); list.add(new bean("张三","18","男")); if(checkflag){ for (int i = 0; i <list.size() ; i++) { list.get(i).setIson(true); } }else{ for (int i = 0; i <list.size() ; i++) { list.get(i).setIson(false); } } setAdapter(); check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (check.isChecked()) { checkflag=true; for (int i = 0; i < list.size(); i++) { list.get(i).setIson(true); } } else { checkflag=false; for (int i = 0; i < list.size(); i++) { list.get(i).setIson(false); } } adapter.notifyDataSetChanged(); } }); adapter.getitemPosition(new Adapter.getItemPosition() { @Override public void getitemPosition(int position) { Boolean ison = list.get(position).getIson(); ison=!ison; list.get(position).setIson(ison); flag = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i).getIson()) { flag++; } } } }); } public void setcheckeb(boolean ischecked){ check.setChecked(ischecked); adapter.notifyDataSetChanged(); } public void setAdapter(){ if(adapter==null) { Log.d("哈哈1","我走了"); adapter = new Adapter(MainActivity.this,list,MainActivity.this); listView.setAdapter(adapter); }else{ Log.d("哈哈2","我走了"); adapter.notifyDataSetChanged(); } } // 双击退出 long backTime; @Override public void onBackPressed() { if (System.currentTimeMillis() - backTime > 2000){ Toast.makeText(MainActivity.this,"再按一次退出",Toast.LENGTH_SHORT).show(); backTime = System.currentTimeMillis(); return; } super.onBackPressed(); } }
____________________________________________________________________________________________________
适配器
package com.sf.quanxuan; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class Adapter extends BaseAdapter { Context context; ArrayList<bean> list; MainActivity mainActivity; getItemPosition getItemPosition; public interface getItemPosition{ void getitemPosition(int position); } public void getitemPosition(getItemPosition position){ this.getItemPosition = position; } public Adapter(Context context, ArrayList<bean> list, MainActivity mainActivity) { this.context = context; this.list = list; this.mainActivity = mainActivity; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView==null){ convertView = View.inflate(context, R.layout.bj, null); viewHolder = new ViewHolder(); viewHolder.name=(TextView) convertView.findViewById(R.id.name); viewHolder.check=(CheckBox) convertView.findViewById(R.id.check); viewHolder.age=(TextView) convertView.findViewById(R.id.age); viewHolder.sex=(TextView) convertView.findViewById(R.id.sex); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.name.setText(list.get(position).getNamne()); viewHolder.age.setText(list.get(position).getAge()); viewHolder.sex.setText(list.get(position).getSex()); viewHolder.check.setChecked(list.get(position).getIson()); viewHolder.check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Boolean isno = list.get(position).getIson(); isno=!isno; list.get(position).setIson(isno); //定义标识位 boolean flog=true; for (int i = 0; i <list.size() ; i++) { //如果子控件有一个的值为false时 父控件的值为false 否则不走if判断 父控件为true if (!list.get(i).getIson()){ flog=false; } } mainActivity.setcheckeb(flog); } }); return convertView; } class ViewHolder{ TextView name,age,sex; CheckBox check; } }