android仿淘宝等电商购物车(Expandablelistview)

项目需要,然后开始做,其实方式有多种,博主犯懒选了个简单的方式来进行实现的android仿淘宝等电商购物车(Expandablelistview)_第1张图片android仿淘宝等电商购物车(Expandablelistview)_第2张图片android仿淘宝等电商购物车(Expandablelistview)_第3张图片android仿淘宝等电商购物车(Expandablelistview)_第4张图片

按照购物车的需要,首先必须有商品,这个商品输入那个店铺的,这样抽象一下相当于要把多个一对多做成动态数组

这个是最基本的,再有就是需要在购物车中进行选择,编辑的操作

再有就是增加用户的信息,类似商品推荐等等,先把基本的做好

首先需要定义相应的实例,商品实例,店铺实例,店铺商品一对多实例,可以抽象成泛型,泛型真是好东西啊!!!一个是可以节约代码,另一个是后期维护比较简单

商品实例:

childitembean

package com.fanyafeng.nested.ExpandListView;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by fanyafeng on 2016/2/23,0023.
 */
public class ChildItemBean implements Parcelable,Comparable{
    private boolean childIsChecked;
    private String childImage;
    private String name;
    private int count;

    public ChildItemBean(boolean childIsChecked, String childImage, String name, int count) {
        this.childIsChecked = childIsChecked;
        this.childImage = childImage;
        this.name = name;
        this.count = count;
    }

    public boolean isChildIsChecked() {
        return childIsChecked;
    }

    public void setChildIsChecked(boolean childIsChecked) {
        this.childIsChecked = childIsChecked;
    }

    public String getChildImage() {
        return childImage;
    }

    public void setChildImage(String childImage) {
        this.childImage = childImage;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "ChildItemBean{" +
                "childIsChecked=" + childIsChecked +
                ", childImage='" + childImage + '\'' +
                ", name='" + name + '\'' +
                ", count=" + count +
                '}';
    }

    @Override
    public int compareTo(Object another) {
        return 0;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte((byte) (childIsChecked ? 1 : 0));
        dest.writeString(childImage);
        dest.writeString(name);
        dest.writeInt(count);
    }

    protected ChildItemBean(Parcel in) {
        childIsChecked = in.readByte() != 0;
        childImage = in.readString();
        name = in.readString();
        count = in.readInt();
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public ChildItemBean createFromParcel(Parcel in) {
            return new ChildItemBean(in);
        }

        @Override
        public ChildItemBean[] newArray(int size) {
            return new ChildItemBean[size];
        }
    };
}

商品xml布局:




    

        

        

        

        

        

            

店铺实例:

broupitembean

package com.fanyafeng.nested.ExpandListView;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by fanyafeng on 2016/2/23,0023.
 */
public class GroupItemBean implements Parcelable,Comparable{
    private boolean groupIsChecked;
    private String groupImage;
    private String groupName;
    private boolean groupIsCoupon;
    private boolean groupIsEdit;

    public GroupItemBean(boolean groupIsChecked, String groupImage, String groupName, boolean groupIsCoupon, boolean groupIsEdit) {
        this.groupIsChecked = groupIsChecked;
        this.groupImage = groupImage;
        this.groupName = groupName;
        this.groupIsCoupon = groupIsCoupon;
        this.groupIsEdit = groupIsEdit;
    }

    public boolean isGroupIsChecked() {
        return groupIsChecked;
    }

    public void setGroupIsChecked(boolean groupIsChecked) {
        this.groupIsChecked = groupIsChecked;
    }

    public String getGroupImage() {
        return groupImage;
    }

