(安卓) 購物車二級列表,計算和全反選 以及 EventBus(訊息傳遞)
阿新 • • 發佈:2019-02-01
先看效果圖:
MainActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;
import bwie.com.day_shoping_21.adpader.MyAdapter;
import bwie.com.day_shoping_21.bean.GoodsBean;
import bwie.com.day_shoping_21.prasenter.Presaenter;
import bwie.com.day_shoping_21.view.IView;
public class MainActivity extends AppCompatActivity implements IView{
private ExpandableListView elv;
private List<List<GoodsBean.DataBean.DatasBean>> data=new ArrayList<List<GoodsBean.DataBean.DatasBean>>();
private CheckBox mQuanxuan;
private TextView zongjia;
private TextView heji;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
elv = (ExpandableListView) findViewById(R.id.elv);
mQuanxuan = (CheckBox) findViewById(R.id.quanxuan);
zongjia = (TextView) findViewById(R.id.zongji);
heji = (TextView) findViewById(R.id.heji);
Presaenter presaenter = new Presaenter();
presaenter.attachView(this);
presaenter.getNews();
//註冊EventBus
EventBus.getDefault().register(this);
mQuanxuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判斷全選的狀態
myAdapter.checkALLStatus(mQuanxuan.isChecked());
}
});
}
@Override
public void success(List<GoodsBean.DataBean> news) {
//遍歷一級列表
for (int i = 0; i <news.size() ; i++) {
GoodsBean.DataBean dataBean = news.get(i);
//得到二級的物件
List<GoodsBean.DataBean.DatasBean> datas = dataBean.getDatas();
//將其新增到二級列表中
data.add(datas);
}
//介面卡
myAdapter = new MyAdapter(this, news, data);
//設定介面卡
elv.setAdapter(myAdapter);
//將一級列表的展開標誌隱藏
elv.setGroupIndicator(null);
//迴圈集合
for (int i = 0; i <news.size() ; i++) {
//將二級列表展開
elv.expandGroup(i);
}
}
@Override
public void failed(Exception e) {
}
//接收介面卡傳過來的值
@Subscribe
public void onMessageEvent(MessageEvent event) {
//接收介面卡(Myadpader)傳遞過來的值 ,將全選的狀態設定為true或者false
mQuanxuan.setChecked(event.isCheck());
}
//接收介面卡傳過來的數量和總價
@Subscribe
public void onMessageEvent(CountPrice event) {
zongjia.setText("結算("+event.getCount()+")"+"");
heji.setText("總計:"+event.getPrice()+"");
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
介面卡所用到的類:
public class CountPrice {
private int count;
private int price;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
public class MessageEvent {
private boolean check;
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
}
介面卡:(主要判斷都在這裡)
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import java.util.List;
import bwie.com.day_shoping_21.CountPrice;
import bwie.com.day_shoping_21.MessageEvent;
import bwie.com.day_shoping_21.R;
import bwie.com.day_shoping_21.bean.GoodsBean;
/**
* Created by 迷人的腳毛!! on 2017/11/21.
*/
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
private List<GoodsBean.DataBean> list;
private List<List<GoodsBean.DataBean.DatasBean>> data;
public MyAdapter(Context context, List<GoodsBean.DataBean> list, List<List<GoodsBean.DataBean.DatasBean>> data) {
this.list = list;
this.context = context;
this.data = data;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return data.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return data.get(groupPosition).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final ViewHodler1 viewHodler1;
if(view==null){
view=View.inflate(context, R.layout.a_view,null);
viewHodler1=new ViewHodler1();
viewHodler1.name=view.findViewById(R.id.a_name);
viewHodler1.abox=view.findViewById(R.id.a_box);
view.setTag(viewHodler1);
}else{
viewHodler1 = (ViewHodler1) view.getTag();
}
final GoodsBean.DataBean dataBean = list.get(i);
final String title = dataBean.getTitle();
viewHodler1.abox.setChecked(dataBean.isCheck());
viewHodler1.name.setText(title);
//對一級的checkBox設定監聽
viewHodler1.abox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//點選改變狀態值
dataBean.setCheck(viewHodler1.abox.isChecked());
//通過一級的是否勾選,改變二級CheckBox狀態
dataCheboxZT(i,viewHodler1.abox.isChecked());
EventBus.getDefault().post(jiSuan());
//通過判斷是否全選,改變全選的狀態
QuanxuangZT(listCheckboxQX());
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
final ViewHodler2 viewHodler2;
if(view==null){
view=View.inflate(context,R.layout.b_view,null);
viewHodler2=new ViewHodler2();
viewHodler2.title=view.findViewById(R.id.b_title);
viewHodler2.price=view.findViewById(R.id.b_price);
viewHodler2.bbox=view.findViewById(R.id.b_box);
viewHodler2.jia = view.findViewById(R.id.b_jia);
viewHodler2.jian = view.findViewById(R.id.b_jian);
viewHodler2.num = view.findViewById(R.id.b_num);
viewHodler2.del = view.findViewById(R.id.b_del);
view.setTag(viewHodler2);
}else{
viewHodler2 = (ViewHodler2) view.getTag();
}
final GoodsBean.DataBean.DatasBean datasBean = data.get(i).get(i1);
viewHodler2.title.setText(datasBean.getType_name());
viewHodler2.price.setText(datasBean.getPrice()+"");
viewHodler2.bbox.setChecked(datasBean.isCheck());
// viewHodler2.num.setText(datasBean.getCou()+"");
//對二級裡面的checkBox設定監聽
viewHodler2.bbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//二級的checkBox的點選時改變datasBean的屬性
datasBean.setCheck(viewHodler2.bbox.isChecked());
datasBean.setCou(1);
//計算勾選狀態下的價格和數量
EventBus.getDefault().post(jiSuan());
//判斷二級的CheckBox是全選,改變一級的checkBox
listCheckboxZT(i,dataCheckboxZT(i));
//判斷一級的checkbox是否選中,改變全選的狀態值
QuanxuangZT(listCheckboxQX());
//重新整理介面卡
notifyDataSetChanged();
}
});
//減法的點選事件
viewHodler2.jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num =datasBean.getCou();
if(num==1){
return;
}
//給控制元件賦值
viewHodler2.num.setText(--num+"");
//給控制元件賦值以後在賦值給控制元件本身
datasBean.setCou(num);
//當點選選中Checkbox的時候
if(viewHodler2.bbox.isChecked()){
//進行計算賦值傳遞給主MainActivity進行顯示
EventBus.getDefault().post(jiSuan());
}
notifyDataSetChanged();
}
});
//加的點選事件
viewHodler2.jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(context, "0", Toast.LENGTH_SHORT).show();
int num = datasBean.getCou();
++num;
//給控制元件賦值
viewHodler2.num.setText(num+"");
//當給控制元件賦值以後在賦給控制元件本身
datasBean.setCou(num);
//當點選選中Checkbox的時候
if(viewHodler2.bbox.isChecked()){
//進行繼續
EventBus.getDefault().post(jiSuan());
}
notifyDataSetChanged();
}
});
//刪除的點選事件
viewHodler2.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<GoodsBean.DataBean.DatasBean> datasBeen = data.get(i);
GoodsBean.DataBean.DatasBean remove = datasBeen.remove(i1);
if(datasBeen.size()==0){
data.remove(i);
list.remove(i);
}
EventBus.getDefault().post(jiSuan());
notifyDataSetChanged();
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class ViewHodler1{
CheckBox abox;
TextView name;
}
class ViewHodler2{
CheckBox bbox;
TextView title,price,time,num;
ImageView jia,jian;
Button del;
}
/***
* 改變一級checkbox的狀態
*/
private void listCheckboxZT(int i,boolean flag){
GoodsBean.DataBean dataBean = list.get(i);
dataBean.setCheck(flag);
}
/**
* 改變全選的狀態
*/
private void QuanxuangZT(boolean flag){
MessageEvent messageEvent = new MessageEvent();
messageEvent.setCheck(flag);
EventBus.getDefault().post(messageEvent);
}
/**
* 改變二級checkbox的狀態
*/
private void dataCheboxZT(int i,boolean flag){
List<GoodsBean.DataBean.DatasBean> datasBeen = data.get(i);
for (int j = 0; j < datasBeen.size(); j++) {
datasBeen.get(j).setCheck(flag);
}
}
/**
* 判斷一級的checkbox是否全部選中
*/
private boolean listCheckboxQX(){
for (int i = 0; i <list.size() ; i++) {
GoodsBean.DataBean dataBean = list.get(i);
List<GoodsBean.DataBean.DatasBean> datas = dataBean.getDatas();
for (int j = 0; j < datas.size(); j++) {
GoodsBean.DataBean.DatasBean datasBean = datas.get(j);
if(!datasBean.isCheck()){
return false;
}
}
}
EventBus.getDefault().post(jiSuan());
return true;
}
/**
* 判斷二級的CheckBox是否全部選中
*/
private boolean dataCheckboxZT(int i){
List<GoodsBean.DataBean.DatasBean> datasBeen = data.get(i);
for (int j = 0; j < datasBeen.size(); j++) {
if(!datasBeen.get(j).isCheck()){
return false;
}
}
return true;
}
//====================================================================================================
/**
* 計算選擇中時的價格和數量
*/
private CountPrice jiSuan(){
int cout=0;
int price=0;
for (int i = 0; i <list.size() ; i++) {
GoodsBean.DataBean dataBean = list.get(i);
List<GoodsBean.DataBean.DatasBean> datas = dataBean.getDatas();
for (int j = 0; j < datas.size(); j++) {
GoodsBean.DataBean.DatasBean datasBean = datas.get(j);
if(datasBean.isCheck()){
cout+=datasBean.getCou();
price+=datasBean.getPrice()*datasBean.getCou();
}
}
}
CountPrice countPrice = new CountPrice();
countPrice.setCount(cout);
countPrice.setPrice(price);
return countPrice;
}
//===============================================
/**
* 全選的時候改變所有的checkbox的狀態
*/
public void checkALLStatus(boolean flag){
for (int i = 0; i <list.size() ; i++) {
GoodsBean.DataBean dataBean = list.get(i);
dataBean.setCheck(flag);
List<GoodsBean.DataBean.DatasBean> datas = dataBean.getDatas();
for (int j = 0; j < datas.size(); j++) {
GoodsBean.DataBean.DatasBean datasBean = datas.get(j);
datasBean.setCheck(flag);
}
}
EventBus.getDefault().post(jiSuan());
notifyDataSetChanged();
}
}
==================================================================================================
以上是主要類
接下來就是比較常寫的一些解析類:
HttpUtils:
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import java.io.IOException;
import java.util.Map;
import bwie.com.day_shoping_21.GsonUtlis;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by 迷人的腳毛!! on 2017/11/21.
*/
public class HttpUtils {
private static final String TAG = "HttpUtils";
private static Handler handler=new Handler();
private static volatile HttpUtils instance;
private HttpUtils(){
}
public static HttpUtils getInstance() {
if (null == instance) {
synchronized (HttpUtils.class) {
if (instance == null) {
instance = new HttpUtils();
}
}
}
return instance;
}
public void get(String url, Map<String,String> map, final CallBack callBack, final Class cls){
// http://www.baoidu.com/login?mobile=11111&password=11111&age=1&name=zw
// 1.http://www.baoidu.com/login --------? key=value&key=value
// 2.http://www.baoidu.com/login? --------- key=value&key=value
// 3.http://www.baoidu.com/login?mobile=11111 -----&key=value&key=value
if (TextUtils.isEmpty(url)){
return;
}
StringBuffer sb = new StringBuffer();
sb.append(url);
if (url.contains("?")){
//如果包含並且是最後一位,就是第二種型別
if (url.indexOf("?")== url.length()-1){
}else{
//如果包含? 並且不是最後一位
sb.append("&");
}
}else{
//不包含? 對應的是第一種型別
sb.append("?");
}
//遍歷map集合進行拼接,拼接的形式是key=values
for(Map.Entry<String,String> entry:map.entrySet()){
sb.append(entry.getKey())
.append("=")
.append(entry.getValue())
.append("&");
}
//如果存在&號,把最後一個&去掉
if (sb.indexOf("&")!=-1){
// lastIndexOf 最後一個
sb.deleteCharAt(sb.lastIndexOf("&"));
}
Log.i(TAG, "get url: " + sb);
OkHttpClient client = new OkHttpClient();
Request build = new Request.Builder()
.get()
.url(sb.toString())
.build();
Call call = client.newCall(build);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
//呼叫自己的回撥介面回傳回去
callBack.onFailed(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String reutl = response.body().string();
//請求成功之後解析,通過自己的回撥介面將資料傳輸回去
handler.post(new Runnable() {
@Override
public void run() {
Object o;
//如果是空的話
if (TextUtils.isEmpty(reutl)){
o=null;
Log.i(TAG,"為空!!!!!!!!!"+o);
}else{
o = GsonUtlis.getInstance().fromJson(reutl, cls);
Log.i(TAG,"不不不不不不為空!!!!!!!!!"+o);
}
callBack.onSuccess(o);
}
});
}
});
}
}
模型層介面:
public interface CallBack {
void onSuccess( Object o);
void onFailed( Exception e);
}
檢視層介面:
public interface IView {
void success( List<GoodsBean.DataBean> news);
void failed( Exception e);
}
P層public class Presaenter {
private IView iv;
public void attachView(IView iv){
this.iv=iv;
}
public void getNews(){
HashMap<String, String > map = new HashMap<>();
map.put("uri", "evaluation");
HttpUtils.getInstance().get("http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9", map, new CallBack() {
@Override
public void onSuccess(Object o) {
GoodsBean bean= (GoodsBean) o;
if(bean!=null){
List<GoodsBean.DataBean> data = bean.getData();
iv.success(data);
}
}
@Override
public void onFailed(Exception e) {
iv.failed(e);
}
},GoodsBean.class);
}
//定義一個方法讓iv等於空 防止記憶體洩漏問題
public void detachView() {
if (iv != null) {
iv = null;
}
}
}
============================================================================
接下來就是佈局了:
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_marginTop="0dp"
android:orientation="vertical"
tools:context="bwie.com.day_shoping_21.MainActivity">
<TextView
android:layout_gravity="center_horizontal"
android:textSize="30dp"
android:layout_marginTop="0dp"
android:gravity="center"
android:background="@color/colorPrimary"
android:textColor="#fff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="購物車"
android:id="@+id/textView" />
<ExpandableListView
android:id="@+id/elv"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/quanxuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="全選" />
<TextView
android:id="@+id/zongji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="合計:0" />
<TextView
android:id="@+id/heji"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="100dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text="結算(0)" />
</LinearLayout>
</LinearLayout>
一級佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp">
<CheckBox
android:id="@+id/a_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="20dp"
android:id="@+id/a_name"
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
二級佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
>
<CheckBox
android:layout_marginTop="20dp"
android:id="@+id/b_box"
android:layout_marginLeft="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/b_title"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
<TextView
android:layout_marginTop="10dp"
android:id="@+id/b_price"
android:textColor="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<ImageView
android:id="@+id/b_jian"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/shopcart_minus_grey" />
<TextView
android:id="@+id/b_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/shopcart_add_btn"
android:paddingBottom="2dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="2dp"
android:text="1" />
<ImageView
android:id="@+id/b_jia"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:src="@drawable/shopcart_add_red" />
<Button
android:id="@+id/b_del"
android:text="刪除"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
依賴:EventBus(訊息傳遞)
compile 'org.greenrobot:eventbus:3.1.1'
=============================================================================================
一下是加了自定義控制元件的
二級列表的xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
>
<CheckBox
android:layout_marginTop="20dp"
android:id="@+id/b_box"
android:layout_marginLeft="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/b_title"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
<TextView
android:layout_marginTop="10dp"
android:id="@+id/b_price"
android:textColor="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<bwie.com.day_shoping_1219.AddDelView
android:layout_width="126dp"
android:layout_height="match_parent"
android:id="@+id/ade"
></bwie.com.day_shoping_1219.AddDelView>
<Button
android:id="@+id/b_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="刪除" />
</LinearLayout>
</LinearLayout>
介面卡:
package bwie.com.day_shoping_1219.adpater;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import java.util.List;
import bwie.com.day_shoping_1219.AddDelView;
import bwie.com.day_shoping_1219.CountPrice;
import bwie.com.day_shoping_1219.MessageEvent;
import bwie.com.day_shoping_1219.R;
import bwie.com.day_shoping_1219.bean.UserBean;
import static bwie.com.day_shoping_1219.R.id.ade;
/**
* Created by 迷人的腳毛!! on 2017/12/19.
*/
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
private List<UserBean.DataBean> list;
private List<List<UserBean.DataBean.ListBean>> data;
public MyAdapter(Context context, List<UserBean.DataBean> list, List<List<UserBean.DataBean.ListBean>> data) {
this.context = context;
this.list = list;
this.data = data;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int i) {
return data.get(i).size();
}
@Override
public Object getGroup(int i) {
return list.get(i);
}
@Override
public Object getChild(int i, int i1) {
return data.get(i).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final ViewHodler1 viewHodler1;
if (view==null){
view = View.inflate(context, R.layout.yiji, null);
viewHodler1 = new ViewHodler1();
viewHodler1.name=view.findViewById(R.id.a_name);
viewHodler1.abox=view.findViewById(R.id.a_box);
viewHodler1.bianji=view.findViewById(R.id.bianji);
view.setTag(viewHodler1);
}else{
viewHodler1 = (ViewHodler1) view.getTag();
}
final UserBean.DataBean dataBean = list.get(i);
viewHodler1.name.setText(dataBean.getSellerName());
viewHodler1.abox.setChecked(dataBean.isCheck());
//對一級的checkbox設定監聽
viewHodler1.abox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//點選改變狀態值
dataBean.setCheck(viewHodler1.abox.isChecked());
//通過一級的手否勾選,改變二級的checkbox狀態
dataCheckboxZT(i,viewHodler1.abox.isChecked());
//通過判斷是否全選,改變全選的狀態
QuanxuangZT(listCheckboxQX());
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
final ViewHodler2 viewHodler2;
if(view==null){
view=View.inflate(context,R.layout.erji,null);
viewHodler2=new ViewHodler2();
viewHodler2.title=view.findViewById(R.id.b_title);
viewHodler2.price=view.findViewById(R.id.b_price);
viewHodler2.bbox=view.findViewById(R.id.b_box);
viewHodler2.del = view.findViewById(R.id.b_del);
viewHodler2.ade=view.findViewById(ade);
view.setTag(viewHodler2);
}else{
viewHodler2 = (ViewHodler2) view.getTag();
}
final UserBean.DataBean.ListBean datasBean = data.get(i).get(i1);
viewHodler2.title.setText(datasBean.getTitle());
viewHodler2.price.setText(datasBean.getPrice()+"");
viewHodler2.bbox.setChecked(datasBean.isCheck());
//對二級裡面的checkBox設定監聽
viewHodler2.bbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//二級的checkbox的點選時改變datatbean的屬性
datasBean.setCheck(viewHodler2.bbox.isChecked());
//計算勾選狀態下的價格和數量
EventBus.getDefault().post(jiSuan());
//判斷二級的checkbox是全選,改變一級的checkbox
listCheckboxZT(i,dataCheckboxZT(i));
//判斷一級的checkbox是否選中,改變全選的狀態值
QuanxuangZT(listCheckboxQX());
//重新整理介面卡
notifyDataSetChanged();
}
});
//自定義控制元件的加減以及EditText 。這三個的回撥介面
viewHodler2.ade.setOnAddDelClickLinstener(new AddDelView.OnAddDelClickLinstener() {
@Override
public void onAddClick(View v) {
int num = datasBean.getCou();
++num;
//給控制元件賦值
viewHodler2.ade.setNumber(num);
//當給控制元件賦值以後在賦給控制元件本身
datasBean.setCou(num);
//當點選選中Checkbox的時候
if(viewHodler2.bbox.isChecked()){
//進行繼續
EventBus.getDefault().post(jiSuan());
}
notifyDataSetChanged();
}
@Override
public void onDelClick(View v) {
int num = datasBean.getCou();
if (num==1){
return;
}
//給控制元件賦值
viewHodler2.ade.setNumber(--num);
//給控制元件賦值以後賦值給控制元件本身
datasBean.setCou(num);
if(viewHodler2.bbox.isChecked()){
//進行計算賦值傳遞給主MainActivity進行顯示
EventBus.getDefault().post(jiSuan());
}
notifyDataSetChanged();
}
});
//刪除的點選事件
viewHodler2.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<UserBean.DataBean.ListBean> listBeen = data.get(i);
UserBean.DataBean.ListBean remove = listBeen.remove(i1);
if (listBeen.size()==0){
data.remove(i);
list.remove(i);
}
EventBus.getDefault().post(jiSuan());
notifyDataSetChanged();
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
class ViewHodler1{
CheckBox abox;
TextView name,bianji;
}
class ViewHodler2{
CheckBox bbox;
TextView title,price;
Button del;
AddDelView ade;
}
//改變一級的checkbox的狀態
private void listCheckboxZT(int i,boolean flag){
UserBean.DataBean dataBean = list.get(i);
dataBean.setCheck(flag);
}
//改變二級的checkbox的狀態
private void dataCheckboxZT(int i,boolean flag){
List<UserBean.DataBean.ListBean> listBeen = data.get(i);
for (int j = 0; j <listBeen.size() ; j++) {
listBeen.get(j).setCheck(flag);
}
}
//改變全選狀態
private void QuanxuangZT(boolean flag){
MessageEvent messageEvent = new MessageEvent();
messageEvent.setCheck(flag);
EventBus.getDefault().post(messageEvent);
}
//判斷一級的checkbox是否全部選中
private boolean listCheckboxQX(){
for (int i = 0; i <list.size() ; i++) {
UserBean.DataBean dataBean = list.get(i);
List<UserBean.DataBean.ListBean> lista = dataBean.getList();
for (int j = 0; j <lista.size() ; j++) {
UserBean.DataBean.ListBean listBean = lista.get(j);
if (!listBean.isCheck()){
return false;
}
}
}
EventBus.getDefault().post(jiSuan());
return true;
}
//判斷二級的checkbox是否全部選中
private boolean dataCheckboxZT(int i){
List<UserBean.DataBean.ListBean> listBeen = data.get(i);
for (int j = 0; j <listBeen.size() ; j++) {
if (!listBeen.get(j).isCheck()){
return false;
}
}
return true;
}
private CountPrice jiSuan(){
int cout=0;
int price=0;
for (int i = 0; i <list.size() ; i++) {
UserBean.DataBean dataBean = list.get(i);
List<UserBean.DataBean.ListBean> datas = dataBean.getList();
for (int j = 0; j < datas.size(); j++) {
UserBean.DataBean.ListBean datasBean = datas.get(j);
if(datasBean.isCheck()){
cout+=datasBean.getCou();
price+=datasBean.getPrice()*datasBean.getCou();
}
}
}
CountPrice countPrice = new CountPrice();
countPrice.setCount(cout);
countPrice.setPrice(price);
return countPrice;
}
//===============================================
/**
* 全選的時候改變所有的checkbox的狀態
*/
public void checkALLStatus(boolean flag){
for (int i = 0; i <list.size() ; i++) {
UserBean.DataBean dataBean = list.get(i);
dataBean.setCheck(flag);
List<UserBean.DataBean.ListBean> datas = dataBean.getList();
for (int j = 0; j < datas.size(); j++) {
UserBean.DataBean.ListBean datasBean = datas.get(j);
datasBean.setCheck(flag);
}
}
EventBus.getDefault().post(jiSuan());
notifyDataSetChanged();
}
}
增加的一點小業務邏輯 自己套用把
點選編輯顯示和隱藏控制元件:主要邏輯:
private int isxian=View.VISIBLE;
//在一級列表裡面點選時
viewHodler1.bianji.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isxian==View.VISIBLE) {
isxian=View.GONE;
}else{
isxian=View.VISIBLE;
}
notifyDataSetChanged();
}
});
//在二級列表找到需要隱藏的控制元件給賦值
viewHodler2.del.setVisibility(isxian);