项目需要,然后开始做,其实方式有多种,博主犯懒选了个简单的方式来进行实现的
按照购物车的需要,首先必须有商品,这个商品输入那个店铺的,这样抽象一下相当于要把多个一对多做成动态数组
这个是最基本的,再有就是需要在购物车中进行选择,编辑的操作
再有就是增加用户的信息,类似商品推荐等等,先把基本的做好
首先需要定义相应的实例,商品实例,店铺实例,店铺商品一对多实例,可以抽象成泛型,泛型真是好东西啊!!!一个是可以节约代码,另一个是后期维护比较简单
商品实例:
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<ChildItemBean> CREATOR = new Creator<ChildItemBean>() {
@Override
public ChildItemBean createFromParcel(Parcel in) {
return new ChildItemBean(in);
}
@Override
public ChildItemBean[] newArray(int size) {
return new ChildItemBean[size];
}
};
}
商品xml布局:
<?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="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/check_child"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:gravity="center_vertical" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/iv_expand_child_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center_vertical" />
<TextView
android:id="@+id/tv_expand_child_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="旗舰店"
android:textColor="@android:color/black"
android:textSize="18dp" />
<TextView
android:id="@+id/tv_done_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:text="x1 道符"
android:textColor="@android:color/black"
android:textSize="18dp" />
<LinearLayout
android:id="@+id/layout_is_edit"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="gone">
<Button
android:id="@+id/btn_count_reduce"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:background="#9F897E"
android:text="—"
android:textColor="@android:color/white" />
<EditText
android:id="@+id/content_fu_count"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#E5DED8"
android:digits="0123456789"
android:gravity="center_vertical|center_horizontal"
android:inputType="number"
android:longClickable="false"
android:maxLength="2"
android:text="1" />
<Button
android:id="@+id/btn_count_add"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginRight="8dp"
android:background="#9F897E"
android:text="+"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/tv_count_edit"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginRight="8dp"
android:gravity="center_vertical|center_horizontal"
android:text="编辑"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/tv_count_del"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="@android:color/holo_red_light"
android:gravity="center_vertical|center_horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="删除"
android:textColor="@android:color/black" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tv_child_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="备注"
android:textColor="@android:color/black"
android:textSize="18dp"
android:visibility="gone" />
<TextView
android:id="@+id/tv_child_express"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:padding="10dp"
android:text="快递费:20元"
android:textColor="@android:color/black"
android:textSize="18dp"
android:visibility="gone" />
</LinearLayout>
店铺实例:
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<GroupItemBean> CREATOR = new Creator<GroupItemBean>() {
@Override
public GroupItemBean createFromParcel(Parcel in) {
return new GroupItemBean(in);
}
@Override
public GroupItemBean[] newArray(int size) {
return new GroupItemBean[size];
}
};
}
店铺布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/check_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/iv_expand_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/check_parent"
android:background="@mipmap/ic_launcher"
android:gravity="center_vertical" />
<TextView
android:id="@+id/tv_expand_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv_expand_icon"
android:gravity="center_vertical"
android:text="某某某旗舰店"
android:textColor="@android:color/black"
android:textSize="18dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_expand_get"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_expand_name"
android:gravity="center_vertical"
android:text="领券"
android:layout_centerVertical="true"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/tv_upright_line"
android:layout_width="1px"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_toRightOf="@+id/tv_expand_get"
android:background="@android:color/black" />
<TextView
android:id="@+id/tv_expand_edit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_upright_line"
android:gravity="center_vertical"
android:text="编辑"
android:layout_centerVertical="true"
android:textColor="@android:color/black" />
</LinearLayout>
</RelativeLayout>
一对多实例:
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<ChildItemBean> Child;
public ExpandBean(GroupItemBean group, List<ChildItemBean> child) {
Group = group;
Child = child;
}
public GroupItemBean getGroup() {
return Group;
}
public void setGroup(GroupItemBean group) {
Group = group;
}
public List<ChildItemBean> getChild() {
return Child;
}
public void setChild(List<ChildItemBean> 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<ExpandBean> CREATOR = new Creator<ExpandBean>() {
@Override
public ExpandBean createFromParcel(Parcel in) {
return new ExpandBean(in);
}
@Override
public ExpandBean[] newArray(int size) {
return new ExpandBean[size];
}
};
}
地址布局:
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="收货信息"
android:textColor="@android:color/black"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:text="编辑"
android:textColor="@android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="收货人"
android:textColor="@android:color/black"
android:textSize="16dp" />
<TextView
android:id="@+id/tv_message_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:text="姓名"
android:textColor="@android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="手机"
android:textColor="@android:color/black"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:text="1380000000"
android:textColor="@android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="收货地址"
android:textColor="@android:color/black"
android:textSize="16dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:text="北京市东城区"
android:textColor="@android:color/black"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="备注"
android:textColor="@android:color/black"
android:textSize="16dp" />
<TextView
android:id="@+id/tv_message_note"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:text="看看效果"
android:textColor="@android:color/black"
android:textSize="16dp" />
</RelativeLayout>
</LinearLayout>
好,准备工作基本已经做完了,开始编写逻辑代码:
我的主要逻辑放在了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<ExpandBean> expandBeanList;
private GroupHolder groupHolder;
private ChildHolder childHolder;
private AdapterCallback adapterCallback;
public interface AdapterCallback {
public void callBack(boolean allSelected, List<ExpandBean> 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<ExpandBean> 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();
}
}
/**
* 使某个组处于编辑状态
* <p>
* 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
* <p>
* 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();
}
}
/**
* 增加,减少数据操作
* <p>
* 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<ExpandBean> 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<ExpandBean> 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<ChildItemBean> weiChildItemBeanList = new ArrayList<>();
for (int i = 1; i <= 4; i++) {
ChildItemBean childItemBean = new ChildItemBean(false, imageUri, "夏侯" + i, i);
weiChildItemBeanList.add(childItemBean);
}
List<ChildItemBean> shuChildItemBeanList = new ArrayList<>();
for (int i = 1; i <= 3; i++) {
ChildItemBean childItemBean = new ChildItemBean(false, imageUri, "赵云" + i, i);
shuChildItemBeanList.add(childItemBean);
}
List<ChildItemBean> wuChildItemBeanList = new ArrayList<>();
for (int i = 1; i <= 6; i++) {
ChildItemBean childItemBean = new ChildItemBean(false, imageUri, "周瑜" + i, i);
wuChildItemBeanList.add(childItemBean);
}
List<ChildItemBean> 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<ExpandBean> 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
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.fanyafeng.nested.ExpandListView.ExpandListViewActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_expand_list_view" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fanyafeng.nested.ExpandListView.ExpandListViewActivity"
tools:showIn="@layout/activity_expand_list_view">
<ExpandableListView
android:id="@+id/expand_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"
android:background="#ffffff"
android:cacheColorHint="#00000000"
android:divider="@null"
android:listSelector="#00000000">
</ExpandableListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_expand_all"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="全选" />
<TextView
android:id="@+id/tv_expand_price"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/white"
android:gravity="center_horizontal|center_vertical"
android:padding="10dp"
android:text="个数"
android:textSize="20dp" />
<TextView
android:id="@+id/tv_expand_commit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/holo_red_light"
android:gravity="center_horizontal|center_vertical"
android:padding="10dp"
android:text="确认"
android:textSize="20dp" />
</LinearLayout>
</RelativeLayout>
还有就是里面用了一个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下来也可以
屏幕录制的路径https://github.com/1181631922/Nested/blob/master/16-2-25-%E4%B8%8A%E5%8D%8811-43.mp4
写给自己:
我们以往的思维方式在很大程度上决定了我们每一次的选择,所以我们的选择并不是偶然的或随机的。我们只能在我们思想范围以内做出选择,而不会做出超出思想范围以外的任何行为。正是所谓的思路决定了出路,想法决定了方法。