    public void setGroupImage(String groupImage) {
        this.groupImage = groupImage;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public boolean isGroupIsCoupon() {
        return groupIsCoupon;
    }

    public void setGroupIsCoupon(boolean groupIsCoupon) {
        this.groupIsCoupon = groupIsCoupon;
    }

    public boolean isGroupIsEdit() {
        return groupIsEdit;
    }

    public void setGroupIsEdit(boolean groupIsEdit) {
        this.groupIsEdit = groupIsEdit;
    }

    @Override
    public String toString() {
        return "GroupItemBean{" +
                "groupIsChecked=" + groupIsChecked +
                ", groupImage='" + groupImage + '\'' +
                ", groupName='" + groupName + '\'' +
                ", groupIsCoupon=" + groupIsCoupon +
                ", groupIsEdit=" + groupIsEdit +
                '}';
    }

    @Override
    public int compareTo(Object another) {
        return 0;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte((byte) (groupIsChecked ? 1 : 0));
        dest.writeString(groupImage);
        dest.writeString(groupName);
        dest.writeByte((byte) (groupIsCoupon ? 1 : 0));
        dest.writeByte((byte) (groupIsEdit ? 1 : 0));
    }

    protected GroupItemBean(Parcel in) {
        groupIsChecked = in.readByte() != 0;
        groupImage = in.readString();
        groupName = in.readString();
        groupIsCoupon = in.readByte() != 0;
        groupIsEdit = in.readByte() != 0;
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public GroupItemBean createFromParcel(Parcel in) {
            return new GroupItemBean(in);
        }

        @Override
        public GroupItemBean[] newArray(int size) {
            return new GroupItemBean[size];
        }
    };

}
店铺布局:





    

    

    

    

        

        

        
    


一对多实例:

expandbean

package com.fanyafeng.nested.ExpandListView;

import android.os.Parcel;
import android.os.Parcelable;

import java.util.List;

/**
 * Created by fanyafeng on 2016/2/22,0022.
 */
public class ExpandBean implements Comparable,Parcelable{
    private GroupItemBean Group;
    private List Child;

    public ExpandBean(GroupItemBean group, List child) {
        Group = group;
        Child = child;
    }

    public GroupItemBean getGroup() {
        return Group;
    }

    public void setGroup(GroupItemBean group) {
        Group = group;
    }

    public List getChild() {
        return Child;
    }

    public void setChild(List child) {
        Child = child;
    }

    @Override
    public String toString() {
        return "ExpandBean{" +
                "Group=" + Group +
                ", Child=" + Child +
                '}';
    }

    @Override
    public int compareTo(Object another) {
        return 0;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(Group, flags);
        dest.writeTypedList(Child);
    }

    protected ExpandBean(Parcel in) {
        Group = in.readParcelable(GroupItemBean.class.getClassLoader());
        Child = in.createTypedArrayList(ChildItemBean.CREATOR);
    }

    public static final Creator CREATOR = new Creator() {
        @Override
        public ExpandBean createFromParcel(Parcel in) {
            return new ExpandBean(in);
        }

        @Override
        public ExpandBean[] newArray(int size) {
            return new ExpandBean[size];
        }
    };
}
地址布局:





    

        

        
    

    

        

        
    

    

        

        
    

    

        

        
    

    

        

        
    



好,准备工作基本已经做完了,开始编写逻辑代码:

我的主要逻辑放在了adapter中了,而且把adapter从activity中进行抽离了,不过最后的业务处理应该放在activity中进行

expandadapter

package com.fanyafeng.nested.ExpandListView;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fanyafeng.nested.ChangeData.ChangeDataBean;
import com.fanyafeng.nested.ChangeData.ChangeDataDialog;
import com.fanyafeng.nested.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanyafeng on 2016/2/22,0022.
 */
public class ExpandAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List expandBeanList;

    private GroupHolder groupHolder;
    private ChildHolder childHolder;

    private AdapterCallback adapterCallback;

    public interface AdapterCallback {
        public void callBack(boolean allSelected, List expandBeanCallList);
    }

    public void setCallback(AdapterCallback adapterCallback) {
        this.adapterCallback = adapterCallback;
    }

