package bawei.com.demo2.v;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import bawei.com.demo2.R;
import bawei.com.demo2.adapter.myAdapter;
import bawei.com.demo2.bena.Product;
import bawei.com.demo2.contar.contarIntface;
import bawei.com.demo2.p.Mypsersenter;
/**
-
@Auther: 作者:张东升
-
@Date: 2019/4/4 11:14:41
-
@Description:
*/
public class ShouyeActivity extends Fragment implements contarIntface.vIntface {myAdapter adapter; RecyclerView recyclerView; public List<Product.DataBean> mList = new ArrayList<>(); contarIntface.Pintface pintface; public CheckBox mCheckBox; TextView textView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_shouye,container,false);
recyclerView = view.findViewById(R.id.RecyclerView_1_id);
textView = view.findViewById(R.id.title_sum);
mCheckBox =view.findViewById(R.id.chec_id);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
pintface = new Mypsersenter(this);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new myAdapter(getActivity(),mList);
recyclerView.setAdapter(adapter);
pintface.toModel();
init();
}
public void init(){
mCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {if(mCheckBox.isChecked()){ setChecAll(true); }else { setChecAll(false); textView.setText(0 + ""); } adapter.notifyDataSetChanged(); }
});
}
//全选中和全不选中的方法
public void setChecAll(boolean isChec){
for(int i = 0 ; i < mList.size() ; i++){
mList.get(i).isSelect = isChec;
//孩子集合
for(int j = 0 ; j < mList.get(i).getList().size() ; j++){
mList.get(i).getList().get(j).itemSelect = isChec;
}
}
total();
adapter.notifyDataSetChanged();
}//商家选中,或者不选中
/**
*-
@param i 商家的下标
-
@param tag 商家中孩子的下标,默认值位-1
-
@param isChec 是否为选中状态
*/
int value = 0;
public void setSjChecAll(int i ,int tag , boolean isChec){//商家的选中状态
mList.get(i).isSelect = isChec;
//商家中孩子的选中状态
if(tag != -1){
value = 0;
mList.get(i).getList().get(tag).itemSelect = false;
value = (int) mList.get(i).getList().get(tag).getPrice();
}else {
value = 0;
for(int j = 0 ; j < mList.get(i).getList().size() ; j++){
mList.get(i).getList().get(j).itemSelect = isChec;
value += (int)mList.get(i).getList().get(j).getPrice();
}}
total();
adapter.notifyDataSetChanged();
}
//单独点商家下的孩子
//选中的商家下的孩子的状态 Product.DataBean.ListBean listBean
public int setSjChildChec(boolean isChec , Object o){
Product.DataBean.ListBean listBean = (Product.DataBean.ListBean) o;
int n = 0;
for(int i = 0 ; i < mList.size() ;i ++ ){
for(int j = 0 ; j < mList.get(i).getList().size() ; j ++){
Product.DataBean.ListBean listBean_1 = mList.get(i).getList().get(j);
if(listBean_1 .equals(listBean)){
listBean_1.itemSelect = isChec;
n = i;
}
}
}
adapter.notifyDataSetChanged();
total();
return n;
}
//选中的商家下的孩子的状态来获取到商家的集合下标
public int getValue (boolean isChec , Object o){
Product.DataBean.ListBean listBean = (Product.DataBean.ListBean) o;
int n = 0;
for(int i = 0 ; i < mList.size() ;i ++ ){
for(int j = 0 ; j < mList.get(i).getList().size() ; j ++){
Product.DataBean.ListBean listBean_1 = mList.get(i).getList().get(j);
if(listBean_1 .equals(listBean)){
listBean_1.itemSelect = isChec;
n = i;
}
}
}
return n;
}
//计算总价
public void total(){
int sum_pic = 0;
for (int i = 0; i <mList.size() ; i++) {
for (int j = 0; j < mList.get(i).getList().size(); j++) {
if(mList.get(i).getList().get(j).itemSelect){
int pic = (int) mList.get(i).getList().get(j).getPrice();
int sum = mList.get(i).getList().get(j).ssum;
Log.e(“tag” ,"sum == " + sum + " jj = "+ j);
int to = pic * sum;
sum_pic += to;
}
}
}
textView.setText(sum_pic + “”);
}
@Override
public void showView(Object o) {
Product product = (Product) o;
List<Product.DataBean> list = product.getData();
mList.addAll(list);
adapter.notifyDataSetChanged();
}
} -