Android二级列表仿购物车布局

//一级列表
<?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="100dp"
    android:orientation="horizontal">
    <CheckBox
        android:id="@+id/cb_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="20dp"
        android:focusable="false"/>
    <TextView
        android:id="@+id/tv_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:text="标记"/>
    <TextView
        android:id="@+id/tv_dian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:text="标记"/>
</LinearLayout>
 
//二级列表
<?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="horizontal">

    <CheckBox
        android:id="@+id/cb_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="45dp"
        android:focusable="false" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="35dp"
            android:text="苹果" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv"
                android:layout_width="100dp"
                android:layout_height="100dp" />
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginLeft="20dp">
                <TextView
                    android:id="@+id/tv_pri"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="123" />
                <TextView
                    android:id="@+id/tv_infor"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="颜色:黑;尺寸:23L" />
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <ImageView
                        android:id="@+id/iv_del"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:src="@drawable/shopcart_minus_grey" />

                    <TextView
                        android:id="@+id/tv_num"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:background="@drawable/shopcart_add_btn"
                        android:paddingBottom="2dp"
                        android:paddingLeft="20dp"
                        android:paddingRight="20dp"
                        android:paddingTop="2dp"
                        android:text="1" />

                    <ImageView
                        android:id="@+id/iv_add"
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginLeft="5dp"
                        android:src="@drawable/shopcart_add_red" />

                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_del"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="35dp"
            android:text="删除" />
    </RelativeLayout>

</LinearLayout>
//自定义椭圆-shopcart_add_btn.xml
//在drawable目录下新建xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="200dp"></corners>
<stroke android:color="@color/colorPrimaryDark" android:width="1dp"></stroke>
</shape>
//购物车页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bwie.exam.view.GoodsActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ff3660">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="20dp"
            android:text="购物车"/>
    </RelativeLayout>
    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimary">
        <CheckBox
            android:id="@+id/checkAll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_toRightOf="@id/checkAll"
            android:layout_marginTop="10dp"
            android:text="全选"/>
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="20sp"
            android:text="合计:"/>
        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="¥"
            android:layout_toRightOf="@id/tv"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:textColor="#ff3660"/>
        <TextView
            android:id="@+id/number"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:background="#ff3660"
            android:textSize="20sp"
            android:gravity="center"
            android:text="结算(0)"/>
    </RelativeLayout>
</LinearLayout>
//展示列表的页面GoodsActivity
public class GoodsActivity extends AppCompatActivity implements IGoodsActivity {

    private ExpandableListView mElv;
    private CheckBox mCheckAll;
    /**
     * ¥价钱
     */
    private TextView mPrice;
    /**
     * 结算(0)
     */
    private TextView mNumber;
    private ElvAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        initView();
        new GoodsPresenter(this).getGoods();
    }

    private void initView() {
        mElv = (ExpandableListView) findViewById(R.id.elv);
        mCheckAll = (CheckBox) findViewById(R.id.checkAll);
        mPrice = (TextView) findViewById(R.id.price);
        mNumber = (TextView) findViewById(R.id.number);
    }

    @Override
    public void show(List<GoodsBean.DataBean> group, List<List<GoodsBean.DataBean.ListBean>> child) {
        adapter = new ElvAdapter(this, group, child);
        mElv.setAdapter(adapter);
        mElv.setGroupIndicator(null);
        //默认二级列表展开
        for (int i = 0; i < group.size(); i++) {
            mElv.expandGroup(i);
        }
    }
}
//IGoodsActivity
public interface IGoodsActivity {
    public void show(List<GoodsBean.DataBean> group,List<List<GoodsBean.DataBean.ListBean>> child);
}
//GoodsModel
public class GoodsModel extends BaseModel implements IGoodsModel {