    public void isAllSelect(boolean isAllSelected) {
        int groupSize;
        if (expandBeanList != null) {
            groupSize = expandBeanList.size();
        } else {
            return;
        }
        for (int i = 0; i < groupSize; i++) {
            expandBeanList.get(i).getGroup().setGroupIsChecked(isAllSelected);
            int childSize = expandBeanList.get(i).getChild().size();
            for (int j = 0; j < childSize; j++) {
                expandBeanList.get(i).getChild().get(j).setChildIsChecked(isAllSelected);
            }
        }
        notifyDataSetChanged();
    }


    public ExpandAdapter(Context context, List expandBeanList) {
        this.context = context;
        this.expandBeanList = expandBeanList;
    }

    @Override
    public int getGroupCount() {
        return expandBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return expandBeanList.get(groupPosition).getChild().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return expandBeanList.get(groupPosition).getGroup();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return expandBeanList.get(groupPosition).getChild().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 true;
    }

    class GroupHolder {
        CheckBox check_parent;
        SimpleDraweeView iv_expand_icon;
        TextView tv_expand_name;
        TextView tv_expand_edit;
        TextView tv_expand_get;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_parent_expand, null);
            groupHolder = new GroupHolder();
            groupHolder.check_parent = (CheckBox) convertView.findViewById(R.id.check_parent);
            groupHolder.iv_expand_icon = (SimpleDraweeView) convertView.findViewById(R.id.iv_expand_icon);
            groupHolder.tv_expand_name = (TextView) convertView.findViewById(R.id.tv_expand_name);
            groupHolder.tv_expand_get = (TextView) convertView.findViewById(R.id.tv_expand_get);
            groupHolder.tv_expand_edit = (TextView) convertView.findViewById(R.id.tv_expand_edit);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }
//        checkbox状态
        groupHolder.check_parent.setOnClickListener(new GroupClick(groupPosition));
//        是否处于选中状态
        if (expandBeanList.get(groupPosition).getGroup().isGroupIsChecked()) {
            groupHolder.check_parent.setChecked(true);
        } else {
            groupHolder.check_parent.setChecked(false);
        }
