Android中使用ListView和CheckBox實現購物車多選功能
阿新 • • 發佈:2019-02-20
1效果圖
2MainActivity對應的佈局activity_main
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation ="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#0066FF"
>
<CheckBox
android:id="@+id/cbQuanxuan"
android:layout_width ="wrap_content"
android:layout_height="50dp"
android:text="全選"
/>
<Button
android:id="@+id/btnQueding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight ="true"
android:text="確定"
/>
</RelativeLayout>
<ListView
android:id="@+id/lvTest"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
3MainActivity中
package com.zhh.mycheckbox;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import com.orhanobut.logger.Logger;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private ListView lvTest;
private CheckBox cbQuanxuan;
LvAdapter lvAdapter;
List<Domain> list;
private Button btnQueding;
private boolean ismChecked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
myClick();
}
private void initView() {
btnQueding = findViewById(R.id.btnQueding);
cbQuanxuan = findViewById(R.id.cbQuanxuan);
lvTest = findViewById(R.id.lvTest);
list = getData();
lvAdapter = new LvAdapter(this, list);
lvTest.setAdapter(lvAdapter);
}
private void myClick() {
// 外面的全選
cbQuanxuan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Logger.t("111").d("setOnCheckedChangeListener>>>" + isChecked);
ismChecked = isChecked;
// 全選是選中狀態
if (isChecked) {
for (int i = 0; i < list.size(); i++) {
list.get(i).isXuanzhong = true;
lvAdapter.notifyDataSetChanged();
}
}
}
});
cbQuanxuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Logger.t("111").d("setOnClickListener");
// 全選不是選中狀態
if (!ismChecked) {
for (int i = 0; i < list.size(); i++) {
list.get(i).isXuanzhong = false;
lvAdapter.notifyDataSetChanged();
}
}
}
});
lvAdapter.setCheck(new LvAdapter.Check() {
@Override
public void mycheck(int position, boolean isChecked) {
Logger.t("111").d("isChecked>>>" + isChecked);
list.get(position).isXuanzhong = isChecked;
for (int j = 0; j < list.size(); j++) {
if (list.get(j).isXuanzhong) {
} else {
// 有沒選中的
cbQuanxuan.setChecked(false);
}
}
}
});
btnQueding.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isXuanzhong) {
String str = list.get(i).content;
sb.append(">>>>>" + str);
}
}
Logger.t("111").d("***" + sb.toString());
}
});
}
private List<Domain> getData() {
List<Domain> list = new ArrayList<Domain>();
for (int i = 0; i < 10; i++) {
Domain domain = new Domain();
int j = i % 2;
if (j == 0) {
domain.isXuanzhong = false;
} else {
domain.isXuanzhong = true;
}
domain.content = "我的內容" + i;
list.add(domain);
}
return list;
}
}
4LvAdapter介面卡的佈局item_layout
<?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="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
5LvAdapter介面卡中
package com.zhh.mycheckbox;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
/**
* Created by 16838 on 2018/5/22.
*/
public class LvAdapter extends BaseAdapter{
private Context context;
private List<Domain>list;
private Check check;
public LvAdapter(Context context,List<Domain>list){
this.context=context;
this.list=list;
}
public void setCheck(Check check){
this.check=check;
}
public interface Check{
public void mycheck(int position,boolean isChecked);
}
@Override
public int getCount() {
return list.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHoder viewHoder;
if(convertView==null) {
viewHoder=new ViewHoder();
convertView= LayoutInflater.from(context).inflate(R.layout.item_layout,null);
viewHoder.checkBox=(CheckBox)convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHoder);
}else{
viewHoder= (ViewHoder) convertView.getTag();
}
viewHoder.checkBox.setText(list.get(position).content);
viewHoder.checkBox.setChecked(list.get(position).isXuanzhong);
viewHoder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(check!=null) {
check.mycheck(position,isChecked);
}
}
});
return convertView;
}
class ViewHoder{
CheckBox checkBox;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}