    @Override
    public void getGoods(final OnNetListener<GoodsBean> onNetListener) {
        HttpUtil.getHttpUtil().doGet(Api.URL, new Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onFailure(e);
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                final GoodsBean goodsBean = new Gson().fromJson(string, GoodsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        onNetListener.onSuccess(goodsBean);
                    }
                });
            }
        });
    }
}
//BaseModel
public class BaseModel {
    public static Handler handler = new Handler(Looper.getMainLooper());
}
//IGoodsModel
public interface IGoodsModel {
    public void getGoods(OnNetListener<GoodsBean> onNetListener);
}
//GoodsPresenter
public class GoodsPresenter {
    private IGoodsModel iGoodsModel;
    private IGoodsActivity iGoodsActivity;

    public GoodsPresenter(IGoodsActivity iGoodsActivity) {
        this.iGoodsActivity = iGoodsActivity;
        iGoodsModel = new GoodsModel();
    }
    public void getGoods(){
        iGoodsModel.getGoods(new OnNetListener<GoodsBean>() {
            @Override
            public void onSuccess(GoodsBean goodsBean) {
                List<GoodsBean.DataBean> data = goodsBean.getData();
                List<List<GoodsBean.DataBean.ListBean>> child = new ArrayList<List<GoodsBean.DataBean.ListBean>>();
                for (int i = 0; i < data.size(); i++) {
                    List<GoodsBean.DataBean.ListBean> list = data.get(i).getList();
                    child.add(list);
                }
                iGoodsActivity.show(data,child);
            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }
}
//二级列表适配器
public class ElvAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<GoodsBean.DataBean> group;
    private List<List<GoodsBean.DataBean.ListBean>> child;
    private LayoutInflater inflater;

    public ElvAdapter(Context context, List<GoodsBean.DataBean> group, List<List<GoodsBean.DataBean.ListBean>> child) {
        this.context = context;
        this.group = group;
        this.child = child;
        inflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.get(groupPosition).get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder holder;
        if (convertView == null){
            holder = new GroupViewHolder();
            convertView = inflater.inflate(R.layout.group,null);
            holder.cb_group = (CheckBox) convertView.findViewById(R.id.cb_group);
            holder.tv_dian = (TextView) convertView.findViewById(R.id.tv_dian);
            convertView.setTag(holder);
        } else {
            holder = (GroupViewHolder) convertView.getTag();
        }
        GoodsBean.DataBean dataBean = group.get(groupPosition);
        holder.tv_dian.setText(dataBean.getSellerName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if (convertView == null){
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.child,null);
            holder.cb_child = (CheckBox) convertView.findViewById(R.id.cb_group);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);
            holder.tv_pri = (TextView) convertView.findViewById(R.id.tv_pri);
            holder.tv_infor = (TextView) convertView.findViewById(R.id.tv_infor);
            holder.iv_del = (ImageView) convertView.findViewById(R.id.iv_del);
            holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            holder.iv_add = (ImageView) convertView.findViewById(R.id.iv_add);
            holder.tv_del = (TextView) convertView.findViewById(R.id.tv_del);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        GoodsBean.DataBean.ListBean listBean = child.get(groupPosition).get(childPosition);
        holder.tv_title.setText(listBean.getTitle().substring(0,9));
        String[] split = listBean.getImages().split("\\|");
        ImageLoader.getInstance().displayImage(split[1],holder.iv);
        holder.tv_pri.setText(listBean.getPrice()+"");
        holder.tv_num.setText(listBean.getNum()+"");
        //二级列表的CheckBox
        holder.cb_child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置该条目对象里面的CheckBox属性值

            }
        });
        return convertView;
    }
    //优化列表
    //一级
    class GroupViewHolder{
        CheckBox cb_group;
        TextView tv_dian;
    }
    //二级
    class ChildViewHolder{
        CheckBox cb_child;
        TextView tv_title;
        ImageView iv;
        TextView tv_pri;
        TextView tv_infor;
        ImageView iv_del;
        TextView tv_num;
        ImageView iv_add;
        TextView tv_del;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值