//        是否有优惠券
        if (expandBeanList.get(groupPosition).getGroup().isGroupIsCoupon()) {
            groupHolder.tv_expand_get.setVisibility(View.VISIBLE);
        } else {
            groupHolder.tv_expand_get.setVisibility(View.GONE);
        }
        groupHolder.iv_expand_icon.setImageURI(Uri.parse(expandBeanList.get(groupPosition).getGroup().getGroupImage()));
        groupHolder.tv_expand_name.setText(expandBeanList.get(groupPosition).getGroup().getGroupName());
        if (expandBeanList.get(groupPosition).getGroup().isGroupIsEdit()) {
            groupHolder.tv_expand_edit.setText("完成");
        } else {
            groupHolder.tv_expand_edit.setText("编辑");
        }
        groupHolder.tv_expand_edit.setOnClickListener(new GroupViewClick(groupPosition));
        return convertView;
    }

    class GroupClick implements View.OnClickListener {
        private int groupPosition;

        public GroupClick(int groupPosition) {
            this.groupPosition = groupPosition;
        }

        @Override
        public void onClick(View v) {
            int groupSize = expandBeanList.size();
            int childCount = expandBeanList.get(groupPosition).getChild().size();
            if (!expandBeanList.get(groupPosition).getGroup().isGroupIsChecked()) {
                expandBeanList.get(groupPosition).getGroup().setGroupIsChecked(true);
                for (int i = 0; i < childCount; i++) {
                    expandBeanList.get(groupPosition).getChild().get(i).setChildIsChecked(true);
                }

                boolean isAllChecked = true;
                for (int i = 0; i < groupSize; i++) {
                    if (!expandBeanList.get(i).getGroup().isGroupIsChecked()) {
                        isAllChecked = false;
                        break;
                    }
                }
                adapterCallback.callBack(isAllChecked, expandBeanList);

            } else {
                expandBeanList.get(groupPosition).getGroup().setGroupIsChecked(false);
                for (int i = 0; i < childCount; i++) {
                    expandBeanList.get(groupPosition).getChild().get(i).setChildIsChecked(false);
                }
                adapterCallback.callBack(false, expandBeanList);
            }
            notifyDataSetChanged();
        }
    }


    /**
     * 使某个组处于编辑状态
     * 

* groupPosition组的位置 */ class GroupViewClick implements View.OnClickListener { private int groupPosition; public GroupViewClick(int groupPosition) { this.groupPosition = groupPosition; } @Override public void onClick(View v) { int groupId = v.getId(); boolean isAllChecked = true; if (groupId == groupHolder.tv_expand_edit.getId()) { if (expandBeanList.get(groupPosition).getGroup().isGroupIsEdit()) { expandBeanList.get(groupPosition).getGroup().setGroupIsEdit(false); } else { expandBeanList.get(groupPosition).getGroup().setGroupIsEdit(true); } int groupSize = expandBeanList.size(); for (int i = 0; i < groupSize; i++) { if (!expandBeanList.get(i).getGroup().isGroupIsChecked()) { isAllChecked = false; break; } } adapterCallback.callBack(isAllChecked, expandBeanList); notifyDataSetChanged(); } } } class ChildHolder { CheckBox check_child; TextView tv_expand_child_name; TextView tv_done_edit; SimpleDraweeView iv_expand_child_icon; LinearLayout layout_is_edit; Button btn_count_reduce; EditText content_fu_count; Button btn_count_add; TextView tv_count_edit; TextView tv_count_del; TextView tv_child_note; TextView tv_child_express; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LinearLayout linearLayout = new LinearLayout(context); if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.layout_child_expand, null); childHolder = new ChildHolder(); childHolder.check_child = (CheckBox) convertView.findViewById(R.id.check_child); childHolder.tv_expand_child_name = (TextView) convertView.findViewById(R.id.tv_expand_child_name); childHolder.tv_done_edit = (TextView) convertView.findViewById(R.id.tv_done_edit); childHolder.iv_expand_child_icon = (SimpleDraweeView) convertView.findViewById(R.id.iv_expand_child_icon); childHolder.layout_is_edit = (LinearLayout) convertView.findViewById(R.id.layout_is_edit); childHolder.btn_count_reduce = (Button) convertView.findViewById(R.id.btn_count_reduce); childHolder.content_fu_count = (EditText) convertView.findViewById(R.id.content_fu_count); childHolder.btn_count_add = (Button) convertView.findViewById(R.id.btn_count_add); childHolder.tv_count_edit = (TextView) convertView.findViewById(R.id.tv_count_edit); childHolder.tv_count_del = (TextView) convertView.findViewById(R.id.tv_count_del); childHolder.tv_child_note = (TextView) convertView.findViewById(R.id.tv_child_note); childHolder.tv_child_express = (TextView) convertView.findViewById(R.id.tv_child_express); convertView.setTag(childHolder); } else { childHolder = (ChildHolder) convertView.getTag(); } if (expandBeanList.get(groupPosition).getChild().get(childPosition).isChildIsChecked()) { childHolder.check_child.setChecked(true); } else { childHolder.check_child.setChecked(false); } childHolder.check_child.setOnClickListener(new ChildClick(groupPosition, childPosition)); if (expandBeanList.get(groupPosition).getGroup().isGroupIsEdit()) { childHolder.tv_done_edit.setVisibility(View.GONE); childHolder.layout_is_edit.setVisibility(View.VISIBLE); } else { childHolder.tv_done_edit.setVisibility(View.VISIBLE); childHolder.layout_is_edit.setVisibility(View.GONE); } childHolder.tv_expand_child_name.setText(expandBeanList.get(groupPosition).getChild().get(childPosition).getName()); childHolder.tv_done_edit.setText("X" + expandBeanList.get(groupPosition).getChild().get(childPosition).getCount() + " 个"); childHolder.content_fu_count.setText(String.valueOf(expandBeanList.get(groupPosition).getChild().get(childPosition).getCount())); childHolder.iv_expand_child_icon.setImageURI(Uri.parse(expandBeanList.get(groupPosition).getChild().get(childPosition).getChildImage())); childHolder.btn_count_add.setOnClickListener(new ChildViewClick(groupPosition, childPosition)); childHolder.btn_count_reduce.setOnClickListener(new ChildViewClick(groupPosition, childPosition)); childHolder.tv_count_del.setOnClickListener(new DelChildViewClick(groupPosition, childPosition)); childHolder.tv_count_edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ChangeDataBean changeDataBean = new ChangeDataBean(123, "password"); ChangeDataDialog changeDataDialog = new ChangeDataDialog(context, R.style.mystyle, R.layout.layout_dialog_input, changeDataBean, new ChangeDataDialog.InputListener() { @Override public void getNameAndPassword(String number, String password) { Toast.makeText(context, number + password, Toast.LENGTH_SHORT).show(); } }); changeDataDialog.getWindow().setGravity(Gravity.BOTTOM); changeDataDialog.show(); } }); if (expandBeanList.get(groupPosition).getChild().size() == childPosition + 1) { childHolder.tv_child_note.setVisibility(View.VISIBLE); childHolder.tv_child_express.setVisibility(View.VISIBLE); int groupsize = expandBeanList.get(groupPosition).getChild().size(); int count = 0; for (int i = 0; i < groupsize; i++) { if (expandBeanList.get(groupPosition).getChild().get(i).isChildIsChecked()) { count += expandBeanList.get(groupPosition).getChild().get(i).getCount(); } } childHolder.tv_child_note.setText("数量:" + count + "个"); childHolder.tv_child_express.setText("快递费用:20元"); } else { childHolder.tv_child_note.setVisibility(View.GONE); childHolder.tv_child_express.setVisibility(View.GONE); } return convertView; } class ChildClick implements View.OnClickListener { private int groupPosition; private int childPosition; public ChildClick(int groupPosition, int childPosition) { this.groupPosition = groupPosition; this.childPosition = childPosition; } @Override public void onClick(View v) { int groupSize = expandBeanList.size(); int childCount = expandBeanList.get(groupPosition).getChild().size(); boolean isAllSelect = true; if (!expandBeanList.get(groupPosition).getChild().get(childPosition).isChildIsChecked()) { expandBeanList.get(groupPosition).getChild().get(childPosition).setChildIsChecked(true); for (int i = 0; i < childCount; i++) { if (!expandBeanList.get(groupPosition).getChild().get(i).isChildIsChecked()) { isAllSelect = false; break; } } if (isAllSelect) { expandBeanList.get(groupPosition).getGroup().setGroupIsChecked(true); } else { expandBeanList.get(groupPosition).getGroup().setGroupIsChecked(false); } boolean isAllChecked = true; for (int i = 0; i < groupSize; i++) { if (!expandBeanList.get(i).getGroup().isGroupIsChecked()) { isAllChecked = false; break; } } adapterCallback.callBack(isAllChecked, expandBeanList); } else { expandBeanList.get(groupPosition).getChild().get(childPosition).setChildIsChecked(false); expandBeanList.get(groupPosition).getGroup().setGroupIsChecked(false); adapterCallback.callBack(false, expandBeanList); } notifyDataSetChanged(); } } /** * 删除childview *

* groupPosition 组的位置 * childPosition 子的位置 */ class DelChildViewClick implements View.OnClickListener { private int groupPosition; private int childPosition; public DelChildViewClick(int groupPosition, int childPosition) { this.groupPosition = groupPosition; this.childPosition = childPosition; } @Override public void onClick(View v) { expandBeanList.get(groupPosition).getChild().remove(childPosition); if (expandBeanList.get(groupPosition).getChild().size() <= 0) { expandBeanList.remove(groupPosition); expandBeanList.get(groupPosition).getGroup().setGroupIsEdit(false); } notifyDataSetChanged(); } } /** * 增加,减少数据操作 *

* groupPosition 组的位置 * childPosition 子的位置 */ class ChildViewClick implements View.OnClickListener { private int groupPosition; private int childPosition; public ChildViewClick(int groupPosition, int childPosition) { this.groupPosition = groupPosition; this.childPosition = childPosition; } @Override public void onClick(View v) { int mycount = expandBeanList.get(groupPosition).getChild().get(childPosition).getCount(); int groupSize = expandBeanList.size(); if (v.getId() == childHolder.btn_count_add.getId()) { if (mycount < 99) { expandBeanList.get(groupPosition).getChild().get(childPosition).setCount(++mycount); } else { Toast.makeText(context, "已经超出最大数量", Toast.LENGTH_SHORT).show(); } } else if (v.getId() == childHolder.btn_count_reduce.getId()) { if (mycount > 1) { expandBeanList.get(groupPosition).getChild().get(childPosition).setCount(--mycount); } else { Toast.makeText(context, "符的个数不能小于1", Toast.LENGTH_SHORT).show(); } } boolean isAllChecked = true; for (int i = 0; i < groupSize; i++) { if (!expandBeanList.get(i).getGroup().isGroupIsChecked()) { isAllChecked = false; break; } } adapterCallback.callBack(isAllChecked, expandBeanList); notifyDataSetChanged(); } } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }


