//主类
package com.lixinyang.myeastofbeijing.Home.View.Fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import com.lixinyang.myeastofbeijing.Home.Modle.Adapter.Myadapter_expend;
import com.lixinyang.myeastofbeijing.Home.Modle.Bean.CartBean;
import com.lixinyang.myeastofbeijing.Home.Modle.Util.Httpconfig;
import com.lixinyang.myeastofbeijing.Home.Modle.modelImpl;
import com.lixinyang.myeastofbeijing.Home.Presenter.PresenterImpl;
import com.lixinyang.myeastofbeijing.Home.View.ShowCart_View;
import com.lixinyang.myeastofbeijing.Home.View.updatecastView;
import com.lixinyang.myeastofbeijing.R;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2018/1/8.
*/
public class Frag4 extends Fragment implements ShowCart_View,View.OnClickListener,updatecastView {
private View view;
private CartBean cartBean;
private ExpandableListView elv;
private CheckBox cart_cb;
private TextView cart_sum;
private TextView cart_jisuan;
private Myadapter_expend myadapter_expend;
boolean flag=true;
private int pid;
private int num;
private PresenterImpl presenter;
private String sellerid;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_home_frag4, null);
initView();
presenter = new PresenterImpl();
presenter.showcart(getActivity(),new modelImpl(),Frag4.this);
return view;
}
private void initView() {
elv = view.findViewById(R.id.elv);
cart_cb = view.findViewById(R.id.cart_cb);
cart_sum = view.findViewById(R.id.cart_sum);
cart_jisuan = view.findViewById(R.id.cart_jisuan);
cart_cb.setOnClickListener(this);
}
@Override
public String geturi() {
return new Httpconfig().getCart();
}
@Override
public void showosuccess(CartBean cartBean) {
this.cartBean = cartBean;
myadapter_expend = new Myadapter_expend(getActivity(), cartBean, this);
elv.setAdapter(myadapter_expend);
//让子布局全部展开
int groupCount = myadapter_expend.getGroupCount();
for (int i = 0; i < groupCount; i++) {
elv.expandGroup(i);
}
List
for (int i = 0; i < data.size(); i++) {
List
for (int j = 0; j < list.size(); j++) {
if(list.get(j).getSelected()==0){
flag = false;
}
}
}
cart_cb.setChecked(flag);
}
@Override
public void showerror(String error) {
Toast.makeText(getActivity(),error,Toast.LENGTH_SHORT).show();
}
@Override
public void showsum(double sum) {
}
@Override
public CartBean getCartBean() {
return cartBean;
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.cart_cb:
List
for (int i = 0; i < data.size(); i++) {
List
sellerid = data.get(i).getSellerid();
for (int j = 0; j < list.size(); j++) {
if(flag){
list.get(j).setSelected(0);
}else{
list.get(j).setSelected(1);
}
pid = (int) list.get(j).getPid();
num = (int) list.get(j).getNum();
presenter.updatecart(getActivity(),new modelImpl(),Frag4.this);
myadapter_expend.notifyDataSetChanged();
}
}
flag=!flag;
break;
}
}
@Override
public String updateuri() {
return new Httpconfig().getUpdatecart();
}
@Override
public String updateuid() {
return "71";
}
@Override
public String updatepid() {
return ""+pid;
}
@Override
public String updatesellerid() {
return sellerid;
}
@Override
public String updatenum() {
return num+"";
}
@Override
public String getselected() {
if(flag){
return "0";
}else{
return "1";
}
}
@Override
public void updatecasterror(String error) {
Toast.makeText(getContext(),error,Toast.LENGTH_SHORT).show();
}
@Override
public void updatecastsuccess(String success) {
}
}
//父
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:id="@+id/cart_cb_group"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000"/>
子
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cart_cb_child"/>
android:layout_height="50dp"
android:id="@+id/cart_iv_child"/>
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_height="wrap_content"
android:id="@+id/cart_tv1_child"/>
android:layout_height="wrap_content"
android:id="@+id/cart_tv2_child"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#f00"/>
fragment 布局
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="50dp"
>
android:id="@+id/gouwu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购物车"
android:textSize="21dp"
android:layout_centerHorizontal="true"/>
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/gouwu"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编辑"
android:textSize="16dp"
android:layout_marginRight="50dp"
android:layout_alignParentRight="true"/>
android:layout_marginRight="10dp"
android:src="@drawable/comm2"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_height="1px"
android:background="#999">
android:layout_height="wrap_content"
android:id="@+id/elv"
android:layout_weight="1"
android:groupIndicator="@null">
android:layout_height="0.75dp"
android:background="#999999"/>
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:text="全选"
android:textColor="@android:color/black"
android:textSize="25sp"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="合计:0"
android:textColor="@android:color/black"
android:textSize="25sp"
android:layout_marginLeft="-80dp"/>
android:layout_height="match_parent"
android:text="去计算"
android:textSize="20dp"
android:textColor="#fff"
android:background="#f00"
android:gravity="center"
android:layout_marginLeft="-80dp"
android:id="@+id/cart_jisuan"/>
adapterexpend
package com.lixinyang.myeastofbeijing.Home.Modle.Adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.lixinyang.myeastofbeijing.Home.Modle.Bean.CartBean;
import com.lixinyang.myeastofbeijing.Home.View.ShowCart_View;
import com.lixinyang.myeastofbeijing.R;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2018/1/18.
*/
public class Myadapter_expend extends BaseExpandableListAdapter {
private static final String TAG = "Myadapter_expend";
private List
private ShowCart_View showCart_view;
private CartBean cartBean;
private Context context;
public Myadapter_expend(Context context, CartBean cartBean, ShowCart_View showCart_view){
this.showCart_view = showCart_view;
this.context = context;
this.list = cartBean.getData();
this.cartBean = cartBean;
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int i) {
return list.get(i).getList().size();
}
@Override
public Object getGroup(int i) {
return list.get(i);
}
@Override
public Object getChild(int i, int i1) {
return list.get(i).getList().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(int i, boolean b, View view, ViewGroup viewGroup) {
ParentViewHolder parentViewHolder = null;
if(view==null){
view = LayoutInflater.from(context).inflate(R.layout.activity_cart_group, null);
CheckBox cart_cb_group = view.findViewById(R.id.cart_cb_group);
TextView cart_tv_group = view.findViewById(R.id.cart_tv_group);
parentViewHolder = new ParentViewHolder(cart_cb_group,cart_tv_group);
view.setTag(parentViewHolder);
}else{
parentViewHolder = (ParentViewHolder) view.getTag();
}
List
for (int j = 0; j < childlist.size(); j++) {
Log.e(TAG, "getGroupView: "+ childlist.get(j).getSelected());
if(childlist.get(j).getSelected()==0){
parentViewHolder.getParent_cb().setChecked(false);
}else{
parentViewHolder.getParent_cb().setChecked(true);
}
}
parentViewHolder.getParent_tv().setText(list.get(i).getSellerName());
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
ChildViewHolder childViewHolder = null;
if(view==null){
view = LayoutInflater.from(context).inflate(R.layout.activity_cart_child,null);
CheckBox cart_cb_child = view.findViewById(R.id.cart_cb_child);
ImageView cart_iv_child = view.findViewById(R.id.cart_iv_child);
TextView cart_tv1_child = view.findViewById(R.id.cart_tv1_child);
TextView cart_tv2_child = view.findViewById(R.id.cart_tv2_child);
TextView cart_tv3_child = view.findViewById(R.id.cart_tv3_child);
childViewHolder = new ChildViewHolder(cart_cb_child,cart_iv_child,cart_tv1_child,cart_tv2_child,cart_tv3_child);
view.setTag(childViewHolder);
}else{
childViewHolder = (ChildViewHolder) view.getTag();
}
if(list.get(i).getList().get(i1).getSelected()==0){
childViewHolder.getChild_cb().setChecked(false);
}else{
childViewHolder.getChild_cb().setChecked(true);
}
String images = list.get(i).getList().get(i1).getImages();
String imageUrl = images.split(".jpg")[0] + ".jpg";
Glide.with(context).load(imageUrl).into(childViewHolder.getChild_iv());
childViewHolder.getChild_tv1().setText(list.get(i).getList().get(i1).getTitle());
childViewHolder.getChild_tv2().setText(list.get(i).getList().get(i1).getSubhead()+"");
childViewHolder.getChild_tv3().setText(list.get(i).getList().get(i1).getPrice()+"");
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
//ViewHolder优化
class ParentViewHolder{
private TextView parent_tv;
private CheckBox parent_cb;
public ParentViewHolder(CheckBox parent_cb, TextView parent_tv){
this.parent_cb = parent_cb;
this.parent_tv = parent_tv;
}
public TextView getParent_tv() {
return parent_tv;
}
public void setParent_tv(TextView parent_tv) {
this.parent_tv = parent_tv;
}
public CheckBox getParent_cb() {
return parent_cb;
}
public void setParent_cb(CheckBox parent_cb) {
this.parent_cb = parent_cb;
}
}
class ChildViewHolder{
private CheckBox child_cb;
private ImageView child_iv;
private TextView child_tv1;
private TextView child_tv2;
private TextView child_tv3;
public ChildViewHolder(CheckBox child_cb, ImageView child_iv, TextView child_tv1, TextView child_tv2, TextView child_tv3){
this.child_cb = child_cb;
this.child_iv = child_iv;
this.child_tv1 = child_tv1;
this.child_tv2 = child_tv2;
this.child_tv3 = child_tv3;
}
public CheckBox getChild_cb() {
return child_cb;
}
public void setChild_cb(CheckBox child_cb) {
this.child_cb = child_cb;
}
public ImageView getChild_iv() {
return child_iv;
}
public void setChild_iv(ImageView child_iv) {
this.child_iv = child_iv;
}
public TextView getChild_tv1() {
return child_tv1;
}
public void setChild_tv1(TextView child_tv1) {
this.child_tv1 = child_tv1;
}
public TextView getChild_tv2() {
return child_tv2;
}
public void setChild_tv2(TextView child_tv2) {
this.child_tv2 = child_tv2;
}
public TextView getChild_tv3() {
return child_tv3;
}
public void setChild_tv3(TextView child_tv3) {
this.child_tv3 = child_tv3;
}
}
}