1.mainactivity里面的
package com.example.lianxi1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.lianxi1.adapter.Myadapter;
import com.example.lianxi1.bean.Bean;
import com.example.lianxi1.mvp.IPresenter;
import com.example.lianxi1.mvp.IView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IView.View {
private RecyclerView recyclerview1;
private IPresenter presenter;
private ImageView check_all;
private boolean check = false;
private TextView text_sum;
private List<Bean.DataBean> list=new ArrayList<>();
private Myadapter adapter;
private double total=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview1 = findViewById(R.id.recyclerview1);
presenter = new IPresenter();
presenter.attData(this);
check_all = findViewById(R.id.check_all);
text_sum = findViewById(R.id.sum);
presenter.infoData("http://www.zhaoapi.cn/product/getCarts?uid=71");
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview1.setLayoutManager(layoutManager);
check_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (check){
total=0;
check_all.setImageResource(R.drawable.check_no);
check=false;
//商家的集合
for (int i = 0; i < list.size(); i++) {
//商品的集合
List<Bean.DataBean.ListBean> list1 = MainActivity.this.list.get(i).getList();
for (int j = 0; j < list1.size(); j++) {
list1.get(j).setCheck(false);
}
}
text_sum.setText("合计:¥"+total);
}else {
total=0;
//商家的集合
for (int i = 0; i < list.size(); i++) {
//商品的集合
List<Bean.DataBean.ListBean> list1 = MainActivity.this.list.get(i).getList();
for (int j = 0; j < list1.size(); j++) {
list1.get(j).setCheck(true);
total= total +list1.get(j).getPrice();
}
}
check_all.setImageResource(R.drawable.check_yes);
check=true;
text_sum.setText("合计:¥"+total);
}
adapter.notifyDataSetChanged();
}
});
}
@Override
public void showData(final List<Bean.DataBean> msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
list= msg;
adapter = new Myadapter(msg, MainActivity.this);
recyclerview1.setAdapter(adapter);
selectShop();
}
});
}
private void selectShop() {
adapter.setListener(new Myadapter.ItemClickListener() {
@Override
public void onItemClick(List<Bean.DataBean> list) {
total=0;
int num=0;
int numi=0;
for (int i = 0; i < list.size(); i++) {
//商品的集合
List<Bean.DataBean.ListBean> list1 = MainActivity.this.list.get(i).getList();
for (int j = 0; j < list1.size(); j++) {
num++;
if (list1.get(j).isCheck()){
total= total+list1.get(j).getPrice();
numi++;
}
}
}
text_sum.setText("合计:¥"+total);
adapter.notifyDataSetChanged();
if (num!=numi){
check_all.setImageResource(R.drawable.check_no);
check=true;
}else {
check_all.setImageResource(R.drawable.check_yes);
check=false;
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.deleteData(this);
}
}
2.view
package com.example.lianxi1.mvp;
import com.example.lianxi1.bean.Bean;
import java.util.List;
public interface IView {
interface View{
void showData(List<Bean.DataBean> msg);
}
interface Presenter<Iview>{
void attData(Iview iview);
void deleteData(Iview iview);
void infoData(String url);
}
interface Model{
interface oncallback{
void stringmsg(List<Bean.DataBean> msg);
}
void requestData(String url,oncallback oncallback);
}
}
3.presenter
package com.example.lianxi1.mvp;
import com.example.lianxi1.bean.Bean;
import java.lang.ref.WeakReference;
import java.util.List;
public class IPresenter implements IView.Presenter<IView.View>{
IView.View view;
IModel model;
private WeakReference vweakReference;
private WeakReference mweakReference;
@Override
public void attData(IView.View view) {
this.view=view;
model=new IModel();
vweakReference = new WeakReference(view);
mweakReference = new WeakReference(model);
}
@Override
public void deleteData(IView.View view) {
vweakReference.clear();
mweakReference.clear();
}
@Override
public void infoData(String url) {
model.requestData(url, new IView.Model.oncallback() {
@Override
public void stringmsg(List<Bean.DataBean> msg) {
view.showData(msg);
}
});
}
}
4.model
package com.example.lianxi1.mvp;
import com.example.lianxi1.bean.Bean;
import com.example.lianxi1.ui.OkHttpUtlis;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class IModel implements IView.Model{
@Override
public void requestData(String url, final oncallback oncallback) {
OkHttpUtlis.getinstan().get(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Gson gson=new Gson();
Bean bean = gson.fromJson(response.body().string(), Bean.class);
List<Bean.DataBean> list = bean.getData();
oncallback.stringmsg(list);
}
});
}
}
5.adapter
package com.example.lianxi1.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.lianxi1.R;
import com.example.lianxi1.bean.Bean;
import java.util.List;
public class Myadapter extends RecyclerView.Adapter<Myadapter.ViewHodel> {
private List<Bean.DataBean> list;
private Context context;
private Myadapter1 myadapter1;
public Myadapter(List<Bean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public Myadapter.ViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.itm, parent, false);
ViewHodel hodel=new ViewHodel(inflate);
return hodel;
}
@Override
public void onBindViewHolder(@NonNull Myadapter.ViewHodel holder, int position) {
holder.text_name1.setText(list.get(position).getSellerName());
StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
holder.recyclerview2.setLayoutManager(layoutManager);
this.myadapter1 = new Myadapter1(list.get(position).getList(), context);
Myadapter1 myadapter1= this.myadapter1;
holder.recyclerview2.setAdapter(myadapter1);
myadapter1.setListener(new Myadapter1.ItemClickListener() {
@Override
public void onItemClick() {
listener.onItemClick(list);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHodel extends RecyclerView.ViewHolder{
private RecyclerView recyclerview2;
private TextView text_name1;
public ViewHodel(View itemView) {
super(itemView);
text_name1 = itemView.findViewById(R.id.text_name1);
recyclerview2 = itemView.findViewById(R.id.recyclerview2);
}
}
private ItemClickListener listener;
public void setListener(ItemClickListener listener) {
this.listener = listener;
}
public interface ItemClickListener{
void onItemClick(List<Bean.DataBean> list);
}
}
6.adapter1
package com.example.lianxi1.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.lianxi1.R;
import com.example.lianxi1.bean.Bean;
import com.squareup.picasso.Picasso;
import java.util.List;
public class Myadapter1 extends RecyclerView.Adapter<Myadapter1.ViewHodel> {
private List<Bean.DataBean.ListBean> list;
private Context context;
public Myadapter1(List<Bean.DataBean.ListBean> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public Myadapter1.ViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(context).inflate(R.layout.itm2, parent, false);
ViewHodel viewHodel=new ViewHodel(inflate);
return viewHodel;
}
@Override
public void onBindViewHolder(@NonNull final Myadapter1.ViewHodel holder, final int position) {
holder.text_name2.setText(list.get(position).getTitle());
holder.text_name3.setText(list.get(position).getPrice()+"");
String images = list.get(position).getImages();
String[] split = images.split("\\|");
Picasso.with(context).load(split[0]).into(holder.ima_name);
if (list.get(position).isCheck()){
holder.img_check.setImageResource(R.drawable.check_yes);
}else {
holder.img_check.setImageResource(R.drawable.check_no);
}
holder.img_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (list.get(position).isCheck()){
list.get(position).setCheck(false);
//holder.img_check.setImageResource(R.drawable.check_no);
}else {
list.get(position).setCheck(true);
//holder.img_check.setImageResource(R.drawable.check_yes);
}
listener.onItemClick();
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHodel extends RecyclerView.ViewHolder{
private ImageView img_check;
private ImageView ima_name;
private TextView text_name2;
private TextView text_name3;
public ViewHodel(View itemView) {
super(itemView);
ima_name = itemView.findViewById(R.id.ima_name);
text_name2 = itemView.findViewById(R.id.text_name2);
text_name3 = itemView.findViewById(R.id.text_name3);
img_check = itemView.findViewById(R.id.img_check);
}
}
private ItemClickListener listener;
public void setListener(ItemClickListener listener) {
this.listener = listener;
}
public interface ItemClickListener{
void onItemClick();
}
}
7.布局mainactivity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="@+id/aa1"
android:background="#11111111"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:text="购物车"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="编辑"
android:textSize="20sp" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_below="@+id/aa1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimaryDark">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/check_no"
android:layout_centerVertical="true"
android:id="@+id/check_all"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/check_all"
android:layout_centerVertical="true"
android:text="全选" />
<TextView
android:id="@+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:textSize="20sp"
android:text="合计:¥1500.00" />
<TextView
android:background="#f00"
android:text="去结算"
android:layout_alignParentRight="true"
android:layout_width="100dp"
android:gravity="center"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
8.itm1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_name1"
android:text="xxx"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
9.itm2
<?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">
<RelativeLayout
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/check_no"
android:id="@+id/img_check"
android:layout_centerVertical="true"/>
<ImageView
android:src="@color/colorPrimaryDark"
android:id="@+id/ima_name"
android:layout_toRightOf="@+id/img_check"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/text_name2"
android:layout_toRightOf="@+id/ima_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="xxxx" />
<TextView
android:id="@+id/text_name3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_name2"
android:layout_toRightOf="@+id/ima_name"
android:text="sasasaassaa" />
</RelativeLayout>
</RelativeLayout>