淘宝购物
淘宝内部的'淘金币'、'淘宝折上折'等优惠券能有效降低购物成本 #生活技巧# #节省生活成本# #购物优惠技巧# #比价购物软件#
1.购物车主页面
public class ShopFragmentFragment extends BaseFragment<ShopFragmentPresenter> implements ShopFragmentContract.View {
Unbinder unbinder;
@BindView(R.id.rece_shop)
RecyclerView receShop;
@BindView(R.id.check_all)
CheckBox checkAll;
@BindView(R.id.text_zongjia)
TextView textZongjia;
@BindView(R.id.btn_tijiao)
Button btnTijiao;
private News4 news4;
private MyWaiAdapter myWaiAdapter;
private int b;
public static ShopFragmentFragment newInstance() {
ShopFragmentFragment fragment = new ShopFragmentFragment();
return fragment;
}
@Override
public void setupFragmentComponent(@NonNull AppComponent appComponent) {
DaggerShopFragmentComponent //如找不到该类,请编译一下项目
.builder()
.appComponent(appComponent)
.shopFragmentModule(new ShopFragmentModule(this))
.build()
.inject(this);
}
@Override
public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_shop, container, false);
}
@Override
public void initData(@Nullable Bundle savedInstanceState) {
mPresenter.shoping();
}
@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);
//注册eventbus
// EventBus.getDefault().register(getActivity());
mPresenter.shoping();
return rootView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void showDatashop(String json) {
Gson gson = new Gson();
news4 = gson.fromJson(json, News4.class);
List<News4.DataBean> data = news4.getData();
//设置外层条目
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
myWaiAdapter = new MyWaiAdapter(R.layout.wai_layout, data);
receShop.setAdapter(myWaiAdapter);
receShop.setLayoutManager(manager);
//对适配回调的内容进行监听
myWaiAdapter.setOnCheckedChangeLisenter(new MyWaiAdapter.OnCheckedChangeLisenter() {
@Override
public void onChecked(int layoutPosition, boolean isChecked) {
//全选条目的标识
boolean isAllChecked = true;
//外层条目的判断
for (int i = 0; i < news4.getData().size(); i++) {
boolean checked = news4.getData().get(i).getChecked();
//内层条目的判断
for (int j = 0; j < news4.getData().get(i).getList().size(); j++) {
boolean innerChecked = news4.getData().get(i).getList().get(j).getInnerChecked();
isAllChecked = (isAllChecked & checked & innerChecked);
}
}
checkAll.setChecked(isAllChecked);
//外层条目
//outterBean.getData().get(layoutPosition).setOutterChecked(isChecked);
//刷新适配器
myWaiAdapter.notifyDataSetChanged();
}
public void onOutterIntemChecked(int layoutPosition, boolean checked) {
news4.getData().get(layoutPosition).setChecked(checked);
//全选条目的标识
boolean isAllChecked = true;
//外层条目的判断
for (int i = 0; i < news4.getData().size(); i++) {
boolean checked1 = news4.getData().get(i).getChecked();
//内层条目的判断
for (int j = 0; j < news4.getData().get(i).getList().size(); j++) {
boolean innerChecked = news4.getData().get(i).getList().get(j).getInnerChecked();
isAllChecked = (isAllChecked & checked1 & innerChecked);
}
}
checkAll.setChecked(isAllChecked);
//外层条目
//outterBean.getData().get(layoutPosition).setOutterChecked(isChecked);
//刷新适配器
myWaiAdapter.notifyDataSetChanged();
}
});
}
@Override
public void error(String s) {
}
@OnClick(R.id.check_all)
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.check_all:
if (checkAll.isChecked()) {
//如果全选被选中,最外层的是否选中的状态手动更改为true,刷新适配器
for (int i = 0; i < news4.getData().size(); i++) {
news4.getData().get(i).setChecked(true);
for (int j = 0; j < news4.getData().get(i).getList().size(); j++) {
news4.getData().get(i).getList().get(j).setInnerChecked(true);
}
}
} else {
//如果全选未被选中,最外层的是否选中的状态手动更改为true,刷新适配器
for (int i = 0; i < news4.getData().size(); i++) {
news4.getData().get(i).setChecked(false);
for (int j = 0; j < news4.getData().get(i).getList().size(); j++) {
news4.getData().get(i).getList().get(j).setInnerChecked(false);
}
}
}
initview();
myWaiAdapter.notifyDataSetChanged();
break;
}
}
//总价
private void initview() {
int zong = 0;
for (int i = 0; i < news4.getData().size(); i++) {
for (int j = 0; j < news4.getData().get(i).getList().size(); j++) {
if (news4.getData().get(i).getList().get(j).getInnerChecked()) {
zong += news4.getData().get(i).getList().get(j).getPrice() * news4.getData().get(i).getList().get(j).getNum();
}
}
}
textZongjia.setText("总价是:" + zong);
myWaiAdapter.notifyDataSetChanged();
}
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onEvent(User user) {/* Do something */
initview();
// Toast.makeText(getActivity(), "aaa", Toast.LENGTH_SHORT).show();
}
;
@OnClick(R.id.btn_tijiao)
public void onViewClicked() {
Intent intent = new Intent(getActivity(), DingdanActivity.class);
startActivity(intent);
}
}
2.bean类1
public static class DataBean {
/**
* list : [{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":5,"pid":57,"price":5199,"pscid":40,"selected":1,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}]
* sellerName : 商家1
* sellerid : 1
*/
private String sellerName;
private String sellerid;
private List<ListBean> list;
private boolean isChecked;
public boolean getChecked()
{
return isChecked;
}
public void setChecked(boolean isChecked)
{
this.isChecked = isChecked;
}
bean类2
public static class ListBean {
/**
* bargainPrice : 11800.0
* createtime : 2017-10-10T17:33:37
* detailUrl : https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends
* images : https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg
* num : 5
* pid : 57
* price : 5199.0
* pscid : 40
* selected : 1
* sellerid : 1
* subhead : 【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统
* title : 小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银
*/
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private double price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
private boolean innerChecked;
public boolean getInnerChecked()
{
return innerChecked;
}
public void setInnerChecked(boolean innerChecked)
{
this.innerChecked = innerChecked;
}
3.购物车底部布局
<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">
<android.support.v7.widget.RecyclerView
android:id="@+id/rece_shop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity=""
android:gravity="center_vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="全选"
android:id="@+id/check_all"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="总价是:¥"
android:textSize="20sp"
android:textColor="#f00"
android:layout_marginLeft="80dp"
android:layout_marginTop="10dp"
android:id="@+id/text_zongjia"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="去结算"
android:textColor="#ffffff"
android:background="#f00"
android:textSize="20sp"
android:id="@+id/btn_tijiao"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</LinearLayout>
4.购物车外部适配器
public class MyWaiAdapter extends BaseQuickAdapter<News4.DataBean,BaseViewHolder> {
private int layoutPosition;
private List<News4.DataBean.ListBean> listBeans;
private MyShopingAdapter myShopingAdapter;
public interface OnCheckedChangeLisenter {
void onChecked(int layoutPosition, boolean isChecked);
}
public void setOnCheckedChangeLisenter(OnCheckedChangeLisenter onCheckedChangeLisenter) {
this.onCheckedChangeLisenter = onCheckedChangeLisenter;
}
private RecyclerView rece_waishop;
OnCheckedChangeLisenter onCheckedChangeLisenter;
public MyWaiAdapter(int layoutResId, @Nullable List<News4.DataBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, News4.DataBean item) {
//当前位置
layoutPosition = helper.getLayoutPosition();
//对外层的条目商家信息内容
helper.setText(R.id.check_waishop,item.getSellerName());
//对外层的勾选状态
CheckBox check_waishop = helper.getView(R.id.check_waishop);
//外层被点击之后及时通过接口回调的形式通知全选/反选按钮
check_waishop.setOnCheckedChangeListener(null);
check_waishop.setChecked(item.getChecked());
check_waishop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = check_waishop.isChecked();
item.setChecked(checked);
if(checked)
{
for (int i = 0; i < item.getList().size(); i++) {
item.getList().get(i).setInnerChecked(true);
}
}
else
{
for (int i = 0; i < item.getList().size(); i++) {
item.getList().get(i).setInnerChecked(false);
}
}
}
});
/*check_waishop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.setChecked(isChecked);
if(isChecked)
{
for (int i = 0; i < item.getList().size(); i++) {
item.getList().get(i).setInnerChecked(true);
}
}
else
{
for (int i = 0; i < item.getList().size(); i++) {
item.getList().get(i).setInnerChecked(false);
}
}
onCheckedChangeLisenter.onChecked(helper.getLayoutPosition(),isChecked);
}
});*/
//外层条目的适配器中需获取内层的RecyclerView
rece_waishop = helper.getView(R.id.rece_waishop);
//内层的数据源item.getList();
listBeans = item.getList();
//需要将数据源设置给适配器
myShopingAdapter = new MyShopingAdapter(R.layout.shoping_layout, listBeans);
LinearLayoutManager manager = new LinearLayoutManager(App.getInstance(), LinearLayoutManager.VERTICAL, false);
rece_waishop.setLayoutManager(manager);
rece_waishop.setAdapter(myShopingAdapter);
myShopingAdapter.setOnCheckedChangeLisenter(new MyShopingAdapter.OnCheckedChangeLisenter() {
@Override
public void onChecked(int LayoutPosition, boolean isChecked) {
boolean isAllInnerChecked = true;
for (int i = 0; i < item.getList().size(); i++) {
boolean innerChecked = item.getList().get(i).getInnerChecked();
isAllInnerChecked = (isAllInnerChecked & innerChecked);
}
check_waishop.setChecked(isAllInnerChecked);
//外层条目
item.setChecked(isAllInnerChecked);
//首先需要更改bean对象的状态,把具体的位置进行回传
//然后通过接口回到通知调用者,状态发送改变,及时对全选/反选按钮做更新处理
onCheckedChangeLisenter.onChecked(layoutPosition,isChecked);
}
});
}
}
5.外部适配器布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商家1"
android:id="@+id/check_waishop"
/>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rece_waishop"
/>
</LinearLayout>
6.内部适配器
public class MyShopingAdapter extends BaseQuickAdapter<News4.DataBean.ListBean,BaseViewHolder> {
private int shangpinnumber;
public MyShopingAdapter(int layoutResId, @Nullable List<News4.DataBean.ListBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, News4.DataBean.ListBean item) {
helper.setText(R.id.text_biaoti, item.getTitle());
helper.setText(R.id.text_shopprice,"总价是:"+item.getPrice());
SimpleDraweeView iv_showIcon = helper.getView(R.id.simp_viewshop);
//根据|进行切割
String images = item.getImages();
String[] splitImages = images.split("\\|");
//Fresco加载图片
Uri uri = Uri.parse(splitImages[0]);
iv_showIcon.setImageURI(uri);
//从Bean对象去取出勾选状态
Log.e("getInnerChecked", "item.getInnerChecked():" + item.getInnerChecked());
helper.setChecked(R.id.check_shang, item.getInnerChecked());
CheckBox check_shang = helper.getView(R.id.check_shang);
check_shang.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//内层的状态更改完
item.setInnerChecked(check_shang.isChecked());
//通过接口回调,把状态回显给外层
onCheckedChangeLisenter.onChecked(helper.getLayoutPosition(),check_shang.isChecked());
}
});
Jiajian jia_jiajian = helper.getView(R.id.jia_jiajian);
jia_jiajian.setOnNumCallBack(new Jiajian.onNumCallBack() {
@Override
public void onShowNum(int number) {
shangpinnumber = number;
Toast.makeText(mContext, "number"+number, Toast.LENGTH_SHORT).show();
}
@Override
public void add() {
item.setNum(item.getNum()+1);
User user = new User();
//todo eventbus发送粘性事件
EventBus.getDefault().postSticky(user);
}
@Override
public void minus() {
item.setNum(item.getNum()-1);
User user = new User();
EventBus.getDefault().postSticky(user);
}
});
// helper.setText(R.id.jia_jiajian,item.getNum()+"");
Jiajian jiajianView = helper.getView(R.id.jia_jiajian);
TextView text_number = jiajianView.findViewById(R.id.text_number);
text_number.setText(item.getNum()+"");
}
public interface OnCheckedChangeLisenter {
void onChecked(int layoutPosition, boolean isChecked);
}
public void setOnCheckedChangeLisenter(OnCheckedChangeLisenter onCheckedChangeLisenter) {
this.onCheckedChangeLisenter = onCheckedChangeLisenter;
}
OnCheckedChangeLisenter onCheckedChangeLisenter;
}
7.内部适配器布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_height="70dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check_shang"
/>
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="70dp"
android:layout_height="70dp"
app:placeholderImage="@mipmap/ic_launcher"
android:id="@+id/simp_viewshop"
android:layout_marginLeft="40dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="展示数据"
android:id="@+id/text_biaoti"
android:layout_marginLeft="125dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="价格:"
android:textColor="#f00"
android:layout_marginLeft="125dp"
android:layout_marginTop="45dp"
android:id="@+id/text_shopprice"
/>
<jingd.bwie.com.jdong.mvp.ui.Jiajian
android:id="@+id/jia_jiajian"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="13dp"
android:layout_marginTop="22dp"
android:gravity="center_vertical"
/>
</RelativeLayout>
7.加减器页面
public class Jiajian extends LinearLayout implements View.OnClickListener {
private Button btn_jian;
private Button btn_jia;
private TextView text_number;
public Jiajian(Context context) {
super(context);
}
public Jiajian(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
LayoutInflater.from(context).inflate(R.layout.jiajian_layout,this);
btn_jian = (Button)findViewById(R.id.btn_jian);
btn_jia = (Button)findViewById(R.id.btn_jia);
text_number = (TextView)findViewById(R.id.text_number);
btn_jia.setOnClickListener(this);
btn_jian.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String s = text_number.getText().toString();
int number = Integer.parseInt(s);
switch (v.getId())
{
case R.id.btn_jia:
number+=1;
text_number.setText(String.valueOf(number));
if(onNumCallBack!=null)
{
onNumCallBack.add();
}
break;
case R.id.btn_jian:
if(number<1)
{
Toast.makeText(getContext(), "最小不能小于1", Toast.LENGTH_SHORT).show();
return;
}
number-=1;
text_number.setText(String.valueOf(number));
onNumCallBack.onShowNum(number);
if(onNumCallBack!=null)
{
onNumCallBack.minus();
}
break;
}
}
//接口回调
public interface onNumCallBack{
void onShowNum(int number);
void add();
void minus();
}
onNumCallBack onNumCallBack;
public void setOnNumCallBack(onNumCallBack onNumCallBack){
this.onNumCallBack=onNumCallBack;
}
}
网址:淘宝购物 https://www.yuejiaxmz.com/news/view/526212
相关内容
淘宝好购物助手淘宝购物助手下载
十大淘宝购物省钱法宝
淘宝购物攻略,淘宝购物攻略:从入门到精通,轻松成为网购达人!
淘宝网购物如何省钱
实用的淘宝购物技巧
淘宝购物省钱6大秘笈
淘宝购物如何才会省钱?
淘宝达人怎么挑品质好货?淘宝达人购物技巧
淘宝购物秘籍:轻松变身网购达人