MainActivity中:android
public class MainActivity extends AppCompatActivity { MyListView listView; static Adapter adapter; CheckBox cb_cart_all; static Float zongjia; static TextView tv_cart_Allprice_1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_cart_Allprice_1=(TextView)findViewById(R.id.tv_cart_Allprice_1); listView=(MyListView)findViewById(R.id.listview); cb_cart_all=(CheckBox)findViewById(R.id.cb_cart_all); //點擊全選按鈕 cb_cart_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked){ //全選 adapter.setListDataFocuseAble(); jisuanZongJia(); }else { //取消選擇 // adapter.setListDataUnFocuseAble(); jisuanZongJia(); } adapter.notifyDataSetChanged(); } }); adapter=new Adapter(this); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); init(); } //加載數據 public void init(){ List<Shoucang> listdata=new ArrayList<Shoucang>(); for(int i=0;i<5;i++){ Shoucang shoucang=new Shoucang(); shoucang.setDrugName("第"+i+"條"); shoucang.setDrugmoney(""+i*10); //有一個id用於刪除和向後臺傳遞數據 shoucang.setShoucangId("" +(199+i)); shoucang.setDrugFlag(3); listdata.add(shoucang); } adapter.setListData(listdata); } //計算選中的總價 static public void jisuanZongJia(){ final List<Map<String, String>> selectData = getSelectIDList(); float s = 0; for (int i = selectData.size() - 1; i >= 0; i--) { s+= Float.valueOf(selectData.get(i).get("zongjia")); } zongjia = s; shoppingCartSetTotalPrice(zongjia); } //設置總價 public static void shoppingCartSetTotalPrice(float price){ tv_cart_Allprice_1.setText("" + price); } //CB點擊 public static void shoppingCartRefush(List<Shoucang> listDataRec){ jisuanZongJia(); } static public List<Map<String,String>> getSelectIDList(){ return adapter.getselectData(); } }
若是須要點擊條目就選中chebox的化能夠添加app
mAddLearnListview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { indexArea = i; CheckBox checkBox = view.findViewById(R.id.add_learn_list_chebox); if (checkBox.isChecked()){ checkBox.setChecked(false); }else { checkBox.setChecked(true); } } });
mainactivity的XML佈局:ide
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RelativeLayout android:id="@+id/l1" android:layout_width="match_parent" android:layout_height="40dp" android:background="#00c168"> <ImageView android:id="@+id/backImage" android:onClick="exit" android:layout_width="wrap_content" android:layout_height="23dp" android:src="@mipmap/ic_launcher" android:layout_centerVertical="true" tools:ignore="OnClick" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="列表" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:paddingRight="15dp" android:layout_gravity="center_vertical" android:textColor="#FFFFFF" android:textSize="20dp" /> </RelativeLayout> <ScrollView android:layout_above="@+id/view" android:layout_below="@id/l1" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/main_list_shoucang" android:layout_width="match_parent" android:layout_height="wrap_content"> <com.guorentong.learn.listviewchebox.MyListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/listview"/> </RelativeLayout> </ScrollView> <View android:id="@+id/view" android:layout_width="match_parent" android:layout_height="1dp" android:layout_above="@+id/bottom"/> <RelativeLayout android:id="@+id/bottom" android:background="#ffffff" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:orientation="horizontal" > <CheckBox android:layout_centerVertical="true" android:scaleX="0.75" android:scaleY="0.75" android:id="@+id/cb_cart_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:text="全選" android:textSize="17dp" android:textColor="#191919" /> <RelativeLayout android:layout_marginRight="15dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:gravity="center_vertical" android:layout_height="wrap_content"> <TextView android:id="@+id/text2" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="總價:"/> <TextView android:id="@+id/tv_cart_Allprice_1" android:layout_centerVertical="true" android:layout_toRightOf="@+id/text2" android:gravity="center_vertical" android:layout_width="200dp" android:layout_height="40dp" android:textSize="20dp"/> </RelativeLayout> </RelativeLayout> </RelativeLayout>
Adapter中:佈局
public class Adapter extends BaseAdapter { List<Map<String,String>> sd; Map<String,String> selectDataItem; private Context context; private List<Shoucang> listData=new ArrayList<Shoucang>(); public Adapter(Context context) { super(); this.context = context; } public List<Shoucang> getListData() { return listData; } public void setListData(List<Shoucang> listData) { this.listData = listData; } //2顯示checkBox並選中,3顯示但未選中 //取消全選 public void setListDataUnFocuseAble(){ for(int idx = 0;idx < this.listData.size();idx++){ this.listData.get(idx).setDrugFlag(3); } } //點擊全選 public void setListDataFocuseAble(){ for(int idx = 0;idx < this.listData.size();idx++){ this.listData.get(idx).setDrugFlag(2); } } @Override public int getCount() { return listData.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 view, ViewGroup parent) { DrugListHolder holder=null; if(view==null){ holder=new DrugListHolder(); view= LayoutInflater.from(context).inflate(R.layout.shoucang_druglist_item, null); holder.itemName =(TextView) view.findViewById(R.id.drugName); holder.itemMoney =(TextView) view.findViewById(R.id.drugmoney); holder.cb_choice =(CheckBox) view.findViewById(R.id.cb_choice_person_shoucang); view.setTag(holder); }else{ holder=(DrugListHolder) view.getTag(); } holder.cb_choice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ listData.get(position).setDrugFlag(2); MainActivity.shoppingCartRefush(listData); Toast.makeText(context,"您選中了第"+position+"項",Toast.LENGTH_LONG).show(); }else{ listData.get(position).setDrugFlag(3); MainActivity.shoppingCartRefush(listData); Toast.makeText(context, "您取消中了第" + position + "項", Toast.LENGTH_LONG).show(); } } }); //請求列表展現圖 Shoucang Shoucang=listData.get(position); //全選和取消選擇 if(Shoucang.getDrugFlag()==2){ holder.cb_choice.setChecked(true); }else if(Shoucang.getDrugFlag()==3){ holder.cb_choice.setChecked(false); } //ImageCacheUtil.getInstance().displayImage(context,holder.itemimage1,listData.get(position).getImgIv1()); holder.itemName.setText("" + Shoucang.getDrugName()); holder.itemMoney.setText("" + Shoucang.getDrugmoney()); final String shoucangId= listData.get(position).getShoucangId(); return view; } //列表數據 public final class DrugListHolder { //列表名 TextView itemName; TextView itemMoney; CheckBox cb_choice; } public List<Map<String,String>> getselectData(){ sd = new ArrayList<Map<String,String>>(); if(this.listData.size() > 0){ for(int idx = 0;idx < this.listData.size();idx++){ if(this.listData.get(idx).getDrugFlag() == 2){ selectDataItem = new HashMap<String,String>(); selectDataItem.put("id",this.listData.get(idx).getShoucangId()); selectDataItem.put("zongjia",String.valueOf(listData.get(idx).getDrugmoney())); sd.add(selectDataItem); } } } return sd; } }
shoucang_druglist_item佈局:this
也能夠不用ScrollView滑動3d
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:background="#ffffff" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp" android:layout_weight="1" android:id="@+id/caidan_line"> <CheckBox android:layout_marginLeft="4dp" android:id="@+id/cb_choice_person_shoucang" android:scaleX="0.7" android:scaleY="0.7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:focusable="false" android:focusableInTouchMode="false" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="130dp" android:layout_marginLeft="6dp" android:layout_marginRight="5dp" android:id="@+id/caidan_relativeLayout"> <RelativeLayout android:gravity="center" android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true"> <ImageView android:src="@mipmap/ic_launcher" android:id="@+id/menu_image1_item" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="15dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/> </RelativeLayout> <LinearLayout android:layout_marginLeft="15dp" android:layout_toRightOf="@+id/image" android:layout_centerVertical="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:textSize="17dp" android:text="dnj" android:textColor="#191919" android:id="@+id/drugName" android:layout_below="@id/menu_image1_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"/> <TextView android:text="sjdkkfhsk" android:textSize="16dp" android:textColor="#757575" android:id="@+id/drugmoney" android:layout_below="@id/drugName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true"/> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout> </ScrollView>
這裏用用的MyListView 解決listview和scrollView的衝突問題。xml
MyListView:ip
public class MyListView extends ListView{ public MyListView(Context context) { // TODO Auto-generated method stub super(context); } public MyListView(Context context, AttributeSet attrs) { // TODO Auto-generated method stub super(context, attrs); } public MyListView(Context context, AttributeSet attrs, int defStyle) { // TODO Auto-generated method stub super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
實體類Shoucang:utf-8
其中private int drugFlag;參數get
是爲了判斷是否選中的參數
public class Shoucang { private String drugName; private String Drugmoney; private int drugFlag; private String shoucangId; public Shoucang() { } public String getDrugName() { return drugName; } public void setDrugName(String drugName) { this.drugName = drugName; } public String getDrugmoney() { return Drugmoney; } public void setDrugmoney(String drugmoney) { Drugmoney = drugmoney; } public int getDrugFlag() { return drugFlag; } public void setDrugFlag(int drugFlag) { this.drugFlag = drugFlag; } public String getShoucangId() { return shoucangId; } public void setShoucangId(String shoucangId) { this.shoucangId = shoucangId; } }