mainActivity.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".MainActivity" android:orientation="vertical" > <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rec" /> </android.support.constraint.ConstraintLayout>
商家car.xml
<?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:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/titles" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> </LinearLayout> <android.support.v7.widget.RecyclerView android:id="@+id/adapter_rec" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
商品itemcar.xml
<?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:orientation="horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginLeft="15dp" > <CheckBox android:id="@+id/checkboxs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" /> <ImageView android:id="@+id/imges" android:layout_width="120dp" android:layout_height="120dp" android:src="@mipmap/ic_launcher_round" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="25dp"/> </LinearLayout>
OkHttp封装
package com.bwei.lenovo.jgouwuche.network;
import android.os.Environment;
import android.os.Handler;
import com.google.gson.Gson;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class OkHttp {
//实现一个单例
private static volatile OkHttp instance;
private OkHttpClient client;
private Handler handler = new Handler();
//添加拦截器
private Interceptor getAppInterceptor(){
Interceptor interceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
return response;
}
};
return interceptor;
}
//添加拦截器
private OkHttp(){
File file = new File(Environment.getExternalStorageDirectory(), "cache1");
client = new OkHttpClient.Builder()
.readTimeout(3000,TimeUnit.SECONDS)
.connectTimeout(3000,TimeUnit.SECONDS)
.addInterceptor(getAppInterceptor())
.cache(new Cache(file,10*1024))
.build();
}
//创建单例
public static OkHttp getInstance(){
if (instance == null){
synchronized (OkHttp.class){
if (null == instance){
instance = new OkHttp();
}
}
}
return instance;
}
public void doPost(String url, Map<String,String> params, final Class clazz, final NetCallBack netCallBack)
{
FormBody.Builder body = new FormBody.Builder();
for(String key :params .keySet())
{
body.add(key,params.get(key));
}
Request request = new Request.Builder()
.url(url)
.post(body.build())
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().toString();
Gson gson = new Gson();
final Object o = gson.fromJson(result, clazz);
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.LoadSuccess(o);
}
});
}
});
}
//封装doget方法
public void doGet(String url, final Class clazz, final NetCallBack netCallBack)
{
// 2. 创建一个请求对象
Request request = new Request.Builder()
.get()
.url(url)
.build();
//创建call对象
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
final Object o = gson.fromJson(string, clazz);
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.LoadSuccess(o);
}
});
}
});
}
public interface NetCallBack{
void LoadSuccess(Object obj);
void LoadFail();
}
}
购物车molder层的类
package com.bwei.lenovo.mgouwuche.model; import com.bwei.lenovo.mgouwuche.bean.CarBean; import com.bwei.lenovo.mgouwuche.http.OkHttp; public class CarModel implements ICarModel { @Override public void getICarModel(String url, final GetCallback getCallback) { OkHttp.getInstance().doGet(url, CarBean.class, new OkHttp.NetCallBack() { @Override public void Success(Object object) { getCallback.Saccess(object); } @Override public void Faile() { } }); } }
购物车molder层接口
package com.bwei.lenovo.mgouwuche.model; public interface ICarModel { public void getICarModel(String url,GetCallback getCallback); interface GetCallback{ void Saccess(Object o); void Failts(); } }
购物车presenter层类
package com.bwei.lenovo.mgouwuche.presenter; import com.bwei.lenovo.mgouwuche.MainActivity; import com.bwei.lenovo.mgouwuche.api.Api; import com.bwei.lenovo.mgouwuche.model.CarModel; import com.bwei.lenovo.mgouwuche.model.ICarModel; public class CarPresenter implements ICarPresenter { MainActivity mainActivity; private final CarModel carModel; public CarPresenter(MainActivity mainActivity) { this.mainActivity = mainActivity; carModel = new CarModel(); } @Override public void getICarPresenter() { carModel.getICarModel(Api.ASC, new ICarModel.GetCallback() { @Override public void Saccess(Object o) { mainActivity.getData(o); } @Override public void Failts() { } }); } }
购物车presenter接口
package com.bwei.lenovo.mgouwuche.presenter; public interface ICarPresenter { public void getICarPresenter(); }
商家层adapter
package com.bwei.lenovo.mgouwuche.adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.bwei.lenovo.mgouwuche.R; import com.bwei.lenovo.mgouwuche.bean.CarBean; import java.util.List; public class CarAdapter extends RecyclerView.Adapter<CarAdapter.ViewHolder> { Context context; CarBean carBean; public CarAdapter(Context context, CarBean carBean) { this.context = context; this.carBean = carBean; } @NonNull @Override public CarAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.car, parent, false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(@NonNull CarAdapter.ViewHolder holder, int position) { String names = carBean.getData().get(position).getSellerName(); holder.name.setText(names); //佈局管理器 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); linearLayoutManager.setOrientation(OrientationHelper.VERTICAL); holder.re.setLayoutManager(linearLayoutManager); List<CarBean.DataBean.ListBean> list = carBean.getData().get(position).getList(); ItemAdapter itemAdapter = new ItemAdapter(context,list); holder.re.setAdapter(itemAdapter); } @Override public int getItemCount() { return carBean.getData().size(); } public class ViewHolder extends RecyclerView.ViewHolder{ TextView name; RecyclerView re; public ViewHolder(View itemView) { super(itemView); name = itemView.findViewById(R.id.titles); re = itemView.findViewById(R.id.adapter_rec); } } /* private ShopCallBackListener mshopCallBackListener; public CarAdapter(ShopCallBackListener mshopCallBackListener) { this.mshopCallBackListener = mshopCallBackListener; } private interface ShopCallBackListener{ void callbck(List<CarBean.DataBean> list); }*/ }
商品层adapter
package com.bwei.lenovo.mgouwuche.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.bwei.lenovo.mgouwuche.R; import com.bwei.lenovo.mgouwuche.bean.CarBean; import java.util.List; public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> { Context context; List<CarBean.DataBean.ListBean> list; public ItemAdapter(Context context, List<CarBean.DataBean.ListBean> list) { this.context = context; this.list = list; } @NonNull @Override public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.item_car, parent, false); ItemViewHolder itemViewHolder = new ItemViewHolder(view); return itemViewHolder; } @Override public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) { holder.name.setText(list.get(position).getTitle()); holder.price.setText(list.get(position).getBargainPrice()); Glide.with(context) .load(list.get(position).getImages().split("\\|")[0].replace("https","http")) .into(holder.imges); } @Override public int getItemCount() { return list.size(); } public class ItemViewHolder extends RecyclerView.ViewHolder { TextView name; TextView price; final ImageView imges; public ItemViewHolder(View itemView) { super(itemView); name = itemView.findViewById(R.id.name); price = itemView.findViewById(R.id.price); imges = itemView.findViewById(R.id.imges); } } }
购物车主页面mainActivity
package com.bwei.lenovo.mgouwuche; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.OrientationHelper; import android.support.v7.widget.RecyclerView; import android.view.View; import com.bwei.lenovo.mgouwuche.adapter.CarAdapter; import com.bwei.lenovo.mgouwuche.bean.CarBean; import com.bwei.lenovo.mgouwuche.presenter.CarPresenter; public class MainActivity extends AppCompatActivity { private RecyclerView ress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ress = findViewById(R.id.rec); //布局 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(OrientationHelper.VERTICAL); ress.setLayoutManager(linearLayoutManager); //presenter CarPresenter carPresenter = new CarPresenter(this); carPresenter.getICarPresenter(); } public void getData(Object o){ CarBean carBean = (CarBean) o; CarAdapter carAdapter = new CarAdapter(MainActivity.this, carBean); ress.setAdapter(carAdapter); } }
购物车接口api
package com.bwei.lenovo.mgouwuche.api; public class Api { public static final String ASC = "http://www.zhaoapi.cn/product/getCarts?uid=71"; }