view层
package com.bwie.jingdong.frist.view;
import android.content.Context;
import com.bwie.jingdong.bean.BannerBean;
import com.bwie.jingdong.bean.CategoryBean;
import com.bwie.jingdong.bean.MainClassifyBean;
import com.bwie.jingdong.bean.NextClassifyBean;
import com.bwie.jingdong.bean.ProductBean;
import java.util.List;
/**
* Created by dell on 2018/10/17.
*/
public interface FristView {
void getBanner(List<BannerBean.DataBean> list);
void getCategory(List<CategoryBean.DataBean> list);
void getProduct(List<ProductBean.DataBean> list);
Context getContext();
void failed(Exception e);
}
model层
package com.bwie.jingdong.frist.model;
import com.bwie.jingdong.callback.ICallBack;
import com.bwie.jingdong.utils.HttpUtils;
import java.lang.reflect.Type;
/**
* Created by dell on 2018/10/17.
*/
public class FristModel {
public void getData(String url, ICallBack callBack, Type type){
HttpUtils.getInstance().get(url,callBack,type);
}
}
presenter层
package com.bwie.jingdong.frist.presenter;
import android.content.SharedPreferences;
import com.bwie.jingdong.bean.BannerBean;
import com.bwie.jingdong.bean.CategoryBean;
import com.bwie.jingdong.bean.MainClassifyBean;
import com.bwie.jingdong.bean.ProductBean;
import com.bwie.jingdong.callback.ICallBack;
import com.bwie.jingdong.frist.model.FristModel;
import com.bwie.jingdong.frist.view.FristView;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
/**
* Created by dell on 2018/10/17.
*/
public class FristPresenter {
private FristView fv;
private FristModel fristModel;
public void attach(FristView fv) {
this.fv = fv;
fristModel = new FristModel();
}
public void getBanner(){
Type type = new TypeToken<BannerBean>(){}.getType();
fristModel.getData("https://www.zhaoapi.cn/ad/getAd", new ICallBack() {
@Override
public void onSuccess(Object o) {
BannerBean bannerBean = (BannerBean) o;
if (bannerBean != null){
fv.getBanner(bannerBean.getData());
}
}
@Override
public void onFailed(Exception e) {
fv.failed(e);
}
},type);
}
public void getCategroy(){
Type type = new TypeToken<CategoryBean>(){}.getType();
fristModel.getData("https://www.zhaoapi.cn/product/getCatagory", new ICallBack() {
@Override
public void onSuccess(Object o) {
CategoryBean categoryBean = (CategoryBean) o;
if (categoryBean != null){
fv.getCategory(categoryBean.getData());
}
}
@Override
public void onFailed(Exception e) {
fv.failed(e);
}
},type);
}
public void getProducy(){
Type type = new TypeToken<ProductBean>(){}.getType();
fristModel.getData("https://www.zhaoapi.cn/product/getCarts?uid=71", new ICallBack() {
@Override
public void onSuccess(Object o) {
ProductBean productBean = (ProductBean) o;
if (productBean != null){
fv.getProduct(productBean.getData());
}
}
@Override
public void onFailed(Exception e) {
fv.failed(e);
}
},type);
}
public void detach(){
if (fv != null){
fv = null;
}
}
}
主页面
package com.bwie.jingdong.frist.view;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.bwie.jingdong.R;
import com.bwie.jingdong.bean.MainClassifyBean;
import com.bwie.jingdong.bean.NextClassifyBean;
import com.bwie.jingdong.search.SearchActivity;
import com.bwie.jingdong.adapter.BannerAdapter;
import com.bwie.jingdong.adapter.CategoryAdapter;
import com.bwie.jingdong.adapter.ProductAdapter;
import com.bwie.jingdong.bean.BannerBean;
import com.bwie.jingdong.bean.CategoryBean;
import com.bwie.jingdong.bean.ProductBean;
import com.bwie.jingdong.frist.presenter.FristPresenter;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import java.util.ArrayList;
import java.util.List;
/**
* Created by dell on 2018/10/15.
*/
public class FristFragment extends Fragment implements FristView, View.OnClickListener {
private ImageView imgRichscan;
private EditText etSearch;
private ImageView imgMessage;
private RecyclerView rvSudoku;
private RecyclerView rvProduct;
private ViewPager vpBanner;
private List<BannerBean.DataBean> bannerList;
private List<CategoryBean.DataBean> categoryList;
private List<ProductBean.DataBean> productList;
private BannerAdapter bannerAdapter;
private FristPresenter presenter;
private ProductAdapter productAdapter;
private CategoryAdapter categoryAdapter;
private int REQUEST_CODE = 100;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 100){
int current = vpBanner.getCurrentItem();
if (current<bannerList.size()-1){
current++;
}else {
current=0;
}
vpBanner.setCurrentItem(current);
sendEmptyMessageDelayed(100,4000);
}
}
};
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frist_item,container,false);
imgMessage = v.findViewById(R.id.img_message);
imgRichscan = v.findViewById(R.id.img_richscan);
etSearch = v.findViewById(R.id.et_search);
rvSudoku = v.findViewById(R.id.rv_sudoku);
rvProduct = v.findViewById(R.id.rv_product);
vpBanner = v.findViewById(R.id.vp_banner);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
setListener();
presenter = new FristPresenter();
presenter.attach(this);
presenter.getBanner();
presenter.getCategroy();
presenter.getProducy();
}
private void setListener() {
etSearch.setOnClickListener(this);
imgRichscan.setOnClickListener(this);
imgMessage.setOnClickListener(this);
}
private void initView() {
bannerList = new ArrayList<>();
categoryList = new ArrayList<>();
productList = new ArrayList<>();
bannerAdapter = new BannerAdapter(getActivity(),bannerList);
productAdapter = new ProductAdapter(getActivity(),productList);
categoryAdapter = new CategoryAdapter(getActivity(),categoryList);
vpBanner.setAdapter(bannerAdapter);
rvSudoku.setAdapter(categoryAdapter);
rvProduct.setAdapter(productAdapter);
RecyclerView.LayoutManager clayoutManager = new GridLayoutManager(getActivity(),2, LinearLayoutManager.HORIZONTAL,false);
RecyclerView.LayoutManager playoutManager = new GridLayoutManager(getActivity(),2, LinearLayoutManager.VERTICAL,false);
rvSudoku.setLayoutManager(clayoutManager);
rvProduct.setLayoutManager(playoutManager);
handler.sendEmptyMessageDelayed(100,4000);
}
@Override
public void getBanner(List<BannerBean.DataBean> list) {
if (list != null){
bannerList.clear();
bannerList.addAll(list);
bannerAdapter.notifyDataSetChanged();
}
}
@Override
public void getCategory(List<CategoryBean.DataBean> list) {
if (list != null){
categoryList.clear();
categoryList.addAll(list);
categoryAdapter.notifyDataSetChanged();
}
}
@Override
public void getProduct(List<ProductBean.DataBean> list) {
if (list != null){
productList.clear();
productList.addAll(list);
productAdapter.notifyDataSetChanged();
}
}
@Override
public void failed(Exception e) {
Toast.makeText(getActivity(),"网络异常",Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.et_search:
startActivity(new Intent(getActivity(), SearchActivity.class));
break;
case R.id.img_message:
break;
case R.id.img_richscan:
Intent intent = new Intent(getContext(), CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
break;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (presenter != null){
presenter.detach();
}
}
}
utils页面
package com.bwie.jingdong.utils;
import android.os.Handler;
import com.bwie.jingdong.callback.ICallBack;
import com.google.gson.Gson;
import java.io.IOException;
import java.lang.reflect.Type;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by dell on 2018/10/16.
*/
public class HttpUtils {
private static volatile HttpUtils instance;
private OkHttpClient client;
private Handler handler = new Handler();
private HttpUtils(){
client = new OkHttpClient();
}
public static HttpUtils getInstance(){
if (instance == null){
synchronized (HttpUtils.class){
if (null == instance){
instance = new HttpUtils();
}
}
}
return instance;
}
public void get(String url, final ICallBack callBack, final Type type){
Request request = new Request.Builder()
.get()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
callBack.onFailed(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
Gson gson = new Gson();
final Object o = gson.fromJson(result,type);
handler.post(new Runnable() {
@Override
public void run() {
callBack.onSuccess(o);
}
});
}
});
}
}