//布局
<RelativeLayout 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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_shou"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/dp_90"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="@dimen/dp_43"
android:background="#fff"
>
<CheckBox
android:text="全选"
android:textSize="@dimen/sp_12"
android:id="@+id/checkbox_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
<TextView
android:id="@+id/countPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/sp_12"
android:text="合计:"
android:textColor="#f00"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp_100"
/>
<Button
android:id="@+id/btn_totle"
android:layout_width="@dimen/dp_118"
android:layout_height="@dimen/dp_50"
android:text="去结算"
android:textSize="@dimen/sp_16"
android:textColor="#fff"
android:background="#f00"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</RelativeLayout>
.//Activity
public class Frag_03 extends Fragment implements IQueryContrace.IQueryView,View.OnClickListener {
@BindView(R.id.recycler_shou)
RecyclerView recyclerShou;
@BindView(R.id.checkbox_all)
CheckBox checkboxAll;
@BindView(R.id.countPrice)
TextView countPrice;
@BindView(R.id.btn_totle)
Button btnTotle;
Unbinder unbinder;
private IQueryPresenterInfo iQueryPresenterInfo;
private QueryAdapter queryAdapter;
private List<QueryBean.ResultBean> result;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_03, container, false);
iQueryPresenterInfo = new IQueryPresenterInfo();
iQueryPresenterInfo.attach(this);
iQueryPresenterInfo.requestData();
/*//刷新
RefreshLayout refreshLayout = (RefreshLayout) view.findViewById(R.id.refreshLayout);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
iQueryPresenterInfo.requestData();
refreshlayout.finishRefresh(2000*//*,false*//*);//传入false表示刷新失败
}
});
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(RefreshLayout refreshlayout) {
refreshlayout.finishLoadMore(2000*//*,false*//*);//传入false表示加载失败
}
});*/
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void queryData(QueryBean queryBean) {
checkboxAll.setOnCheckedChangeListener(null);
checkboxAll.setOnClickListener(this);
result = queryBean.getResult();
queryAdapter = new QueryAdapter(R.layout.item_goods, result);
recyclerShou.setAdapter(queryAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerShou.setLayoutManager(linearLayoutManager);
queryAdapter.notifyDataSetChanged();
Log.e("购物车数据","queryBean===================================="+queryBean.getMessage());
//全选反选
queryAdapter.setOnGoodItemClickListenerClick(new QueryAdapter.OnGoodItemClickListenerClick() {
@Override
public void CallBack() {
boolean resultt=true;
for (int i = 0; i < result.size(); i++) {
boolean goodListChecked = result.get(i).getGoodListChecked();
resultt=resultt&goodListChecked;
}
checkboxAll.setChecked(resultt);
//计算总价
totalCount();
}
});
}
//计算总价
private void totalCount() {
double countData = 0;
for (int i = 0; i <result.size(); i++) {
if (result.get(i).getGoodListChecked()){
double price = result.get(i).getPrice();
int count = result.get(i).getCount();
double totel = price * count;
countData=countData+totel;
}
}
countPrice.setText("合计:"+countData);
}
@Override
public void onClick(View v) {
for (int i = 0; i <result.size(); i++) {
result.get(i).setGoodListChecked(checkboxAll.isChecked());
}
totalCount();
queryAdapter.notifyDataSetChanged();
}
@Override
public void onResume() {
super.onResume();
iQueryPresenterInfo = new IQueryPresenterInfo();
iQueryPresenterInfo.attach(this);
iQueryPresenterInfo.requestData();
}
}
public class QueryAdapter extends BaseQuickAdapter<QueryBean.ResultBean,BaseViewHolder> {
//创建接口
OnGoodItemClickListenerClick onGoodItemClickListenerClick;
public void setOnGoodItemClickListenerClick(OnGoodItemClickListenerClick onGoodItemClickListenerClick) {
this.onGoodItemClickListenerClick = onGoodItemClickListenerClick;
}
public interface OnGoodItemClickListenerClick{
public void CallBack();
}
public QueryAdapter(int layoutResId, @Nullable List<QueryBean.ResultBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, final QueryBean.ResultBean item) {
helper.setText(R.id.text_goodsName,item.getCommodityName());
helper.setText(R.id.text_goodsPrice,"¥"+item.getPrice());
//避免焦点抢占
final CheckBox checkbox_goods = helper.getView(R.id.checkbox_goods);
checkbox_goods.setOnCheckedChangeListener(null);
checkbox_goods.setChecked(item.getGoodListChecked());
ImageView imageView = helper.getView(R.id.image_goods);
Glide.with(mContext).load(item.getPic()).into(imageView);
//子条目
checkbox_goods.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item.setGoodListChecked(checkbox_goods.isChecked());
onGoodItemClickListenerClick.CallBack();
}
});
//获取自定义view
GoodsView goods_view = helper.getView(R.id.goods_view);
goods_view.setNum(item.getCount());
goods_view.setOnNumberItemClickListener(new GoodsView.OnNumberItemClickListener() {
@Override
public void jian(int number) {
item.setCount(number);
onGoodItemClickListenerClick.CallBack();
}
@Override
public void jia(int number) {
item.setCount(number);
onGoodItemClickListenerClick.CallBack();
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_40"
android:background="#fff"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_40"
>
</RelativeLayout>
<CheckBox
android:id="@+id/checkbox_goods"
android:layout_width="@dimen/dp_20"
android:layout_height="@dimen/dp_20"
android:layout_marginLeft="5dp"
android:layout_marginTop="@dimen/dp_10"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="@dimen/dp_30"
android:layout_marginTop="@dimen/dp_10"
android:layout_marginBottom="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_10"
>
<ImageView
android:id="@+id/image_goods"
android:layout_width="@dimen/dp_80"
android:layout_height="@dimen/dp_80"
android:layout_marginTop="@dimen/dp_8"
android:layout_marginLeft="@dimen/dp_8"
android:layout_marginBottom="@dimen/dp_8"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginTop="@dimen/dp_8">
<TextView
android:id="@+id/text_goodsName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="30"
android:singleLine="true"
android:text="名称"
android:textSize="@dimen/sp_12" />
<TextView
android:id="@+id/text_goodsPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="@dimen/dp_64"
android:text="价格"
android:textColor="#f00"
android:textSize="@dimen/sp_14" />
<com.bawie.liu.asuper.data.GoodsView
android:id="@+id/goods_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="@dimen/dp_64"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp">
</com.bawie.liu.asuper.data.GoodsView>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
//契约类
public class IQueryContrace {
//v层
public interface IQueryView{
public void queryData(QueryBean queryBean);
};
//p层
public interface IQueryPresenter<IQueryView>{
//绑定
public void attach(IQueryView iQueryView);
//解绑
public void deach(IQueryView iQueryView);
//传值
public void requestData();
}
//m层
public interface IQueryModel{
public void containQueryModel(CallBack callBack);
//接口
public interface CallBack{
public void responseData(QueryBean queryBean);
}
}
}
//p
public class IQueryPresenterInfo implements IQueryContrace.IQueryPresenter<IQueryContrace.IQueryView> {
IQueryContrace.IQueryView iQueryView;
private SoftReference<IQueryContrace.IQueryModel> iQueryViewSoftReference;
private IQueryContrace.IQueryModel iQueryModel;
@Override
public void attach(IQueryContrace.IQueryView iQueryView) {
this.iQueryView=iQueryView;
iQueryModel = new IQueryModel();
iQueryViewSoftReference = new SoftReference<>(iQueryModel);
}
@Override
public void deach(IQueryContrace.IQueryView iQueryView) {
iQueryViewSoftReference.clear();
}
@Override
public void requestData() {
iQueryModel.containQueryModel(new IQueryContrace.IQueryModel.CallBack() {
@Override
public void responseData(QueryBean queryBean) {
iQueryView.queryData(queryBean);
}
});
}
}
//m
public class IQueryModel implements IQueryContrace.IQueryModel {
@Override
public void containQueryModel(final CallBack callBack) {
UserTableDao userTableDao = App.getDaoSession().getUserTableDao();
List<UserTable> userTables = userTableDao.loadAll();
UserTable userTable = userTables.get(userTables.size() - 1);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.url)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
ApiService service = retrofit.create(ApiService.class);
Observable<QueryBean> queryData = service.getQueryData(userTable.getUserId(), userTable.getSessionId());
queryData.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<QueryBean>() {
@Override
public void accept(QueryBean queryBean) throws Exception {
Log.i("SelShopCar", "添加购物车之前查询购物车成功: "+queryBean.getMessage());
callBack.responseData(queryBean);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.i("SelShopCar", "添加购物车之前查询购物车失败: "+throwable.getMessage());
}
});
}
}
//自定义view
public class GoodsView extends LinearLayout implements View.OnClickListener{
TextView btn_jian,btn_add;
TextView tv_number;
public GoodsView(Context context) {
super(context);
}
public GoodsView(Context context, AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.item_goodsview, this);
btn_jian = view.findViewById(R.id.btn_jian);
tv_number = view.findViewById(R.id.tv_number);
btn_add = view.findViewById(R.id.btn_add);
btn_jian.setOnClickListener(this);
btn_add.setOnClickListener(this);
}
public GoodsView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onClick(View v) {
String numberString = tv_number.getText().toString();
int number = Integer.parseInt(numberString);
switch (v.getId()){
case R.id.btn_jian:
number--;
if (number<0){
number=0;
tv_number.setText(String.valueOf(number));
}
tv_number.setText(String.valueOf(number));
onNumberItemClickListener.jian(number);
break;
case R.id.btn_add:
number++;
tv_number.setText(String.valueOf(number));
onNumberItemClickListener.jia(number);
break;
}
}
//创建接口
OnNumberItemClickListener onNumberItemClickListener;
public void setOnNumberItemClickListener(OnNumberItemClickListener onNumberItemClickListener) {
this.onNumberItemClickListener = onNumberItemClickListener;
}
public void setNum(int count) {
tv_number.setText(count+"");
}
public interface OnNumberItemClickListener{
public void jian(int number);
public void jia(int number);
}
}
//view布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_20"
>
<TextView
android:text="+"
android:id="@+id/btn_add"
android:layout_width="20dp"
android:layout_height="20dp"
/>
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:inputType="number"
android:text="0"
android:layout_marginRight="@dimen/dp_3"
android:layout_marginLeft="@dimen/dp_3"
android:layout_gravity="center"
/>
<TextView
android:text="-"
android:id="@+id/btn_jian"
android:layout_width="20dp"
android:layout_height="20dp"
/>
</LinearLayout>