expandlistviewactivity

package com.fanyafeng.nested.ExpandListView;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.media.Image;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fanyafeng.nested.BaseActivity;
import com.fanyafeng.nested.ChangeData.ChangeDataBean;
import com.fanyafeng.nested.ChangeData.ChangeDataDialog;
import com.fanyafeng.nested.R;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExpandListViewActivity extends BaseActivity {
    private ExpandableListView expand_listview;
    private String imageUri = "http://www.apkbus.com/data/attachment/forum/201402/27/154958qgczo5a17ia3u3c4.png";
    private List expandBeanList = new ArrayList<>();
    private ExpandAdapter expandAdapter;

    private TextView tv_expand_price;
    private TextView tv_expand_commit;
    private CheckBox cb_expand_all;

    private boolean isAdapterAllChecked = false;
    private List myExpandBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expand_list_view);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        initView();
        initData();
    }

    private void initView() {
        cb_expand_all = (CheckBox) findViewById(R.id.cb_expand_all);
        cb_expand_all.setOnClickListener(this);
        tv_expand_price = (TextView) findViewById(R.id.tv_expand_price);
        tv_expand_commit = (TextView) findViewById(R.id.tv_expand_commit);
        tv_expand_commit.setOnClickListener(this);
        expand_listview = (ExpandableListView) findViewById(R.id.expand_listview);
        expand_listview.setGroupIndicator(null);

        List weiChildItemBeanList = new ArrayList<>();
        for (int i = 1; i <= 4; i++) {
            ChildItemBean childItemBean = new ChildItemBean(false, imageUri, "夏侯" + i, i);
            weiChildItemBeanList.add(childItemBean);
        }

        List shuChildItemBeanList = new ArrayList<>();
        for (int i = 1; i <= 3; i++) {
            ChildItemBean childItemBean = new ChildItemBean(false, imageUri, "赵云" + i, i);
            shuChildItemBeanList.add(childItemBean);
        }

        List wuChildItemBeanList = new ArrayList<>();
        for (int i = 1; i <= 6; i++) {
            ChildItemBean childItemBean = new ChildItemBean(false, imageUri, "周瑜" + i, i);
            wuChildItemBeanList.add(childItemBean);
        }

        List qunChildItemBeanList = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            ChildItemBean childItemBean = new ChildItemBean(false, imageUri, "华佗" + i, i);
            qunChildItemBeanList.add(childItemBean);
        }

        GroupItemBean groupItemBean0 = new GroupItemBean(false, imageUri, "魏国", false, false);
        ExpandBean expandBean0 = new ExpandBean(groupItemBean0, weiChildItemBeanList);
        expandBeanList.add(0, expandBean0);
        GroupItemBean groupItemBean1 = new GroupItemBean(false, imageUri, "蜀国", true, false);
        ExpandBean expandBean1 = new ExpandBean(groupItemBean1, shuChildItemBeanList);
        expandBeanList.add(1, expandBean1);
        GroupItemBean groupItemBean2 = new GroupItemBean(false, imageUri, "吴国", false, false);
        ExpandBean expandBean2 = new ExpandBean(groupItemBean2, wuChildItemBeanList);
        expandBeanList.add(2, expandBean2);
        GroupItemBean groupItemBean3 = new GroupItemBean(false, imageUri, "群国", true, false);
        ExpandBean expandBean3 = new ExpandBean(groupItemBean3, qunChildItemBeanList);
        expandBeanList.add(3, expandBean3);

    }

    private void initData() {
//同listview,可以添加头和脚
        View view = LayoutInflater.from(this).inflate(R.layout.layout_address_message, null);
        TextView tv_message_name = (TextView) view.findViewById(R.id.tv_message_name);
        tv_message_name.setText("樊亚风");
        TextView tv_message_note = (TextView) view.findViewById(R.id.tv_message_note);
        tv_message_note.setText("动态更新备注信息");

        expand_listview.addHeaderView(view);
//        expand_listview.addHeaderView(LayoutInflater.from(this).inflate(R.layout.layout_dialog_input, null));
        expand_listview.addFooterView(LayoutInflater.from(this).inflate(R.layout.layout_address_message, null));
        expand_listview.addFooterView(LayoutInflater.from(this).inflate(R.layout.layout_dialog_input, null));
        expandAdapter = new ExpandAdapter(this, expandBeanList);
        myExpandBeanList = expandBeanList;
        expand_listview.setAdapter(expandAdapter);
        expandAdapter.setCallback(new ExpandAdapter.AdapterCallback() {
            @Override
            public void callBack(boolean allSelected, List expandBeanCallList) {
                myExpandBeanList = expandBeanCallList;
                int count = 0;
                int groupSize = expandBeanCallList.size();
                for (int i = 0; i < groupSize; i++) {
                    int childSize = expandBeanCallList.get(i).getChild().size();
                    for (int j = 0; j < childSize; j++) {
                        if (expandBeanCallList.get(i).getChild().get(j).isChildIsChecked()) {
                            count += expandBeanCallList.get(i).getChild().get(j).getCount();
                        }
                    }
                }
                tv_expand_price.setText(count + "个");
                isAdapterAllChecked = allSelected;
                cb_expand_all.setChecked(allSelected);

            }
        });
//        expand_listview.setAdapter(expandableListAdapter);
//        将子项全部展开
        for (int i = 0; i < expandBeanList.size(); i++) {
            expand_listview.expandGroup(i);
        }
//        这是parent不能点击
        expand_listview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
            }
        });
        tv_message_note.setText("再次动态更新备注信息");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_expand_commit:
