<?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">
<TextView
android:id="@+id/four_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="25dp"
android:text="购物车"
android:background="#a6a1a1"
android:gravity="center"/>
<ExpandableListView
android:layout_weight="9"
android:id="@+id/exp_listview"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="60dp">
<CheckBox
android:id="@+id/qx_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总价:"
android:textColor="#f00"
android:textSize="20dp"
android:layout_marginLeft="30dp"
/>
<TextView
android:id="@+id/zong_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥0.00"
android:textSize="20dp"
android:layout_weight="1"
/>
<Button
android:id="@+id/jiesuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="去结算(0)"
/>
</LinearLayout>
</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:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/gcheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/g_text_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="30dp"
android:text="xxx"/>
</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="60dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/c_checked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/c_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/c_shangname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:text="标题"
android:textColor="#000" />
<TextView
android:id="@+id/c_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="@android:color/holo_red_dark"
android:textSize="20sp" />
</LinearLayout>
<com.bwie.chuliangliang.jingdong_demo.mvp.ui.view.MyAddSubView
android:id="@+id/c_addsub"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp"/>
</LinearLayout>
public class MyExplistAdapter extends BaseExpandableListAdapter{
List<CartBean.DataBean> dataBeans;
OnCartListChangeListener onCartListChangeListener;
public MyExplistAdapter(List<CartBean.DataBean> dataBeans) {
this.dataBeans = dataBeans;
}
@Override
public int getGroupCount() {
return dataBeans.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return dataBeans.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder gholder;
if (null == convertView){
convertView = View.inflate(parent.getContext(), R.layout.gitme,null);
gholder = new GroupHolder();
gholder.g_checkBox = convertView.findViewById(R.id.gcheck);
gholder.g_textname = convertView.findViewById(R.id.g_text_name);
convertView.setTag(gholder);
}else{
gholder = (GroupHolder) convertView.getTag();
}
gholder.g_textname.setText(dataBeans.get(groupPosition).getSellerName());
boolean b = isCurrentSellerAllProductSelected(groupPosition);
gholder.g_checkBox.setChecked(b);
gholder.g_checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onCartListChangeListener.SellerSelectedChange(groupPosition);
}
});
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder cholder;
if (null == convertView){
convertView = View.inflate(parent.getContext(), R.layout.citme, null);
cholder = new ChildHolder();
cholder.c_checked = convertView.findViewById(R.id.c_checked);
cholder.c_image = convertView.findViewById(R.id.c_image);
cholder.c_price = convertView.findViewById(R.id.c_price);
cholder.c_shangname = convertView.findViewById(R.id.c_shangname);
cholder.c_myAddSub = convertView.findViewById(R.id.c_addsub);
convertView.setTag(cholder);
}else{
cholder = (ChildHolder) convertView.getTag();
}
CartBean.DataBean.ListBean listBean = dataBeans.get(groupPosition).getList().get(childPosition);
cholder.c_shangname.setText(listBean.getTitle());
cholder.c_price.setText("¥"+listBean.getPrice()+"");
String images = listBean.getImages();
String[] split = images.split("\\|");
cholder.c_image.setImageURI(Uri.parse(split[0]));
cholder.c_checked.setChecked(listBean.getSelected()==1);
cholder.c_checked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onCartListChangeListener.onchildClick(childPosition,groupPosition);
}
});
cholder.c_myAddSub.setNumber(dataBeans.get(groupPosition).getList().get(childPosition).getNum());
cholder.c_myAddSub.setOnSubAddClickLinck(new MyAddSubView.OnSubAddClickLinck() {
@Override
public void onSubAddClickLinck(int number) {
onCartListChangeListener.onDiBuselect(groupPosition,childPosition,number);
//dataBeans.get(groupPosition).getList().get(childPosition).setNum(number);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public boolean isQxUserseleck() {
for (int i = 0; i < dataBeans.size(); i++) {
List<CartBean.DataBean.ListBean> listBeans = dataBeans.get(i).getList();
for (int j = 0; j < listBeans.size(); j++) {
int selected = listBeans.get(j).getSelected();
if (selected==0){
return false;
}
}
}
return true;
}
public boolean isCurrentSellerAllProductSelected(int groupPosition) {
List<CartBean.DataBean.ListBean> list = dataBeans.get(groupPosition).getList();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getSelected() == 0) {
return false;
}
}
return true;
}
public void changeQxUserSeleck(boolean b) {
for (int i = 0; i < dataBeans.size(); i++) {
List<CartBean.DataBean.ListBean> list = dataBeans.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setSelected(b?1:0);
}
}
}
public void shangCheckSleeck(int groupPostion, boolean b) {
List<CartBean.DataBean.ListBean> list = dataBeans.get(groupPostion).getList();
for (int i = 0; i < list.size(); i++) {
list.get(i).setSelected(b?1:0);
}
}
public void childChecked(int childPostion, int groupPostion) {
CartBean.DataBean.ListBean listBean = dataBeans.get(groupPostion).getList().get(childPostion);
listBean.setSelected(listBean.getSelected() == 0?1:0);
}
public boolean seiDiBu() {
for (int i = 0; i < dataBeans.size(); i++) {
List<CartBean.DataBean.ListBean> list = dataBeans.get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected()==0){
return false;
}
}
}
return true;
}
public double zongjia() {
double totalPrice = 0;
for (int i = 0; i < dataBeans.size(); i++) {
List<CartBean.DataBean.ListBean> list = dataBeans.get(i).getList();
for (int j = 0; j < list.size(); j++) {
if (list.get(j).getSelected() == 1) {
double price = list.get(j).getPrice();
int num = list.get(j).getNum();
totalPrice += price *num;
}
}
}
return totalPrice;
}
public void setNum(int groupPostion, int childPostion, int num) {
dataBeans.get(groupPostion).getList().get(childPostion).setNum(num);
}
class GroupHolder{
CheckBox g_checkBox;
TextView g_textname;
}
class ChildHolder{
CheckBox c_checked;
ImageView c_image;
TextView c_shangname;
TextView c_price;
MyAddSubView c_myAddSub;
}
public void setOnCartListChangeListener(OnCartListChangeListener onCartListChangeListener){
this.onCartListChangeListener = onCartListChangeListener;
}
public interface OnCartListChangeListener{
void SellerSelectedChange(int groupPostion);
void onchildClick(int childPostion, int groupPostion);
void onDiBuselect(int groupPostion, int childPostion, int num);
}
}
public class FourFragment extends BaseFragment<FourPresenter> implements FourContract.View {
Unbinder unbinder;
@BindView(R.id.four_text)
TextView fourText;
@BindView(R.id.exp_listview)
ExpandableListView expListview;
@BindView(R.id.qx_check)
CheckBox qxCheck;
@BindView(R.id.zong_price)
TextView zongPrice;
@BindView(R.id.jiesuan)
Button jiesuan;
private int uid;
private String name;
private List<CartBean.DataBean> data;
private MyExplistAdapter adapter;
private Retrofit retrofit;
private double zongjia;
private int zongjia1;
@Override
public void setupFragmentComponent(@NonNull AppComponent appComponent) {
DaggerFourComponent //如找不到该类,请编译一下项目
.builder()
.appComponent(appComponent)
.fourModule(new FourModule(this))
.build()
.inject(this);
}
@Override
public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_four, container, false);
}
@Override
public void initData(@Nullable Bundle savedInstanceState) {
SharedPreferences user = getActivity().getSharedPreferences("User", 0);
name = user.getString("name", null);
user.getString("msg", "登录失败");
uid = user.getInt("uid", 20394);
if (name == null) {
Toast.makeText(getActivity(), "请登录账号....", Toast.LENGTH_SHORT).show();
} else {
mPresenter.getRequestCartMsg(uid);
}
}
/**
* 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
* 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
* 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
* <p>
* 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
* 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
* 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
* <p>
* Example usage:
* <pre>
* public void setData(@Nullable Object data) {
* if (data != null && data instanceof Message) {
* switch (((Message) data).what) {
* case 0:
* loadData(((Message) data).arg1);
* break;
* case 1:
* refreshUI();
* break;
* default:
* //do something
* break;
* }
* }
* }
*
* // call setData(Object):
* Message data = new Message();
* data.what = 0;
* data.arg1 = 1;
* fragment.setData(data);
* </pre>
*
* @param data 当不需要参数时 {@code data} 可以为 {@code null}
*/
@Override
public void setData(@Nullable Object data) {
}
@Override
public void showLoading() {
}
@Override
public void hideLoading() {
}
@Override
public void showMessage(@NonNull String message) {
checkNotNull(message);
ArmsUtils.snackbarText(message);
}
@Override
public void launchActivity(@NonNull Intent intent) {
checkNotNull(intent);
ArmsUtils.startActivity(intent);
}
@Override
public void killMyself() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO: inflate a fragment view
View rootView = super.onCreateView(inflater, container, savedInstanceState);
unbinder = ButterKnife.bind(this, rootView);
return rootView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void showData(String responseString) {
Gson gson = new Gson();
CartBean cartBean = gson.fromJson(responseString, CartBean.class);
data = cartBean.getData();
adapter = new MyExplistAdapter(data);
expListview.setAdapter(adapter);
for (int i = 0; i < data.size(); i++) {
expListview.expandGroup(i);
}
adapter.setOnCartListChangeListener(new MyExplistAdapter.OnCartListChangeListener() {
@Override
public void SellerSelectedChange(int groupPostion) {
boolean b = adapter.isCurrentSellerAllProductSelected(groupPostion);
adapter.shangCheckSleeck(groupPostion, !b);
adapter.notifyDataSetChanged();
onSetQxCheck();
}
@Override
public void onchildClick(int childPostion, int groupPostion) {
adapter.childChecked(childPostion, groupPostion);
adapter.notifyDataSetChanged();
onSetQxCheck();
}
@Override
public void onDiBuselect(int groupPostion, int childPostion, int num) {
//dataBeans.get(groupPostion).getList().get(childPostion).setNum(num);
adapter.setNum(groupPostion, childPostion, num);
adapter.notifyDataSetChanged();
onSetQxCheck();
}
});
}
@Override
public void showDelete(String responseString) {
}
@OnClick({R.id.qx_check, R.id.jiesuan})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.qx_check:
boolean qxUserChecked = adapter.isQxUserseleck();
adapter.changeQxUserSeleck(!qxUserChecked);
adapter.notifyDataSetChanged();
onSetQxCheck();
break;
case R.id.jiesuan:
if (qxCheck.isChecked()==true){
getRequestChuanjianDingDan();
}
break;
}
}
public void getRequestChuanjianDingDan() {
retrofit = new Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(Api.APP_DOMAIN)
.build();
OneFragmentService oneFragmentService = retrofit.create(OneFragmentService.class);
Observable<ResponseBody> observable = oneFragmentService.getRequestChuangJianDingDanMsg(uid,zongjia);
observable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ResponseBody>() {
@Override
public void accept(ResponseBody responseBody) throws Exception {
String responseString = responseBody.string();
Gson gson = new Gson();
AddDingDanBean addDingDanBean = gson.fromJson(responseString, AddDingDanBean.class);
Toast.makeText(getActivity(), addDingDanBean.getMsg(), Toast.LENGTH_SHORT).show();
}
});
}
public void onSetQxCheck() {
boolean b = adapter.seiDiBu();
qxCheck.setChecked(b);
adapter.notifyDataSetChanged();
zongjia = adapter.zongjia();
zongPrice.setText("¥" + zongjia);
}
}
public class MyAddSubView extends LinearLayout implements View.OnClickListener {
private TextView tv_sub_view;
private TextView tv_number_view;
private TextView tv_add_view;
int number = 1;
OnSubAddClickLinck onSubAddClickLinck;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
tv_number_view.setText(number+"");
}
public MyAddSubView(Context context) {
super(context);
}
public MyAddSubView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View inflate = View.inflate(context, R.layout.add_sub_view, this);
tv_sub_view = inflate.findViewById(R.id.tv_sub_view);
tv_number_view = inflate.findViewById(R.id.tv_number_view);
tv_add_view = inflate.findViewById(R.id.tv_add_view);
tv_add_view.setOnClickListener(this);
tv_sub_view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_add_view:
++number;
tv_number_view.setText(number+"");
onSubAddClickLinck.onSubAddClickLinck(number);
break;
case R.id.tv_sub_view:
if (number>1){
--number;
tv_number_view.setText(number+"");
onSubAddClickLinck.onSubAddClickLinck(number);
}else{
Toast.makeText(getContext(), "不能再少啦!", Toast.LENGTH_SHORT).show();
}
break;
}
}
public interface OnSubAddClickLinck{
void onSubAddClickLinck(int number);
}
public void setOnSubAddClickLinck(OnSubAddClickLinck onSubAddClickLinck){
this.onSubAddClickLinck = onSubAddClickLinck;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:background="#000"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:id="@+id/tv_sub_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="-"
android:textColor="#000" />
<TextView
android:id="@+id/tv_number_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="1"
android:textColor="#000" />
<TextView
android:id="@+id/tv_add_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:gravity="center"
android:text="+"
android:textColor="#000" />
</LinearLayout>
购物车1
猜你喜欢
转载自blog.csdn.net/chy521xin/article/details/82783232
今日推荐
周排行