//Recycleview依赖 implementation ‘com.android.support:recyclerview-v7:26.1.0‘ //拦截器的依赖 compile ‘com.squareup.okhttp3:logging-interceptor:3.8.0‘ //SimpleDraweeView加载图片的依赖: compile ‘com.facebook.fresco:fresco:1.5.0‘ //Banner的依赖: compile ‘com.youth.banner:banner:1.4.10‘ // Fresco依赖: compile ‘com.facebook.fresco:fresco:1.5.0‘ //Eventbus依赖: compile ‘org.greenrobot:eventbus:3.1.1‘ // Rxjava2: compile ‘io.reactivex.rxjava2:rxjava:2.1.7‘ compile ‘com.squareup.retrofit2:adapter-rxjava2:2.3.0‘ compile ‘io.reactivex.rxjava2:rxandroid:2.0.1‘ //Okhttp依赖 compile ‘com.squareup.okhttp3:okhttp:3.9.1‘ //retrofit依赖 compile ‘com.squareup.retrofit2:retrofit:2.3.0‘ //GSON依赖 compile ‘com.squareup.retrofit2:converter-gson:2.3.0‘ //底部按钮 compile ‘com.hjm:BottomTabBar:1.1.1‘ //刷新页面 compile ‘com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-4‘
RetrofitHelper
public class RetrofitHelper { private static OkHttpClient client; private static ServiceApi api; static { initOkHttp(); } private static void initOkHttp() { if (client == null) { synchronized (OkHttpClient.class) { if (client == null) { client = new OkHttpClient.Builder().build(); } } } } public static ServiceApi getApi() { if (api == null) { synchronized (ServiceApi.class) { if (api == null) { api = RetrofitHelper.create(ServiceApi.class, UrlUtils.BASE_URL); } } } return api; } private static <T> T create(Class<T> tClass, String baseUrl) { Retrofit re = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); return re.create(tClass); } }
OnListiner
public interface OnListiner<T> { public void onSuccess(T t); public void onFailure(Throwable throwable); }
UrlUtils
public class UrlUtils { public static final String BASE_URL="https://www.zhaoapi.cn/"; public static final String HEAD_PATH="ad/getAd"; public static final String HEAD_FEN="product/getCatagory"; public static final String SUBCLASS="product/getProductCatagory"; }
ServiceApi
public interface ServiceApi { //首页请求 @GET(UrlUtils.HEAD_PATH) public Flowable<HeadBean> getHeads(); //分类请求 @GET(UrlUtils.HEAD_FEN) public Flowable<ClassifyBean> getClassify(); //子分类请求 @GET(UrlUtils.SUBCLASS) public Flowable<SubClass> getSubClass(@Query("cid") String cid); }
M
public class HeadModel implements IHeadModel { @Override public void getDatas(final OnListiner onListiner) { ServiceApi api = RetrofitHelper.getApi(); Flowable<HeadBean> getAd = api.getHeads(); getAd.doOnSubscribe(new Consumer<Subscription>() { @Override public void accept(Subscription subscription) throws Exception { Log.i("hhhhhhhhhhhhhhhhhhh","开始请求数据"); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<HeadBean>() { @Override public void accept(HeadBean headBean) throws Exception { onListiner.onSuccess(headBean); } }); } }
public interface IClassifyModel<T> { public void getClassifyDatas(OnListiner<T> onListiner); }
P
public class HeadPresenter { private IHeadFragment iHeadFragment; private IHeadModel iHeadModel; public HeadPresenter(IHeadFragment iHeadFragment) { this.iHeadFragment = iHeadFragment; iHeadModel = new HeadModel(); } public void getHeadDatas() { iHeadModel.getDatas(new OnListiner() { @Override public void onSuccess(Object o) { iHeadFragment.getDatas((HeadBean) o); } @Override public void onFailure(Throwable throwable) { } }); } }
V
public interface IHeadFragment { public void getDatas(HeadBean headBean); }
public class Fragment01 extends Fragment implements IHeadFragment { private HeadPresenter presenter; private Banner banner; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment01,container,false); banner = view.findViewById(R.id.banner); presenter = new HeadPresenter(this); presenter.getHeadDatas(); return view; } @Override public void getDatas(HeadBean headBean) { Toast.makeText(getActivity(),headBean.getCode(),Toast.LENGTH_SHORT).show(); List<String> images = new ArrayList<>(); List<HeadBean.DataBean> data = headBean.getData(); for (int i = 0;i<data.size();i++) { images.add(data.get(i).getIcon()) ; } banner.setImageLoader(new GlideImageLoader()); banner.isAutoPlay(true); //设置轮播的时间间隔 banner.setDelayTime(3000); banner.setImages(images); banner.start(); } }
适配器
public class HeadClassify extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context context; private List<ClassifyBean.DataBean> list; private LayoutInflater inflater; public HeadClassify(Context context, List<ClassifyBean.DataBean> list) { this.context = context; this.list = list; inflater =LayoutInflater.from(context); } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = inflater.inflate(R.layout.head_fenlei_item, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { MyViewHolder holder1 = (MyViewHolder) holder; ClassifyBean.DataBean dataBean = list.get(position); Uri parse = Uri.parse(dataBean.getIcon()); holder1.img.setImageURI(parse); holder1.txt.setText(dataBean.getName()); } @Override public int getItemCount() { return list.size(); } class MyViewHolder extends RecyclerView.ViewHolder{ private SimpleDraweeView img; private TextView txt; public MyViewHolder(View itemView) { super(itemView); img = itemView.findViewById(R.id.fenlei_img); txt = itemView.findViewById(R.id.fenlei_txt); } } }
<?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="match_parent" android:background="#ffffff" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.youth.banner.Banner android:layout_width="match_parent" android:layout_height="200dp" android:id="@+id/banner" ></com.youth.banner.Banner> <ImageView android:layout_width="match_parent" android:layout_height="200px" android:background="@mipmap/jd_huo_dong" android:id="@+id/jd" android:layout_margin="10dp" /> </LinearLayout> </ScrollView> </LinearLayout>