xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="@layout/titlelayout"></include>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<com.example.gouwuche.view.MyExpandableListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:id="@+id/exlistview"
>
</com.example.gouwuche.view.MyExpandableListView>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:layout_marginLeft="10dp"
>
<CheckBox
android:layout_width="30dp"
android:layout_height="20dp"
android:id="@+id/check"
android:layout_centerVertical="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checked"
android:text="已选(0)"
android:textSize="20sp"
android:layout_toRightOf="@id/check"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
<Button
android:layout_width="80dp"
android:layout_height="50dp"
android:text="下单"
android:textSize="20sp"
android:layout_centerVertical="true"
android:textColor="#fff"
android:id="@+id/xiadan"
android:layout_alignParentRight="true"
android:background="#f00"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sum"
android:text="¥0.00"
android:textSize="22sp"
android:textColor="#f00"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@id/xiadan"
android:layout_centerVertical="true"
/>
</RelativeLayout>
</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="wrap_content"
android:background="#66363636"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/parent_title"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:text="22222"
/>
</LinearLayout>
//子
<?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="wrap_content">
<CheckBox
android:layout_width="30dp"
android:layout_height="30dp"
android:id="@+id/soncheck"
android:layout_margin="5dp"
android:layout_centerVertical="true"
/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:padding="10dp"
android:src="@mipmap/leftjiantou"
android:layout_toRightOf="@id/soncheck"
android:id="@+id/sonion"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sontitle"
android:layout_toRightOf="@id/sonion"
android:layout_marginLeft="5dp"
android:textSize="20sp"
android:text="1111"
android:layout_alignTop="@id/sonion"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Nowprice"
android:text="2222"
android:layout_alignBottom="@id/sonion"
android:layout_toRightOf="@id/sonion"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:textColor="#f00"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/oldprice"
android:textSize="18dp"
android:text="333"
android:layout_toRightOf="@id/Nowprice"
android:layout_alignBottom="@id/Nowprice"
android:layout_marginLeft="10dp"
/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/jian"
android:text="-"
android:layout_toRightOf="@id/oldprice"
android:layout_below="@id/sontitle"
android:layout_marginLeft="40dp"
/>
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/content"
android:text="1"
android:background="@drawable/squear"
android:layout_toRightOf="@id/jian"
android:layout_alignBottom="@id/jian"
android:gravity="center"
/>
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/jia"
android:layout_toRightOf="@id/content"
android:text="+"
android:layout_below="@id/sontitle"
/>
</RelativeLayout>
//头部:
<?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="wrap_content">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/leftjiantou"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购物车"
android:textSize="30sp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
</RelativeLayout>
activity:
package com.example.gouwuche;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ExpandableListView;
import com.example.gouwuche.adapter.ShowAdapter;
import com.example.gouwuche.bean.ShowBean;
import com.example.gouwuche.presenter.ShowPresenterimpl;
import com.example.gouwuche.view.MainView;
import com.example.gouwuche.view.MyExpandableListView;
import java.util.List;
public class MainActivity extends AppCompatActivity implements MainView{
private MyExpandableListView exlistview;
private ShowPresenterimpl showPresenterimpl;
private List<ShowBean.DataBean> data;
private ShowAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
exlistview = findViewById(R.id.exlistview);
showPresenterimpl = new ShowPresenterimpl(this);
showPresenterimpl.getData();
}
@Override
public void onSuccess(ShowBean showBean) {
data = showBean.getData();
Log.i("aaa",data.toString());
if(data!=null){
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter= new ShowAdapter(MainActivity.this,data);
exlistview.setAdapter(adapter);
int count = exlistview.getCount();
for (int i = 0; i <count ; i++) {
exlistview.expandGroup(i);
}
adapter.setCallback(new ShowAdapter.Callback() {
@Override
public void GoodCartsLitenster(int countyMoney, int countsNum) {
sum.setText("¥:"+countyMoney);
checked.setText("已选("+countsNum+")");
}
});
}
});
}
}
@Override
public void onError(int code) {
}
}
view:
package com.example.gouwuche.view;
import com.example.gouwuche.bean.ShowBean;
public interface MainView {
void onSuccess(ShowBean showBean);
void onError(int code);
}
//二级列表;
package com.example.gouwuche.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;
public class MyExpandableListView extends ExpandableListView {
public MyExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, i);
}
}
//model:
package com.example.gouwuche.model;
import com.example.gouwuche.bean.ShowBean;
public interface ShowModel {
//成功
void onSuccess(ShowBean showBean);
//shib
void onError(int code);
}
//实现:
package com.example.gouwuche.model;
import android.util.Log;
import com.example.gouwuche.bean.ShowBean;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class ShowModelimpl {
public void getData(final ShowModel showModel){
OkHttpClient okHttpClient=new OkHttpClient
.Builder()
.build();
Request request = new Request
.Builder()
.url("http://www.zhaoapi.cn/product/getCarts?uid=71")
.build();
//执行
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
showModel.onError(1);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Log.i("aaa",str.toString());
Gson gson=new Gson();
ShowBean showBean = gson.fromJson(str, ShowBean.class);
showModel.onSuccess(showBean);
}
});
}
}
//presenter
package com.example.gouwuche.presenter;
public interface ShowPresenter {
void onDestory();
}
package com.example.gouwuche.presenter;
import com.example.gouwuche.MainActivity;
import com.example.gouwuche.bean.ShowBean;
import com.example.gouwuche.model.ShowModel;
import com.example.gouwuche.model.ShowModelimpl;
import com.example.gouwuche.view.MainView;
public class ShowPresenterimpl implements ShowPresenter{
private MainView mainView;
private ShowModelimpl showModelimpl;
public ShowPresenterimpl(MainView mainView) {
this.mainView=mainView;
this.showModelimpl = new ShowModelimpl();
}
public void getData(){
showModelimpl.getData(new ShowModel() {
@Override
public void onSuccess(ShowBean showBean) {
mainView.onSuccess(showBean);
}
@Override
public void onError(int code) {
mainView.onError(1);
}
});
}
//解绑
@Override
public void onDestory() {
if(mainView!=null){
mainView=null;
}
}
}
//adapter
package com.example.gouwuche.adapter;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.gouwuche.R;
import com.example.gouwuche.bean.ShowBean;
import java.util.List;
public class ShowAdapter extends BaseExpandableListAdapter{
private Context context;
private List<ShowBean.DataBean> list;
//选中的货物的钱数
private int countsMoney ;
//选中的货物的个数
private int countsNum ;
private Callback callback;
public void setCallback(Callback callback) {
this.callback = callback;
}
public ShowAdapter(Context context, List<ShowBean.DataBean> list) {
this.context = context;
this.list = list;
}
public ShowAdapter(Context context, List<ShowBean.DataBean> list, Callback callback) {
this.context = context;
this.list = list;
this.callback=callback;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
getcountsMoneyAndNum();
callback.GoodCartsLitenster(countsMoney,countsNum);
}
public void setIsSelect(boolean isSelect){
for(ShowBean.DataBean li:list){
List<ShowBean.DataBean.ListBean> list1= li.getList();
for(ShowBean.DataBean.ListBean l:list1){
if(isSelect){
l.setSelected(0);
}else {
l.setSelected(1);
}
}
}
notifyDataSetChanged();
}
private void getcountsMoneyAndNum() {
countsMoney=0;
countsNum=0;
if(list==null){
return;
}
for(ShowBean.DataBean li:list){
List<ShowBean.DataBean.ListBean> list1= li.getList();
for(ShowBean.DataBean.ListBean l:list1){
if(l.getSelected()==1){
countsMoney+=(l.getNum()*l.getBargainPrice());
countsNum+=l.getNum();
}
}
}
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int i) {
return list.get(i).getList().size();
}
@Override
public Object getGroup(int i) {
return list.get(i);
}
@Override
public Object getChild(int i, int i1) {
return list.get(i).getList().get(i1);
}
@Override
public long getGroupId(int i) {
return i+1;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
ViewHolderOne holderOne=null;
if(view==null){
holderOne=new ViewHolderOne();
view= View.inflate(context, R.layout.parentlayout, null);
holderOne.parentTitle=view.findViewById(R.id.parent_title);
view.setTag(holderOne);
}else {
holderOne= (ViewHolderOne) view.getTag();
}
holderOne.parentTitle.setText(list.get(i).getSellerName());
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
ViewHolderTwo holderTwo=null;
if(view==null){
holderTwo=new ViewHolderTwo();
view=View.inflate(context,R.layout.sonlayout,null);
holderTwo.NowPrice=view.findViewById(R.id.Nowprice);
holderTwo.OldPrice=view.findViewById(R.id.oldprice);
holderTwo.sontitle=view.findViewById(R.id.sontitle);
holderTwo.sonion=view.findViewById(R.id.sonion);
holderTwo.add= view.findViewById(R.id.jia);
holderTwo.contact=view.findViewById(R.id.content);
holderTwo.jian=view.findViewById(R.id.jian);
holderTwo.check=view.findViewById(R.id.soncheck);
view.setTag(holderTwo);
}else {
holderTwo = (ViewHolderTwo) view.getTag();
}
holderTwo.sontitle.setText(this.list.get(i).getList().get(i1).getTitle());
holderTwo.OldPrice.setText("¥"+ this.list.get(i).getList().get(i1).getBargainPrice());
holderTwo.NowPrice.setText("¥"+ this.list.get(i).getList().get(i1).getPrice());
holderTwo.contact.setText(list.get(i).getList().get(i1).getNum()+"");
String str = this.list.get(i).getList().get(i1).getImages().split("\\|")[0];
Glide.with(context).load(str).into(holderTwo.sonion);
//添加事件
holderTwo.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = list.get(i).getList().get(i1).getNum();
num++;
list.get(i).getList().get(i1).setNum(num);
notifyDataSetChanged();
}
});
//减事件
holderTwo.jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = list.get(i).getList().get(i1).getNum();
if(num>1){
num--;
}else{
num=0;
}
list.get(i).getList().get(i1).setNum(num);
notifyDataSetChanged();
}
});
holderTwo.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selected = list.get(i).getList().get(i1).getSelected();
if(selected==0){
list.get(i).getList().get(i1).setSelected(1);
}else if(selected==1){
list.get(i).getList().get(i1).setSelected(0);
}
notifyDataSetChanged();
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class ViewHolderOne{
TextView parentTitle;
}
class ViewHolderTwo{
TextView NowPrice;
TextView OldPrice;
TextView sontitle;
ImageView sonion;
CheckBox check;
Button add;
TextView contact;
Button jian;
}
public interface Callback {
void GoodCartsLitenster(int countyMoney, int countsNum);
}
}
购物车界面设计与实现
1061

被折叠的 条评论
为什么被折叠?