//                tv_expand_price.setText("确认点击");
                break;
            case R.id.cb_expand_all:
                isAdapterAllChecked = !isAdapterAllChecked;
                expandAdapter.isAllSelect(isAdapterAllChecked);
                int count = 0;
                int groupSize = myExpandBeanList.size();
                for (int i = 0; i < groupSize; i++) {
                    int childSize = myExpandBeanList.get(i).getChild().size();
                    for (int j = 0; j < childSize; j++) {
                        if (myExpandBeanList.get(i).getChild().get(j).isChildIsChecked()) {
                            count += myExpandBeanList.get(i).getChild().get(j).getCount();
                        }
                    }
                }
                tv_expand_price.setText(count + "个");
                break;
        }
    }

    final ExpandableListAdapter expandableListAdapter = new BaseExpandableListAdapter() {


        private String[] generalsTypes = new String[]{"魏国", "蜀国", "吴国"};
        private String[][] generals = new String[][]{
                {"夏侯淳", "甄姬", "许褚", "郭嘉", "司马", "杨修"},
                {"马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云", "马谡"},
                {"吕蒙", "陆逊", "孙权", "周瑜", "孙尚香"}
        };
        private String[][] images = new String[][]{
                {"夏侯淳", "甄姬", "许褚", "郭嘉", "司马", "杨修"},
                {"马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云", "马谡"},
                {"吕蒙", "陆逊", "孙权", "周瑜", "孙尚香"}
        };

        //group个数
        @Override
        public int getGroupCount() {
            return generalsTypes.length;
        }

        //        相应的group下的child个数
        @Override
        public int getChildrenCount(int groupPosition) {
            return generals[groupPosition].length;
        }

        //得到对应的group数据
        @Override
        public Object getGroup(int groupPosition) {
            return generalsTypes[groupPosition];
        }

        //得到相应group下child的数据
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return generals[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            View view = LayoutInflater.from(ExpandListViewActivity.this).inflate(R.layout.layout_parent_expand, null);
            TextView tv_expand_name = (TextView) view.findViewById(R.id.tv_expand_name);
            tv_expand_name.setText(getGroup(groupPosition).toString());
            TextView tv_expand_edit = (TextView) view.findViewById(R.id.tv_expand_edit);
            tv_expand_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(ExpandListViewActivity.this, "编辑被点击,第几组:" + groupPosition, Toast.LENGTH_SHORT).show();

                }
            });
            return view;
        }

        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            LinearLayout linearLayout = new LinearLayout(ExpandListViewActivity.this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            View view = LayoutInflater.from(ExpandListViewActivity.this).inflate(R.layout.layout_child_expand, null);
            TextView tv_expand_child_name = (TextView) view.findViewById(R.id.tv_expand_child_name);
            tv_expand_child_name.setText(getChild(groupPosition, childPosition).toString());
            tv_expand_child_name.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(ExpandListViewActivity.this, "Parent和Child分别为:" + groupPosition + " , " + childPosition, Toast.LENGTH_SHORT).show();
                }
            });
            TextView line = new TextView(ExpandListViewActivity.this);
            SimpleDraweeView iv_expand_child_icon = (SimpleDraweeView) view.findViewById(R.id.iv_expand_child_icon);
            iv_expand_child_icon.setImageURI(Uri.parse(imageUri));
            TextView tv_is_edit = (TextView) view.findViewById(R.id.tv_done_edit);

            linearLayout.addView(view, params);
            line.setTextColor(Color.BLACK);
            line.setHeight(1);
            if (!isLastChild) {
                linearLayout.addView(line);
            }
            return linearLayout;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    };


}

