首先先导入我们要使用的依赖
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.2.5'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.jcodecraeer:xrecyclerview:1.5.9'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.youth.banner:banner:1.4.10'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
其次还要导入权限注意最终没写权限是无法运行出效果的
<uses-permission android:name="com.dingtao.week3.permission.JPUSH_MESSAGE"/>
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
//谨记如果配置application的话要在清单文件中配置
现在开始写布局文件
首先的布局肯定是我们的主页面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/b"
android:layout_width="50dp"
android:layout_height="50dp" />
<ImageView
android:id="@+id/ss"
android:src="@drawable/c"
android:layout_width="50dp"
android:layout_height="50dp" />
<EditText
android:textFontWeight="1"
android:id="@+id/edit"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/qh"
android:src="@drawable/d"
android:layout_width="50dp"
android:layout_height="50dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排序▲"
android:gravity="center"/>
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排序▲"
android:gravity="center"/>
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排序▲"
android:gravity="center"/>
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排序▲"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/shop_car_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/a" />
<com.jcodecraeer.xrecyclerview.XRecyclerView
android:id="@+id/xrecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.jcodecraeer.xrecyclerview.XRecyclerView>
</LinearLayout>
</LinearLayout>
记得要注意自己的布局购物车的image是要跳入购物车的如果你写成相对布局的话会被XRecyclerView覆盖掉(这个要注意)
写接口的bean类
public class Result<T> {
int code;
String msg;
T data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
泛型的bean类
public class Shop {
List<Goods> list;
String sellerName;
String sellerid;
int textColor = 0xffffffff;
int background = R.color.grayblack;
boolean check;
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public int getTextColor() {
return textColor;
}
public void setBackground(int background) {
this.background = background;
}
public int getBackground() {
return background;
}
public void setCheck(boolean check) {
this.check = check;
}
public boolean isCheck() {
return check;
}
public List<Goods> getList() {
return list;
}
public void setList(List<Goods> list) {
this.list = list;
}
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
}
public class Goods implements Serializable {
// "bargainPrice": 111.99,
// "createtime": "2017-10-14T21:39:05",
// "detailUrl": "https:\/\/item.m.jd.com\/product\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends",
// "images": "https:\/\/m.360buyimg.com\/n0\/jfs\/t9004\/210\/1160833155\/647627\/ad6be059\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\/\/m.360buyimg.com\/n0\/jfs\/t7504\/338\/63721388\/491286\/f5957f53\/598e95f1N7f2adb87.jpg!q70.jpg|https:\/\/m.360buyimg.com\/n0\/jfs\/t7441\/10\/64242474\/419246\/adb30a7d\/598e95fbNd989ba0a.jpg!q70.jpg",
// "itemtype": 1,
// "pid": 1,
// "price": 118.0,
// "pscid": 1,
// "salenum": 0,
// "sellerid": 17,
// "subhead": "每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下",
// "title": "北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"
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 int count=1;
public void setCount(int count) {
this.count = count;
}
public int getCount() {
return count;
}
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
写好 bean类就该写mvp了
我们的core层
首先先写成功和失败的借口
public interface DataCall<T> {
void success(T data);
void fail(Result result);
}
用hander来判断失败成功
public abstract class BasePresenter {
DataCall dataCall;
public BasePresenter(DataCall dataCall){
this.dataCall = dataCall;
}
Handler mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
Result result = (Result) msg.obj;
if (result.getCode()==0){
dataCall.success(result.getData());
}else{
dataCall.fail(result);
}
}
};
public void requestData(final Object...args){
new Thread(new Runnable() {
@Override
public void run() {
Message message = mHandler.obtainMessage();
message.obj = getData(args);
mHandler.sendMessage(message);
}
}).start();
}
protected abstract Result getData(Object...args);
public void unBindCall(){
this.dataCall = null;
}
}
SharedPreferences的类
public class DTApplication extends Application {
private static DTApplication instance;
private SharedPreferences mSharedPreferences;
@Override
public void onCreate() {
super.onCreate();
instance = this;
mSharedPreferences = getSharedPreferences("application",
Context.MODE_PRIVATE);
}
public static DTApplication getInstance() {
return instance;
}
public SharedPreferences getShare() {
return mSharedPreferences;
}
}
首先mvp框架的
model层(这个要注意的是接口的毛病,如果接口有问题的话是出不来数据的,我们可以直接传入从接口拿到的数据不通过okhttp直接解析数据显示到列表)
首先写列表的model
public class GoodsListModel {
public static Result goodsList(String keywords, final String page) {
String resultString = HttpUtils.postForm("http://www.zhaoapi.cn/product/searchProducts",
new String[]{"keywords", "page"}, new String[]{keywords, page});
try {
Gson gson = new Gson();
Type type = new TypeToken<Result<List<Goods>>>() {
}.getType();
Result result = gson.fromJson(resultString, type);
// Result<List<Goods>> result = new Result<>();
// result.setCode(0);
// List<Goods> list = new ArrayList<>();
// for (int i = 0; i < 30; i++) {
// Goods goods = new Goods();
// goods.setImages("");
// goods.setTitle("手机"+i);
// list.add(goods);
// }
// result.setData(list);
return result;
} catch (Exception e) {
}
Result result = new Result();
result.setCode(-1);
result.setMsg("数据解析异常");
return result;
}
}
presenter层
首先写列表的presenter层
public class GoodsListPresenter extends BasePresenter {
private int page=1;
private boolean isRefresh=true;
public GoodsListPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
protected Result getData(Object... args) {
isRefresh = (boolean) args[0];//是否需要刷新
if (isRefresh){//刷新
page = 1;
}else{
page++;
}
Result result = GoodsListModel.goodsList((String)args[1],page+"");//调用网络请求获取数据
return result;
}
public boolean isResresh(){
return isRefresh;
}
}
忘记一个非常重要的东西
okhttp的封装类
我们model中用到的HttpUtils就是okhttp 的封装类
public class HttpUtils {
public static String get(String urlString){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
Request request = new Request.Builder().url(urlString).get().build();
try {
Response response = okHttpClient.newCall(request).execute();
String result = response.body().string();
Log.i("dt","请求结果:"+result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static String postForm(String url,String[] name,String[] value){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
FormBody.Builder formBuild = new FormBody.Builder();
for (int i = 0; i < name.length; i++) {
formBuild.add(name[i],value[i]);
}
Request request = new Request.Builder().url(url).post(formBuild.build()).build();
try {
Response response = okHttpClient.newCall(request).execute();
String result = response.body().string();
Log.i("dt",result);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static String postFile(String url,String[] name,String[] value,String fileParamName,File file){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())//日志拦截器
.connectTimeout(10, TimeUnit.SECONDS)//连接超时
.readTimeout(10,TimeUnit.SECONDS)//读取超时
.writeTimeout(10,TimeUnit.SECONDS)//写入超时
.build();
MultipartBody.Builder requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM);
if(file != null){
// MediaType.parse() 里面是上传的文件类型。
RequestBody body = RequestBody.create(MediaType.parse("image/*"), file);
String filename = file.getName();
// 参数分别为: 文件参数名 ,文件名称 , RequestBody
requestBody.addFormDataPart(fileParamName, "jpg", body);
}
if (name!=null) {
for (int i = 0; i < name.length; i++) {
requestBody.addFormDataPart(name[i], value[i]);
}
}
Request request = new Request.Builder().url(url).post(requestBody.build()).build();
try {
Response response = okHttpClient.newCall(request).execute();
if (response.code()==200) {
return response.body().string();
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public static String postJson(String url,String jsonString){
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),jsonString);
Request request = new Request.Builder().url(url).post(requestBody).build();
try {
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
接着我们的思路
完成presenter我们的下一步就是
主页面的效果实现
public class MainActivity extends AppCompatActivity implements XRecyclerView.LoadingListener,
DataCall<List<Goods>>, View.OnClickListener,GoodsListAdapter.OnItemClickListener,
GoodsListAdapter.OnItemLongClickListener {
private XRecyclerView mRecyclerView;
private GoodsListAdapter mAdapter;
private LinearLayoutManager mLinearManager;
private GridLayoutManager mGridManager;
private static final int GRID_LAYOUT_MANAGER = 1;
private static final int LINEAR_LAYOUT_MANAGER = 2;
private ImageView mBtnLayout;
private EditText mKeywordsEdit;
//新建商品列表Presenter
private GoodsListPresenter mPresenter = new GoodsListPresenter(this);
private ImageView shop;
/**
* 在onCreate方法里面查找Recylerview,并且设置上适配器。
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_PHONE_STATE, android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
}
mKeywordsEdit = findViewById(R.id.edit);
mBtnLayout = findViewById(R.id.qh);
findViewById(R.id.ss).setOnClickListener(this);
findViewById(R.id.shop_car_image).setOnClickListener(this);
mBtnLayout.setOnClickListener(this);
mRecyclerView = findViewById(R.id.xrecyclerView);//查找mRecyclerView
mRecyclerView.setLoadingListener(this);//添加下拉和刷新的监听器
// StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
mGridManager = new GridLayoutManager(this, 2,
GridLayoutManager.VERTICAL, false);//网格布局
mLinearManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);//线性布局
mRecyclerView.setLayoutManager(mLinearManager);
mAdapter = new GoodsListAdapter(this);//新建适配器
mAdapter.setOnItemClickListener(this);
mAdapter.setOnItemLongClickListener(this);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ss:
mRecyclerView.refresh();
break;
case R.id.qh:
if (mRecyclerView.getLayoutManager().equals(mLinearManager)) {
// if (mAdapter.getItemViewType(0) == GoodsListAdapter.LINEAR_TYPE) {
isGrid = true;
mAdapter.setViewType(GoodsListAdapter.GRID_TYPE);
mRecyclerView.setLayoutManager(mGridManager);
} else {
isGrid = false;
mAdapter.setViewType(GoodsListAdapter.LINEAR_TYPE);
mRecyclerView.setLayoutManager(mLinearManager);
}
mAdapter.notifyDataSetChanged();
break;
case R.id.shop_car_image:
Intent intent=new Intent(this,Cart2Activity.class);
startActivity(intent);
break;
}
// if (v.getId() == R.id.ss) {//搜索
// mRecyclerView.refresh();
// //调用列表刷新,刷新方法中获取输入框的值进行自动请求;
// } else if (v.getId() == R.id.qh) {//切换布局
//
if (!isGrid) {
// if (mRecyclerView.getLayoutManager().equals(mLinearManager)) {
if (mAdapter.getItemViewType(0) == GoodsListAdapter.LINEAR_TYPE) {
// isGrid = true;
// mAdapter.setViewType(GoodsListAdapter.GRID_TYPE);
// mRecyclerView.setLayoutManager(mGridManager);
// } else {
// isGrid = false;
// mAdapter.setViewType(GoodsListAdapter.LINEAR_TYPE);
// mRecyclerView.setLayoutManager(mLinearManager);
// }
// mAdapter.notifyDataSetChanged();
// }else if (v.getId() == R.id.shop){//进入购物车
Intent intent = new Intent(this,ShopCartActivity1.class);
// Intent intent=new Intent(this,Cart2Activity.class);
// startActivity(intent);
// }
}
@Override
public void onItemClick(Goods goods) {
Intent intent = new Intent(this,WebActivity.class);
intent.putExtra("url",goods.getDetailUrl());
startActivity(intent);
}
@Override
public void onItemLongClick(int position) {
mAdapter.remove(position);
mAdapter.notifyItemRemoved(position+1);//xRecyclerView如果增加了header,childView的序号就需要增加headerview的数量
if (position < mAdapter.getItemCount()+1) {
mAdapter.notifyItemRangeChanged(position+1,mAdapter.getItemCount()-position);
}
}
@Override
public void success(List<Goods> data) {
mRecyclerView.refreshComplete();//结束刷新
mRecyclerView.loadMoreComplete();//结束加载更多
if (mPresenter.isResresh()) {//只有刷新需要清空数据
mAdapter.clearList();
}
mAdapter.addAll(data);
mAdapter.notifyDataSetChanged();
}
@Override
public void fail(Result result) {
mRecyclerView.refreshComplete();//刷新完成,隐藏刷新view
mRecyclerView.loadMoreComplete();//加载完成,隐藏加载view
Toast.makeText(this, result.getCode() + " " + result.getMsg(),
Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
super.onDestroy();
mPresenter.unBindCall();//释放引用,防止内存溢出
}
private boolean isGrid = false;
@Override
public void onRefresh() {
String keywords = mKeywordsEdit.getText().toString();
mPresenter.requestData(true, keywords);
}
@Override
public void onLoadMore() {
String keywords = mKeywordsEdit.getText().toString();
mPresenter.requestData(false, keywords);
}
}
首先先写列表的adapter适配器
public class GoodsListAdapter extends RecyclerView.Adapter<GoodsListAdapter.GoodsHodler> {
private List<Goods> mList = new ArrayList<>();//数据集合
private Context context;
public final static int LINEAR_TYPE = 0;//线性
public final static int GRID_TYPE = 1;//网格
private int viewType = LINEAR_TYPE;
private OnItemClickListener onItemClickListener;
private OnItemLongClickListener onItemLongClickListener;
public GoodsListAdapter(Context context) {
this.context = context;
}
@Override
public int getItemViewType(int position) {
return viewType;
}
//设置item的视图类型
public void setViewType(int viewType) {
this.viewType = viewType;
}
@NonNull
@Override
public GoodsHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = null;
if (viewType == LINEAR_TYPE) {//通过第二个参数viewType判断选用的视图
view = View.inflate(viewGroup.getContext(), R.layout.goods_linear_item, null);//加载item布局
} else {
view = View.inflate(viewGroup.getContext(), R.layout.goods_grid_item, null);//加载item布局
}
GoodsHodler goodsHodler = new GoodsHodler(view);
return goodsHodler;
}
@Override
public void onBindViewHolder(@NonNull final GoodsHodler goodsHodler, final int position) {
final Goods goods = mList.get(position);//拿到商品,开始赋值
goodsHodler.itemView.setTag(mList.get(position));
//增加点击事件
goodsHodler.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//*******跳转webActivity进行网页访问
// Intent intent = new Intent(context,WebActivity.class);
// intent.putExtra("url",goods.getDetailUrl());
// context.startActivity(intent);
//————————跳转自定义的详情页面
Intent intent = new Intent(context,GoodsDetailActivity.class);
Bundle bundle = new Bundle();//使用bundle传递引用数据类型的对象
bundle.putSerializable("goods",goods);
intent.putExtras(bundle);//一定要把值放入了。
context.startActivity(intent);
// if (onItemClickListener!=null) {
// onItemClickListener.onItemClick(goods);
// }
}
});
goodsHodler.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//*****************方案1*************
// ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
// goodsHodler.itemView,"alpha",1,0
// );
// objectAnimator.setDuration(1000);
// objectAnimator.setInterpolator(new LinearInterpolator());
// objectAnimator.addListener(new Animator.AnimatorListener() {
// @Override
// public void onAnimationStart(Animator animation) {
//
// }
//
// @Override
// public void onAnimationEnd(Animator animation) {
// mList.remove(position);//动画执行结束,移除
// notifyDataSetChanged();//刷新列表
// goodsHodler.itemView.setAlpha(1);//由于复用机制,需要重新改成不透明
// }
//
// @Override
// public void onAnimationCancel(Animator animation) {
// mList.remove(position);//动画执行结束,移除
// notifyDataSetChanged();//刷新列表
// goodsHodler.itemView.setAlpha(1);//由于复用机制,需要重新改成不透明
// }
//
// @Override
// public void onAnimationRepeat(Animator animation) {
//
// }
// });
// objectAnimator.start();
//***********方案二**************
if (onItemLongClickListener!=null) {
onItemLongClickListener.onItemLongClick(position);
}
return true;
}
});
goodsHodler.text.setText(goods.getTitle());
//由于我们的数据图片提供的不标准,所以我们需要切割得到图片
String imageurl = "https" + goods.getImages().split("https")[1];
Log.i("dt", "imageUrl: " + imageurl);
imageurl = imageurl.substring(0, imageurl.lastIndexOf(".jpg") + ".jpg".length());
Glide.with(context).load(imageurl).into(goodsHodler.imageView);//加载图片
}
public static void main(String[] args) {
String aa = "a111a222a333a";
String[] b = aa.split("a");
System.out.println(b[0]);
}
@Override
public int getItemCount() {
return mList.size();
}
/**
* 添加集合数据
*/
public void addAll(List<Goods> data) {
if (data != null) {
mList.addAll(data);
}
}
/**
* 清空数据
*/
public void clearList() {
mList.clear();
}
/**
* 设置点击方法
*/
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public void remove(int position) {
mList.remove(position);
}
public class GoodsHodler extends RecyclerView.ViewHolder {
TextView text;
ImageView imageView;
public GoodsHodler(@NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.text);
imageView = itemView.findViewById(R.id.image);
}
}
/**
* @author dingtao
* @date 2018/12/15 10:28 AM
* 点击接口
*/
public interface OnItemClickListener {
void onItemClick(Goods goods);
}
/**
* @author dingtao
* @date 2018/12/15 10:28 AM
* 点击接口
*/
public interface OnItemLongClickListener {
void onItemLongClick(int position);
}
public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
this.onItemLongClickListener = onItemLongClickListener;
}
}
我们要实现切换视图的效果
所以我们要写两个item布局
一个网格一个纵向
先是网格
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aa"
android:padding="10dp"/>
</LinearLayout>
后是纵向
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_margin="10dp"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aa"
android:padding="10dp"/>
</LinearLayout>
接口回调点击事件
我们所要跳转的两个页面
其中一个就是点击条目跳转
跳转到用户详情页
实现轮播图功能和加入购物车点击吐司
用户详情页布局
一个是网页webview一个是banner轮播图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.youth.banner.Banner
android:id="@+id/goods_banner"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:id="@+id/goods_add_cart_btn"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="@color/mark_red"
android:textColor="@color/white"
android:layout_alignParentRight="true"
android:text="加入购物车"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
详情页功能代码实现
public class GoodsDetailActivity extends AppCompatActivity implements View.OnClickListener {
private Goods mGoods;//商品详情
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goods_detail);
Bundle bundle = getIntent().getExtras();
mGoods = (Goods) bundle.getSerializable("goods");//读取列表传递过来的商品数据
initBanner();
findViewById(R.id.goods_add_cart_btn).setOnClickListener(this);
}
/**
* 初始化banner
*/
private void initBanner() {
List<String> imageList = new ArrayList<>();//图片url集合
String[] imageurls = mGoods.getImages().split("https");//对图片进行切割
for (int i = 0; i < imageurls.length; i++) {
if (!TextUtils.isEmpty(imageurls[i])) {
String url = "https" + imageurls[i];
url = url.substring(0, url.lastIndexOf(".jpg") + ".jpg".length());
imageList.add(url);//图片路径拼接完成,重新赋值给数组
}
}
Banner banner = findViewById(R.id.goods_banner);
//设置图片加载器
banner.setImageLoader(new GlideImageLoader());
//设置banner样式
banner.setBannerStyle(BannerConfig.NUM_INDICATOR);
//设置图片集合
banner.setImages(imageList);
//设置标题集合(当banner样式有显示title时)
// banner.setBannerTitles(titleList);
//设置banner动画效果
banner.setBannerAnimation(Transformer.DepthPage);
//设置自动轮播,默认为true
banner.isAutoPlay(false);
//设置轮播时间
// banner.setDelayTime(1500);
//设置指示器位置(当banner模式中有指示器时)
banner.setIndicatorGravity(BannerConfig.RIGHT);
//banner设置方法全部调用完毕时最后调用
banner.start();
}
@Override
public void onClick(View v) {
if (v.getId()==R.id.goods_add_cart_btn){
Toast.makeText(this,"加入购物车",Toast.LENGTH_LONG).show();
}
}
}
public class WebActivity extends AppCompatActivity {
WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
// 设置与Js交互的权限
webSettings.setJavaScriptEnabled(true);
// 设置允许JS弹窗
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
String url = getIntent().getStringExtra("url");
//如果不设置WebViewClient,请求会跳转系统浏览器
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//该方法在Build.VERSION_CODES.LOLLIPOP以前有效,从Build.VERSION_CODES.LOLLIPOP起,建议使用shouldOverrideUrlLoading(WebView, WebResourceRequest)} instead
//返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理
//返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下
return false;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理
//返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下
return false;
}
});
// 先载入JS代码
// 格式规定为:file:///android_asset/文件名.html
mWebView.loadUrl(url);
// webview只是载体,内容的渲染需要使用webviewChromClient类去实现
// 通过设置WebChromeClient对象处理JavaScript的对话框
//设置响应js 的Alert()函数
mWebView.setWebChromeClient(new WebChromeClient());
}
}
第二个跳转就是跳转页面到
购物车页面布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ExpandableListView
android:id="@+id/list_cart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</ExpandableListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<CheckBox
android:id="@+id/check_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="全选" />
<TextView
android:id="@+id/goods_sum_price"
android:layout_toRightOf="@+id/check_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="价格:"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
</LinearLayout>
购物车的model层
public class CartModel {
public static Result goodsList() {
// String resultString = HttpUtils.get("http://www.zhaoapi.cn/product/getCarts?uid=71");
String resultString = "{\"msg\":\"请求成功\",\"code\":\"0\",\"data\":[{\"list\":[],\"sellerName\":\"\",\"sellerid\":\"0\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":11,\"pid\":11,\"price\":8989.0,\"pscid\":1,\"selected\":0,\"sellerid\":4,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家4\",\"sellerid\":\"4\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-03T23:43:53\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":1,\"pid\":13,\"price\":465.0,\"pscid\":1,\"selected\":0,\"sellerid\":6,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家6\",\"sellerid\":\"6\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":63,\"price\":10000.0,\"pscid\":40,\"selected\":0,\"sellerid\":7,\"subhead\":\"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)\",\"title\":\"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5\\/8G\\/256G\"}],\"sellerName\":\"商家7\",\"sellerid\":\"7\"},{\"list\":[{\"bargainPrice\":399.0,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5887\\/201\\/859509257\\/69994\\/6bde9bf6\\/59224c24Ne854e14c.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5641\\/233\\/853609022\\/57374\\/5c73d281\\/59224c24N3324d5f4.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5641\\/233\\/853609022\\/57374\\/5c73d281\\/59224c24N3324d5f4.jpg!q70.jpg\",\"num\":1,\"pid\":87,\"price\":888.0,\"pscid\":85,\"selected\":0,\"sellerid\":8,\"subhead\":\"满2件,总价打6.50折\",\"title\":\"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33\\/32(175\\/84A)\"},{\"bargainPrice\":3455.0,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9106\\/106\\/1785172479\\/537280\\/253bc0ab\\/59bf78a7N057e5ff7.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9106\\/106\\/1785172479\\/537280\\/253bc0ab\\/59bf78a7N057e5ff7.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8461\\/5\\/1492479653\\/68388\\/7255e013\\/59ba5e84N91091843.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8461\\/5\\/1492479653\\/68388\\/7255e013\\/59ba5e84N91091843.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8803\\/356\\/1478945529\\/489755\\/2a163ace\\/59ba5e84N7bb9a666.jpg!q70.jpg\",\"num\":2,\"pid\":52,\"price\":666.0,\"pscid\":39,\"selected\":0,\"sellerid\":8,\"subhead\":\"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机\",\"title\":\"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】\"}],\"sellerName\":\"商家8\",\"sellerid\":\"8\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":65,\"price\":12000.0,\"pscid\":40,\"selected\":0,\"sellerid\":9,\"subhead\":\"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)\",\"title\":\"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5\\/8G\\/256G\"},{\"bargainPrice\":2999.0,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/2385655.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2068\\/298\\/2448145915\\/157953\\/7be197df\\/56d51a42Nd86f1c8e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2437\\/128\\/1687178395\\/117431\\/bcc190c1\\/56d3fcbaNb2963d21.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2467\\/222\\/2263160610\\/95597\\/927b8a2f\\/56d3eafeNdecebeb6.jpg!q70.jpg\",\"num\":2,\"pid\":53,\"price\":777.0,\"pscid\":39,\"selected\":0,\"sellerid\":9,\"subhead\":\"Super AMOLED三星双曲面2K 屏,支持无线充电!\",\"title\":\"三星 Galaxy S7 edge(G9350)4GB+32GB 铂光金 移动联通电信4G手机 双卡双待\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":1,\"pid\":16,\"price\":199.0,\"pscid\":1,\"selected\":0,\"sellerid\":9,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家9\",\"sellerid\":\"9\"},{\"list\":[{\"bargainPrice\":11800.0,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/mitem.jd.hk\\/ware\\/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t6130\\/97\\/1370670410\\/180682\\/1109582a\\/593276b1Nd81fe723.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5698\\/110\\/2617517836\\/202970\\/c9388feb\\/593276b7Nbd94ef1f.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5815\\/178\\/2614671118\\/51656\\/7f52d137\\/593276c7N107b725a.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t5878\\/60\\/2557817477\\/30873\\/4502b606\\/593276caN5a7d6357.jpg!q70.jpg\",\"num\":3,\"pid\":66,\"price\":13000.0,\"pscid\":40,\"selected\":0,\"sellerid\":10,\"subhead\":\"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)\",\"title\":\"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5\\/8G\\/256G\"},{\"bargainPrice\":159.0,\"createtime\":\"2017-10-14T21:49:15\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8716\\/197\\/1271594444\\/173291\\/2f40bb4f\\/59b743bcN8509428e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8347\\/264\\/1286771527\\/92188\\/5cf5ec04\\/59b7420fN65378e9e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7363\\/165\\/3000956253\\/190883\\/179a372\\/59b743bfNd0c79d93.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7399\\/112\\/2935531768\\/183594\\/b77c7d4a\\/59b7441aNc3d40133.jpg!q70.jpg\",\"num\":5,\"pid\":99,\"price\":2100.0,\"pscid\":112,\"selected\":0,\"sellerid\":10,\"subhead\":\"针织针织闪闪闪亮你的眼\",\"title\":\"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-03T23:53:28\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":2,\"pid\":17,\"price\":299.0,\"pscid\":1,\"selected\":0,\"sellerid\":10,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家10\",\"sellerid\":\"10\"},{\"list\":[{\"bargainPrice\":159.0,\"createtime\":\"2017-10-14T21:49:15\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8716\\/197\\/1271594444\\/173291\\/2f40bb4f\\/59b743bcN8509428e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t8347\\/264\\/1286771527\\/92188\\/5cf5ec04\\/59b7420fN65378e9e.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7363\\/165\\/3000956253\\/190883\\/179a372\\/59b743bfNd0c79d93.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7399\\/112\\/2935531768\\/183594\\/b77c7d4a\\/59b7441aNc3d40133.jpg!q70.jpg\",\"num\":1,\"pid\":100,\"price\":2200.0,\"pscid\":112,\"selected\":0,\"sellerid\":11,\"subhead\":\"针织针织闪闪闪亮你的眼\",\"title\":\"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL\"},{\"bargainPrice\":22.9,\"createtime\":\"2017-10-14T21:38:26\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t1930\\/284\\/2865629620\\/390243\\/e3ade9c4\\/56f0a08fNbd3a1235.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2137\\/336\\/2802996626\\/155915\\/e5e90d7a\\/56f0a09cN33e01bd0.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t1882\\/31\\/2772215910\\/389956\\/c8dbf370\\/56f0a0a2Na0c86ea6.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t2620\\/166\\/2703833710\\/312660\\/531aa913\\/57709035N33857877.jpg!q70.jpg\",\"num\":5,\"pid\":34,\"price\":9.0,\"pscid\":2,\"selected\":0,\"sellerid\":11,\"subhead\":\"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》\",\"title\":\"三只松鼠 坚果炒货 零食奶油味 碧根果225g\\/袋\"},{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:48:08\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":2,\"pid\":18,\"price\":399.0,\"pscid\":1,\"selected\":0,\"sellerid\":11,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家11\",\"sellerid\":\"11\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":3,\"pid\":19,\"price\":499.0,\"pscid\":1,\"selected\":0,\"sellerid\":12,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家12\",\"sellerid\":\"12\"},{\"list\":[{\"bargainPrice\":111.99,\"createtime\":\"2017-10-14T21:39:05\",\"detailUrl\":\"https:\\/\\/item.m.jd.com\\/product\\/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends\",\"images\":\"https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t9004\\/210\\/1160833155\\/647627\\/ad6be059\\/59b4f4e1N9a2b1532.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7504\\/338\\/63721388\\/491286\\/f5957f53\\/598e95f1N7f2adb87.jpg!q70.jpg|https:\\/\\/m.360buyimg.com\\/n0\\/jfs\\/t7441\\/10\\/64242474\\/419246\\/adb30a7d\\/598e95fbNd989ba0a.jpg!q70.jpg\",\"num\":3,\"pid\":1,\"price\":118.0,\"pscid\":1,\"selected\":0,\"sellerid\":17,\"subhead\":\"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下\",\"title\":\"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g\"}],\"sellerName\":\"商家17\",\"sellerid\":\"17\"}]}";
try {
Gson gson = new Gson();
Type type = new TypeToken<Result<List<Shop>>>() {
}.getType();
Result result = gson.fromJson(resultString, type);
// Result<List<Goods>> result = new Result<>();
// result.setCode(0);
// List<Goods> list = new ArrayList<>();
// for (int i = 0; i < 30; i++) {
// Goods goods = new Goods();
// goods.setImages("");
// goods.setTitle("手机"+i);
// list.add(goods);
// }
// result.setData(list);
return result;
} catch (Exception e) {
}
Result result = new Result();
result.setCode(-1);
result.setMsg("数据解析异常");
return result;
}
}
购物车的presenter层
public class CartPresenter extends BasePresenter {
public CartPresenter(DataCall dataCall) {
super(dataCall);
}
@Override
protected Result getData(Object... args) {
Result result = CartModel.goodsList();//调用网络请求获取数据
return result;
}
}
方法实现
public class Cart2Activity extends AppCompatActivity implements
DataCall<List<Shop>>,CartAdapter2.TotalPriceListener{
private TextView mSumPrice;
private CheckBox mCheckAll;
private ExpandableListView mGoodsList;
private CartAdapter2 mCartAdapter;
private CartPresenter cartPresenter = new CartPresenter(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart2);
mSumPrice = findViewById(R.id.goods_sum_price);
mCheckAll = findViewById(R.id.check_all);
mCheckAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCartAdapter.checkAll(isChecked);
}
});
mGoodsList = findViewById(R.id.list_cart);
mCartAdapter = new CartAdapter2();
mGoodsList.setAdapter(mCartAdapter);
mCartAdapter.setTotalPriceListener(this);//设置总价回调器
mGoodsList.setGroupIndicator(null);
//让其group不能被点击
mGoodsList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
cartPresenter.requestData();
}
@Override
public void success(List<Shop> data) {
mCartAdapter.addAll(data);
//遍历所有group,将所有项设置成默认展开
int groupCount = data.size();
for (int i = 0; i < groupCount; i++) {
mGoodsList.expandGroup(i);
}
mCartAdapter.notifyDataSetChanged();
}
@Override
public void fail(Result result) {
Toast.makeText(this, result.getCode() + " " + result.getMsg(), Toast.LENGTH_LONG).show();
}
@Override
public void totalPrice(double totalPrice) {
mSumPrice.setText(String.valueOf(totalPrice));
}
}
购物车数据的适配器
public class CartAdapter2 extends BaseExpandableListAdapter {
private List<Shop> mList = new ArrayList<>();
private TotalPriceListener totalPriceListener;
public CartAdapter2(){
}
public void setTotalPriceListener(TotalPriceListener totalPriceListener) {
this.totalPriceListener = totalPriceListener;
}
@Override
public int getGroupCount() {
return mList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return mList.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return mList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return mList.get(groupPosition).getList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHodler holder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.cart2_group_item, null);
holder = new GroupHodler();
holder.checkBox = convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
} else {
holder = (GroupHodler) convertView.getTag();
}
final Shop shop = mList.get(groupPosition);
holder.checkBox.setText(shop.getSellerName());
holder.checkBox.setChecked(shop.isCheck());//设置商铺选中状态
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
shop.setCheck(checkBox.isChecked());//数据更新
List<Goods> goodsList = mList.get(groupPosition).getList();//得到商品信息
for (int i = 0; i < goodsList.size(); i++) {//商品信息循环赋值
goodsList.get(i).setSelected(checkBox.isChecked()?1:0);//商铺选中则商品必须选中
}
notifyDataSetChanged();
//计算价格
calculatePrice();
}
} );
return convertView;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
MyHolder holder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.cart_item, null);
holder = new MyHolder();
holder.text = convertView.findViewById(R.id.text);
holder.price = convertView.findViewById(R.id.text_price);
holder.image = convertView.findViewById(R.id.image);
holder.addSub = convertView.findViewById(R.id.add_sub_layout);
holder.check = convertView.findViewById(R.id.cart_goods_check);
convertView.setTag(holder);
} else {
holder = (MyHolder) convertView.getTag();
}
final Goods goods = mList.get(groupPosition).getList().get(childPosition);
holder.text.setText(goods.getTitle());
holder.price.setText("单价:"+goods.getPrice());//单价
//点击选中,计算价格
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkBox = (CheckBox) v;
goods.setSelected(checkBox.isChecked()?1:0);
calculatePrice();//计算价格
}
});
if (goods.getSelected()==0){
holder.check.setChecked(false);
}else{
holder.check.setChecked(true);
}
String imageurl = "https" + goods.getImages().split("https")[1];
Log.i("dt", "imageUrl: " + imageurl);
imageurl = imageurl.substring(0, imageurl.lastIndexOf(".jpg") + ".jpg".length());
Glide.with(DTApplication.getInstance()).load(imageurl).into(holder.image);//加载图片
holder.addSub.setCount(goods.getNum());//设置商品数量
holder.addSub.setAddSubListener(new AddSubLayout.AddSubListener() {
@Override
public void addSub(int count) {
goods.setNum(count);
calculatePrice();//计算价格
}
});
return convertView;
}
//全部选中或者取消
public void checkAll(boolean isCheck){
for (int i = 0; i < mList.size(); i++) {//循环的商家
Shop shop = mList.get(i);
shop.setCheck(isCheck);
for (int j = 0; j < shop.getList().size(); j++) {
Goods goods = shop.getList().get(j);
goods.setSelected(isCheck?1:0);
}
}
notifyDataSetChanged();
calculatePrice();
}
//计算总价格
private void calculatePrice(){
double totalPrice=0;
for (int i = 0; i < mList.size(); i++) {//循环的商家
Shop shop = mList.get(i);
for (int j = 0; j < shop.getList().size(); j++) {
Goods goods = shop.getList().get(j);
if (goods.getSelected()==1) {//如果是选中状态
totalPrice = totalPrice + goods.getNum() * goods.getPrice();
}
}
}
if (totalPriceListener!=null)
totalPriceListener.totalPrice(totalPrice);
}
public void addAll(List<Shop> data) {
if (data != null)
mList.addAll(data);
}
class MyHolder {
CheckBox check;
TextView text;
TextView price;
ImageView image;
AddSubLayout addSub;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class GroupHodler {
CheckBox checkBox;
}
public interface TotalPriceListener{
void totalPrice(double totalPrice);
}
}
适配器的二级列表条目的布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cart_goods_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="50dp"
android:layout_toRightOf="@+id/cart_goods_check"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_toRightOf="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="aa"
android:padding="10dp"/>
<TextView
android:id="@+id/text_price"
android:layout_toRightOf="@+id/image"
android:layout_below="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="价格"
android:padding="10dp"/>
<com.example.yue1.util.AddSubLayout
android:id="@+id/add_sub_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp">
</com.example.yue1.util.AddSubLayout>
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"
android:text="CheckBox" />
</LinearLayout>
购物车加减的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/btn_add"
android:layout_width="30dp"
android:layout_height="30dp"
android:focusable="false"
android:textSize="20sp"
android:gravity="center"
android:text="+" />
<TextView
android:id="@+id/text_number"
android:layout_width="60dp"
android:layout_height="30dp"
android:gravity="center"
android:textSize="14sp"
android:text="1000" />
<TextView
android:id="@+id/btn_sub"
android:layout_width="30dp"
android:layout_height="30dp"
android:textSize="20sp"
android:focusable="false"
android:gravity="center"
android:text="-" />
</LinearLayout>
自定义View
public class AddSubLayout extends LinearLayout implements View.OnClickListener {
private TextView mAddBtn,mSubBtn;
private TextView mNumText;
private AddSubListener addSubListener;
public AddSubLayout(Context context) {
super(context);
initView();
}
public AddSubLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public AddSubLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
@SuppressLint("NewApi")
public AddSubLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initView();
}
private void initView(){
//加载layout布局,第三个参数ViewGroup一定写成this
View view = View.inflate(getContext(),R.layout.car_add_sub_layout,this);
mAddBtn = view.findViewById(R.id.btn_add);
mSubBtn = view.findViewById(R.id.btn_sub);
mNumText = view.findViewById(R.id.text_number);
mAddBtn.setOnClickListener(this);
mSubBtn.setOnClickListener(this);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
int width = r-l;//getWidth();
int height = b-t;//getHeight();
}
@Override
public void onClick(View v) {
int number = Integer.parseInt(mNumText.getText().toString());
switch (v.getId()){
case R.id.btn_add:
number++;
mNumText.setText(number+"");
break;
case R.id.btn_sub:
if (number==0){
Toast.makeText(getContext(),"数量不能小于0",Toast.LENGTH_LONG).show();
return;
}
number--;
mNumText.setText(number+"");
break;
}
if (addSubListener!=null){
addSubListener.addSub(number);
}
}
public void setCount(int count) {
mNumText.setText(count+"");
}
public void setAddSubListener(AddSubListener addSubListener) {
this.addSubListener = addSubListener;
}
public interface AddSubListener{
void addSub(int count);
}
}
实现加减
imageloader的封装类
public class GlideImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
/**
注意:
1.图片加载器由自己选择,这里不限制,只是提供几种使用方法
2.返回的图片路径为Object类型,由于不能确定你到底使用的那种图片加载器,
传输的到的是什么格式,那么这种就使用Object接收和返回,你只需要强转成你传输的类型就行,
切记不要胡乱强转!
*/
//Glide 加载图片简单用法
Glide.with(context).load(path).into(imageView);
//Picasso 加载图片简单用法
// Picasso.with(context).load(path).into(imageView);
//用fresco加载图片简单用法,记得要写下面的createImageView方法
// Uri uri = Uri.parse((String) path);
// imageView.setImageURI(uri);
}
//提供createImageView 方法,如果不用可以不重写这个方法,主要是方便自定义ImageView的创建
// @Override
// public ImageView createImageView(Context context) {
// //使用fresco,需要创建它提供的ImageView,当然你也可以用自己自定义的具有图片加载功能的ImageView
// SimpleDraweeView simpleDraweeView=new SimpleDraweeView(context);
// return simpleDraweeView;
// }
}
request response封装类日志
public class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
//这个chain里面包含了request和response,所以你要什么都可以从这里拿
Request request = chain.request();
long t1 = System.nanoTime();//请求发起的时间
Log.i("dt",String.format("发送请求 %s on %s%n%s",
request.url(), chain.connection(), request.headers()));
Response response = chain.proceed(request);
long t2 = System.nanoTime();//收到响应的时间
//这里不能直接使用response.body().string()的方式输出日志
//因为response.body().string()之后,response中的流会被关闭,程序会报错,我们需要创建出一
//个新的response给应用层处理
ResponseBody responseBody = response.peekBody(1024 * 1024);
Log.i("dt",String.format("接收响应: [%s] %n返回json:【%s】 %.1fms%n%s",
response.request().url(),
responseBody.string(),
(t2 - t1) / 1e6d,
response.headers()));
return response;
}
}