检查清单文件权限和依赖
权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
涉及依赖
implementation 'com.hjm:BottomTabBar:1.1.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.7.0'
implementation 'com.squareup.okio:okio:1.12.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
涉及接口
public static String Shou="http://www.zhaoapi.cn/product/getCatagory";
public static String Zhu="http://www.zhaoapi.cn/product/getProductCatagory";
1丶实现布局记得创建条目布局
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<com.bwei.xzh.TopBar
android:id="@+id/topbarr"
android:background="@color/colorAccent"
app:leftBackground="@drawable/but_b"
app:rightBackground="@drawable/but_a"
app:titleText="最后的爱"
app:titleTextColor="#FFF"
app:titleTextSize="13sp"
android:layout_width="wrap_content"
android:layout_height="60dp"></com.bwei.xzh.TopBar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:id="@+id/mls"/>
<android.support.v7.widget.RecyclerView
android:layout_marginLeft="20dp"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/ks"/>
</LinearLayout>
</LinearLayout>
Okhttp拦截器
package com.bwei.xzh.network;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Okhttp {
private static final String TAG = "HttpUtils";
private static volatile Okhttp instance;
private OkHttpClient client;
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() {
client = new OkHttpClient().newBuilder()
.readTimeout(3000, TimeUnit.SECONDS) //设置读取超时时间
.connectTimeout(3000, TimeUnit.SECONDS) //设置连接的超时时间
.addInterceptor(getAppInterceptor())//Application拦截器
.build();
}
//单例okhttp
public static Okhttp getInstance() {
if (instance == null) {
synchronized (Okhttp.class) {
if(null == instance) {
instance = new Okhttp();
}
}
}
return instance;
}
//Get请求
public void getHttpGet(String url, Callback call){
//初始化okhttp
OkHttpClient okHttpClient = new OkHttpClient();
//初始化request
Request request = new Request.Builder()
.url(url)
.get()
.build();
Call mCall=okHttpClient.newCall(request);
mCall.enqueue(call);
};
}
适配器
package com.bwei.xzh.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.bumptech.glide.request.RequestOptions;
import com.bwei.xzh.R;
import com.bwei.xzh.bean.Bean;
import java.util.List;
//继承RecyclerView
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.HomeViewHolder> {
Bean home;
Context con;
private LayoutInflater mInflater;
private List<Bean.DataBean> list;
private View inflate;
public HomeAdapter(Context context, Bean homeBean){
this.mInflater=LayoutInflater.from(context);
this.con=context;
this.home=homeBean;
}
//创建子view视图
@NonNull
@Override
public HomeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
inflate = mInflater.inflate(R.layout.erbuju, viewGroup, false);
HomeViewHolder homeViewHolder = new HomeViewHolder(inflate);
return homeViewHolder;
}
//绑定数据
@Override
public void onBindViewHolder(@NonNull HomeViewHolder homeViewHolder, int i) {
list = home.getData();
homeViewHolder.protv.setText(list.get(i).getName());
//设置图片样式
RequestOptions options = new RequestOptions();
options.error(R.mipmap.ic_launcher);
//Glide依赖
Glide.with(con)
.load(list.get(i).getIcon())
.apply(options)
.into(homeViewHolder.im);
}
@Override
public int getItemCount() {
return home.getData().size();
}
//找到id绑定数据
class HomeViewHolder extends RecyclerView.ViewHolder{
ImageView im;
TextView protv;
public HomeViewHolder(@NonNull View itemView) {
super(itemView);
im=itemView.findViewById(R.id.image);
protv=itemView.findViewById(R.id.t2);
}
}
}
Model类
package com.bwei.xzh.Lish.model;
import com.bwei.xzh.network.Okhttp;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class ZsModelInter implements ZsModelImpl{
@Override
public void getDate(String url, final Cteanview cteanview) {
Okhttp.getInstance().getHttpGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
cteanview.Fuils();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
cteanview.Succes(response.body().string());
}
});
}
}
Model接口
package com.bwei.xzh.Lish.model;
public interface ZsModelImpl {
//获取数据
public void getDate(String url, Cteanview cteanview);
//定义接口
interface Cteanview{
void Succes(String data);
void Fuils();
}
}
Presenter类
package com.bwei.xzh.Lish.presenter;
import com.bwei.xzh.Lish.model.ZsModelImpl;
import com.bwei.xzh.Lish.model.ZsModelInter;
import com.bwei.xzh.api.Api;
import com.bwei.xzh.frag.ZhouFrag;
public class ZsPresenterInter implements ZsPresenterImpl{
private final ZsModelInter zsModelInter;
ZhouFrag mView;
//构造方法
public ZsPresenterInter(ZhouFrag mainActivity){
this.mView=mainActivity;
zsModelInter = new ZsModelInter();
};
@Override
public void getModel(String url) {
zsModelInter.getDate(Api.Shou, new ZsModelImpl.Cteanview() {
@Override
public void Succes(String data) {
mView.getViewData(data);
}
@Override
public void Fuils() {
}
});
}
}
Presenter接口
package com.bwei.xzh.Lish.presenter;
public interface ZsPresenterImpl {
public void getModel(String data);
}
View接口
package com.bwei.xzh.Lish.view;
public interface ZsViewImpl {
//返回数据
public void getViewData(String getView);
}
当前的activity
package com.bwei.xzh.frag;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
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 com.bwei.xzh.Lish.presenter.ZsPresenterInter;
import com.bwei.xzh.Lish.view.ZsViewImpl;
import com.bwei.xzh.R;
import com.bwei.xzh.adapter.HomeAdapter;
import com.bwei.xzh.adapter.LiAdapter;
import com.bwei.xzh.api.Api;
import com.bwei.xzh.bean.Bean;
import com.google.gson.Gson;
import java.util.ArrayList;
public class ZhouFrag extends Fragment implements ZsViewImpl {
private RecyclerView mls,ks;
private Gson gson;
private Bean mData;
private ZsPresenterInter presenterInter;
private HomeAdapter homeAdapter;
private LinearLayoutManager linearLayoutManager;
private GridLayoutManager gridLayoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.zhou, container,false);
mls= view.findViewById(R.id.mls);
ks= view.findViewById(R.id.ks);
//列表样式
linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
//网格样式
gridLayoutManager = new GridLayoutManager(getActivity(), 3);
ks.setLayoutManager(gridLayoutManager);
mls.setLayoutManager(linearLayoutManager);
//假数据
ArrayList<String> list = new ArrayList<>();
list.add("京东超市");list.add("全球购");list.add("女装");list.add("全球购");list.add("男鞋");list.add("女鞋");list.add("美肤宝");list.add("岛国");list.add("全球购");
list.add("爱他");
list.add("美特斯邦威");
LiAdapter listAdapter = new LiAdapter(getActivity(),list);
mls.setAdapter(listAdapter);
//调用P层
presenterInter = new ZsPresenterInter(this);
ks.addItemDecoration(new DividerItemDecoration(getActivity(),
DividerItemDecoration.VERTICAL));
presenterInter.getModel(Api.Shou);
return view;
}
//网络请求数据
@Override
public void getViewData(String getView) {
gson = new Gson();
mData = gson.fromJson(getView,Bean.class);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
homeAdapter = new HomeAdapter(getActivity(), mData);
ks.setAdapter(homeAdapter);
}
});
}
}