activity布局,现在用as自建貌似都是默认两个,一个activity_xxxx,另一个content_xxxx

activity




    

        

    

    

    



content




    

    

    

        

        

        
    



还有就是里面用了一个fresco,就是想知道大约这个jar包多大,放在项目里是否合适,貌似三四兆,有点大,感觉一般的用image_loader就可以了,除非有些处理不了的,比如你的项目里需要gif一类,或者需要高效处理图片,我这里的config是从官方demo里面抽出来的,大家可以去看一下官网的demo,文末放github地址,还有一个baseactivity

package com.fanyafeng.nested;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.fanyafeng.nested.config.FrescoConfig;

public class BaseActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fresco.initialize(this, FrescoConfig.getsImagePipelineConfig(this));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

    }
}
这里是fresco的地址 点击打开fresco github地址

我把写的这个demo也传到git上了,大家可以fork到自己的github下,或者直接check下来也可以

点击打开以上demo链接


屏幕录制的路径https://github.com/1181631922/Nested/blob/master/16-2-25-%E4%B8%8A%E5%8D%8811-43.mp4

写给自己:

我们以往的思维方式在很大程度上决定了我们每一次的选择,所以我们的选择并不是偶然的或随机的。我们只能在我们思想范围以内做出选择,而不会做出超出思想范围以外的任何行为。正是所谓的思路决定了出路,想法决定了方法。


你可能感兴趣的:(android零散知识学习)