public class OkHttpUtil { private OkHttpListener mOkHttpListener; private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0://成功 String data = (String) msg.obj; mOkHttpListener.OkHttpSuccess(data); break; case 1://失败 String error = (String) msg.obj; mOkHttpListener.OkHttpError(error); break; } } }; //生成 OkHttpListener set方法 public void setOkHttpListener(OkHttpListener okHttpListener) { mOkHttpListener = okHttpListener; } public OkHttpUtil OkHttpGet(String url){ //获取clench对象 OkHttpClient okHttpClient = new OkHttpClient(); //获取Request对象 Request request = new Request.Builder().url(url).build(); okHttpClient.newCall(request).enqueue(new Callback() { /** * 失败回调方法 * @param call * @param e */ @Override public void onFailure(Call call, IOException e) { Message message = new Message(); message.what = 1; message.obj = e.getMessage(); mHandler.sendMessage(message); } /** * 成功回调方法 * @param call * @param response * @throws IOException */ @Override public void onResponse(Call call, Response response) throws IOException { Message message = new Message(); message.what = 0; message.obj = response.body().string(); mHandler.sendMessage(message); } }); return this; } public OkHttpUtil OkHttoPost(String url, FormBody formBody){ //创建 clench 对象 OkHttpClient okHttpClient = new OkHttpClient(); //获取Request Request request = new Request.Builder().url(url).post(formBody).build(); okHttpClient.newCall(request).enqueue(new Callback() { /** * 失败回调方法 * @param call * @param e */ @Override public void onFailure(Call call, IOException e) { Message message = new Message(); message.what = 1; message.obj = e.getMessage(); mHandler.sendMessage(message); } /** * 成功回调方法 * @param call * @param response * @throws IOException */ @Override public void onResponse(Call call, Response response) throws IOException { Message message = new Message(); message.what = 0; message.obj = response.body().string(); mHandler.sendMessage(message); } }); return this; } //定义一个接口,进行回调 public interface OkHttpListener{ //成功 void OkHttpSuccess(String data); //失败 void OkHttpError(String error); } }
注意: 因为需要排序 ,我们在封装实体类的时候一定要注意 价钱 的类型
public class GoodsBean { private String msg; private String code; private String page; private Listdata; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getPage() { return page; } public void setPage(String page) { this.page = page; } public List getData() { return data; } public void setData(List data) { this.data = data; } public static class DataBean { private double bargainPrice; private String createtime; private String detailUrl; private String images; private int itemtype; private int pid; private double price; private int pscid; private int salenum; private int sellerid; private String subhead; private String title; 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 getItemtype() { return itemtype; } public void setItemtype(int itemtype) { this.itemtype = itemtype; } 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 getSalenum() { return salenum; } public void setSalenum(int salenum) { this.salenum = salenum; } 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; } } }
1.创建一个CallBack接口
public interface ICallBack { void onSuccess(ListdataBeans); void onFailed(String error); }
2.创建一个view层接口
public interface GoodsView { void onGoodsSuccess(ListdataBeans); void onGoodsFailed(String error); }
3.在model层创建一个类
public class GoodsModel { public void goodsData(final ListdataBeans, int page, int sort, final ICallBack callBack) { page = 1; //http://www.zhaoapi.cn/product/searchProducts?keywords=%E6%89%8B%E6%9C%BA&page=1&sort=1 final String goodsUrl = "http://www.zhaoapi.cn/product/searchProducts?keywords=%E6%89%8B%E6%9C%BA&page=" + page + "&sort=" + sort; new OkHttpUtil().OkHttpGet(goodsUrl).setOkHttpListener(new OkHttpUtil.OkHttpListener() { @Override public void OkHttpSuccess(String data) { Gson gson = new Gson(); GoodsBean goodsBean = gson.fromJson(data, GoodsBean.class); List beans = goodsBean.getData(); if (goodsBean.getCode().equals("0")) { callBack.onSuccess(beans); } else { callBack.onFailed(goodsBean.getMsg()); } } @Override public void OkHttpError(String error) { } }); } }
4.在presenter层创建一个类,进行view层和model层的交互与解绑
public class GoodsPresenter { private GoodsView mGoodsView; private GoodsModel mGoodsModel; public GoodsPresenter(GoodsView goodsView) { mGoodsView = goodsView; mGoodsModel = new GoodsModel(); } //解绑 public void datechView() { mGoodsView = null; } public void goodsData(ListdataBeans, int page, int sort) { page = 1; mGoodsModel.goodsData(dataBeans, page, sort, new ICallBack() { @Override public void onSuccess(List dataBeans) { mGoodsView.onGoodsSuccess(dataBeans); } @Override public void onFailed(String error) { mGoodsView.onGoodsFailed(error); } }); } }
public class MyXRecyclerViewAdapter extends RecyclerView.Adapter{ private Context mContext; private List mDataBeans = new ArrayList<>(); public MyXRecyclerViewAdapter(Context context, List dataBeans) { mContext = context; mDataBeans = dataBeans; } //更新适配器 public void update(List dataBeans) { mDataBeans = dataBeans; notifyDataSetChanged(); } @NonNull @Override public MyGoodsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(mContext).inflate(R.layout.goods_view, viewGroup, false); MyGoodsViewHolder holder = new MyGoodsViewHolder(view); return holder; } @Override public void onBindViewHolder(@NonNull final MyGoodsViewHolder myGoodsViewHolder, final int i) { String images = mDataBeans.get(i).getImages(); //分割字符串 String[] split = images.split("\\|"); for (int j = 0; j < images.length(); j++) { Glide.with(mContext).load(split[0]).into(myGoodsViewHolder.mImageViewGoodsImg); } myGoodsViewHolder.mTextViewTitle.setText(mDataBeans.get(i).getTitle()); myGoodsViewHolder.mTextViewPrice.setText(mDataBeans.get(i).getPrice() + ""); myGoodsViewHolder.mTextViewSalenum.setText(mDataBeans.get(i).getSalenum() + ""); myGoodsViewHolder.mImageViewGoodsImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(mContext, "点击了: " + mDataBeans.get(i).getTitle(), Toast.LENGTH_SHORT).show(); mGoodsClick.onGoodsClick(i); } }); } @Override public int getItemCount() { return mDataBeans == null ? 0 : mDataBeans.size(); } class MyGoodsViewHolder extends RecyclerView.ViewHolder { ImageView mImageViewGoodsImg; TextView mTextViewTitle, mTextViewPrice, mTextViewSalenum; public MyGoodsViewHolder(@NonNull View itemView) { super(itemView); mImageViewGoodsImg = itemView.findViewById(R.id.goods_img); mTextViewTitle = itemView.findViewById(R.id.goods_title); mTextViewPrice = itemView.findViewById(R.id.goods_price); mTextViewSalenum = itemView.findViewById(R.id.goods_salenum); } } private goodsClick mGoodsClick; public void setGoodsClick(goodsClick goodsClick) { mGoodsClick = goodsClick; } //自定义接口 public interface goodsClick { void onGoodsClick(int position); } }
public class MainActivity extends AppCompatActivity implements GoodsView, View.OnClickListener { private ImageView mImgBack; private SearchView mEdTxtView; private ImageView mImgDan; /** * 综合 */ private Button mTxtZh; /** * 销量 */ private Button mTxtXl; /** * 价格 */ private Button mTxtJg; /** * 筛选 */ private Button mTxtSx; private XRecyclerView mXRv; private GoodsPresenter mGoodsPresenter; private ListmDataBeans = new ArrayList<>(); private MyXRecyclerViewAdapter mMyXRecyclerViewAdapter; private int pager = 1; private int sort = 0; //先定义一个布尔值用来切换图片 private boolean b = false; private Intent mIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 initView(); //创建presenter层实例 mGoodsPresenter = new GoodsPresenter(this); mGoodsPresenter.goodsData(mDataBeans, pager, sort); mXRv.setPullRefreshEnabled(true);//开启下拉刷新 mXRv.setLoadingMoreEnabled(true);//开启上拉加载更多 mXRv.setLoadingListener(new XRecyclerView.LoadingListener() { @Override public void onRefresh() {//下拉刷新 mGoodsPresenter.goodsData(mDataBeans, pager, sort); mXRv.refreshComplete(); } @Override public void onLoadMore() {//上拉加载更多 pager++;//每次加一 mGoodsPresenter.goodsData(mDataBeans, pager, sort); mMyXRecyclerViewAdapter.update(mDataBeans); mMyXRecyclerViewAdapter.notifyDataSetChanged(); mXRv.refreshComplete(); } }); //线性布局管理器 mXRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); } //初始化控件 private void initView() { mImgBack = (ImageView) findViewById(R.id.img_back); mEdTxtView = (SearchView) findViewById(R.id.ed_txt_view); mImgDan = (ImageView) findViewById(R.id.img_dan); mTxtZh = (Button) findViewById(R.id.txt_zh); mTxtXl = (Button) findViewById(R.id.txt_xl); mTxtJg = (Button) findViewById(R.id.txt_jg); mTxtSx = (Button) findViewById(R.id.txt_sx); mXRv = (XRecyclerView) findViewById(R.id.x_rv); mImgBack.setOnClickListener(this); mImgDan.setOnClickListener(this); mTxtZh.setOnClickListener(this); mTxtXl.setOnClickListener(this); mTxtJg.setOnClickListener(this); mTxtSx.setOnClickListener(this); mXRv.setOnClickListener(this); } /** * 成功 * * @param dataBeans */ @Override public void onGoodsSuccess(final List dataBeans) { mMyXRecyclerViewAdapter = new MyXRecyclerViewAdapter(this, dataBeans); mXRv.setAdapter(mMyXRecyclerViewAdapter); mMyXRecyclerViewAdapter.update(dataBeans); mMyXRecyclerViewAdapter.notifyDataSetChanged(); mMyXRecyclerViewAdapter.setGoodsClick(new MyXRecyclerViewAdapter.goodsClick() { @Override public void onGoodsClick(int position) { mIntent = new Intent(MainActivity.this, CartActivity.class); String url = dataBeans.get(position).getDetailUrl(); int pid = dataBeans.get(position).getPid(); String replace = url.replace("https", "http"); mIntent.putExtra("url",replace+pid); startActivity(mIntent); } }); } /** * 失败 * * @param error */ @Override public void onGoodsFailed(String error) { Toast.makeText(this, error, Toast.LENGTH_SHORT).show(); } @Override public void onClick(View v) { switch (v.getId()) { default: break; case R.id.img_back: this.finish(); break; case R.id.img_dan: checkIv(v); break; case R.id.txt_zh://综合排序 mGoodsPresenter.goodsData(mDataBeans, pager, 0); break; case R.id.txt_xl:///销量排序 mGoodsPresenter.goodsData(mDataBeans, pager, 1); break; case R.id.txt_jg://价格排序 mGoodsPresenter.goodsData(mDataBeans, pager, 2); break; case R.id.txt_sx: break; } } public void checkIv(View v){ Toast.makeText(MainActivity.this,"点击了切换视图按钮",Toast.LENGTH_SHORT).show(); if (b==false) { //点击后想要变成什么要的布局样式就搞一个你的需求 mXRv.setLayoutManager(new GridLayoutManager(this,2)); //给布尔值重新赋值 b = true; //给点击按钮的图片重新赋值 mImgDan.setImageResource(R.drawable.duo); }else if (b==true) { mXRv.setLayoutManager(new LinearLayoutManager(this)); //给布尔值重新赋值 b = false; //给点击按钮的图片重新赋值 mImgDan.setImageResource(R.drawable.dan); } } }
条目布局
public class CartActivity extends AppCompatActivity { private WebView mWv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cart); initView(); //支持js语言 mWv.getSettings().setJavaScriptEnabled(true); // 缩放至屏幕的大小 mWv.getSettings().setLoadWithOverviewMode(true); //支持缩放 mWv.getSettings().setSupportZoom(true); //取值 Intent intent = getIntent(); //获取Intent中的数据 String url = intent.getStringExtra("url"); //把字符串赋值给输入框 mWv.loadUrl(url); //设置用自己的浏览器打开 mWv.setWebViewClient(new MyWebViewClient()); } private void initView() { mWv = (WebView) findViewById(R.id.wv); } //自定义浏览器 class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); } } //预防内存泄露 @Override protected void onPause() { super.onPause(); finish(); } }
WebView布局