FlyBanner+RecyclerView显示+MVP

HomeOkHttpUtils
public class HomeOkHttpUtils {
private Handler handler=new Handler();
private static HomeOkHttpUtils mInstance;
private OkHttpClient okHttpClient;

//私有构造
private HomeOkHttpUtils(){
    //日志拦截
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    okHttpClient = new OkHttpClient.Builder()
            //日志拦截
            .addInterceptor(interceptor)
            //读
            .readTimeout(5,TimeUnit.SECONDS)
            //写
            .writeTimeout(5,TimeUnit.SECONDS)
            //连接超时
            .connectTimeout(5,TimeUnit.SECONDS)
            .build();


}

//单例模式
public static HomeOkHttpUtils getmInstance() {
    if (mInstance==null){
        synchronized (HomeOkHttpUtils.class){
            if (mInstance==null){
                mInstance=new HomeOkHttpUtils();
            }
        }
    }
    return mInstance;
}

//post请求
public void doPost(HashMap<String,String> params,final OkHttpCallback okHttpCallback){
    //请求方法体
    FormBody.Builder formbody = new FormBody.Builder();
    for (Map.Entry<String,String> p:params.entrySet()){
        formbody.add(p.getKey(),p.getValue());
    }
    final RequestBody requestBody = formbody.build();
    Request request = new Request.Builder()
            .url(HomeApi.HOME_URL)
            .post(requestBody)
            .build();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            if (okHttpCallback!=null){
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        okHttpCallback.failure("网络异常了哟!");
                    }
                });
            }
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (requestBody!=null){
                if (response.code()==200){
                    final String resulr = response.body().string();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            okHttpCallback.success(resulr);
                        }
                    });
                }
            }
        }
    });
}

}

OkHttpCallback接口
public interface OkHttpCallback {
void success(String result);
void failure(String msg);
}

HomeContract契约类
public interface HomeCartContract {
public abstract class HomeCartPresenter{
public abstract void getHomeCart(HashMap<String,String> params);
}
interface IHomeCartModel{
void getHomeCart(HashMap<String, String> params, ICartModelCallback iCartModelCallback);
}
interface IHomeCartView{
void success(HomeBean.DataBean list);
void failure(String msg);
}
}

HomeModel
public class HomeModel implements HomeCartContract.IHomeCartModel {
private Handler handler=new Handler();
@Override
public void getHomeCart(HashMap<String, String> params, final ICartModelCallback callback) {
HomeOkHttpUtils.getmInstance().doPost(params, new OkHttpCallback() {
@Override
public void success(final String result) {
handler.post(new Runnable() {
@Override
public void run() {
callback.success(result);
}
});
}

        @Override
        public void failure(final String msg) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    callback.failure(msg);
                }
            });
        }
    });
}

}

IHomeCallback接口
public interface ICartModelCallback {
void success(String result);
void failure(String msg);
}

HomePresenter
public class HomePresenter extends HomeCartContract.HomeCartPresenter {
private HomeModel homeModel;
private HomeCartContract.IHomeCartView iHomeCartView;

public HomePresenter(HomeCartContract.IHomeCartView iHomeCartView) {
    homeModel=new HomeModel();
    this.iHomeCartView = iHomeCartView;
}

@Override
public void getHomeCart(HashMap<String, String> params) {
    homeModel.getHomeCart(params, new ICartModelCallback() {
        @Override
        public void success(String result) {
            if(iHomeCartView!=null){
                HomeBean homeBean = new Gson().fromJson(result, HomeBean.class);
                iHomeCartView.success(homeBean.data);
            }
        }

        @Override
        public void failure(String msg) {
            if (iHomeCartView!=null){
                iHomeCartView.failure(msg);
            }
        }
    });
}

}

HomeBean
public class HomeBean {

public String msg;
public String code;
public DataBean data;


public static class DataBean {


    public MiaoshaBean miaosha;
    public TuijianBean tuijian;
    public List<BannerBean> banner;
    public List<FenleiBean> fenlei;


    public static class MiaoshaBean {

        public String name;
        public String time;
        public List<ListBean> list;

        public static class ListBean {

            public String bargainPrice;
            public String createtime;
            public String detailUrl;
            public String images;
            public String itemtype;
            public String pid;
            public String price;
            public String pscid;
            public String salenum;
            public String sellerid;
            public String subhead;
            public String title;


        }
    }

    public static class TuijianBean {
        public String name;
        public List<ListBeanX> list;

        public static class ListBeanX {


            public String bargainPrice;
            public String createtime;
            public String detailUrl;
            public String images;
            public String itemtype;
            public String pid;
            public String price;
            public String pscid;
            public String salenum;
            public String sellerid;
            public String subhead;
            public String title;


        }
    }

    public static class BannerBean {

        public String aid;
        public String createtime;
        public String icon;
        public Object productId;
        public String title;
        public String type;
        public String url;

    }

    public static class FenleiBean {
        public String cid;
        public String createtime;
        public String icon;
        public String ishome;
        public String name;
    }
}

}

HomeAdapter

public class HomeFenLAdapter extends RecyclerView.Adapter<HomeFenLAdapter.MyVh> {
private Context context;
private List<HomeBean.DataBean.FenleiBean> list;

public HomeFenLAdapter(Context context, List<HomeBean.DataBean.FenleiBean> list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
public HomeFenLAdapter.MyVh onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view=LayoutInflater.from(context).inflate(R.layout.fenl_item_layout,viewGroup,false);
    return new MyVh(view);
}

@Override
public void onBindViewHolder(@NonNull HomeFenLAdapter.MyVh myVh, int i) {
    HomeBean.DataBean.FenleiBean fenleiBean = list.get(i);
    myVh.name.setText(fenleiBean.name);
    Glide.with(context).load(fenleiBean.icon).into(myVh.iv);

}

@Override
public int getItemCount() {
    return list==null?0:list.size();
}

public class MyVh extends RecyclerView.ViewHolder {

    private ImageView iv;
    private TextView name;

    public MyVh(@NonNull View itemView) {
        super(itemView);
        iv = itemView.findViewById(R.id.iv_cls_icon);
        name = itemView.findViewById(R.id.tv_cls_name);
    }
}

}

FragmentOne
public class FragmentOne extends Fragment implements HomeCartContract.IHomeCartView {
private FlyBanner fly;
private String[] mImagesUrl={
http://www.zhaoapi.cn/images/quarter/ad1.png”,
http://www.zhaoapi.cn/images/quarter/ad2.png”,
http://www.zhaoapi.cn/images/quarter/ad3.png”,
http://www.zhaoapi.cn/images/quarter/ad4.png”,
http://120.27.23.105/images/category/shop.png”,
http://120.27.23.105/images/category/qqg.png”,
http://120.27.23.105/images/category/phone.png
};
private RecyclerView clzz_rv;
private HomePresenter homePresenter;
private HomeFenLAdapter homeFenLAdapter;

/**
 * @param inflater
 * @param container
 * @param savedInstanceState
 * @return
 */

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmentone, container, false);
    initView(view);
    initBanner(view);

    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    initData();
}

private void initData() {
    homePresenter = new HomePresenter(this);
    homePresenter.getHomeCart(new HashMap<String, String>());
}

private void initBanner(View view) {
    fly = (FlyBanner) view.findViewById(R.id.fly);
    ArrayList<String> imageurl = new ArrayList<>();
    for (int i=0;i<mImagesUrl.length;i++){
        imageurl.add(mImagesUrl[i]);
    }
    fly.setImagesUrl(imageurl);
}


private void initView(View view) {
    fly = (FlyBanner) view.findViewById(R.id.fly);
    clzz_rv = view.findViewById(R.id.clzz_rv);

    clzz_rv.setLayoutManager(new GridLayoutManager(getActivity(),2,LinearLayoutManager.HORIZONTAL,false));


}

@Override
public void success(HomeBean.DataBean list) {

    homeFenLAdapter = new HomeFenLAdapter(getActivity(),list.fenlei);

// homeFenLAdapter.setData((List<HomeBean.DataBean.FenleiBean>) list);
clzz_rv.setAdapter(homeFenLAdapter);
}

@Override
public void failure(String msg) {

}

}

fragmentone主布局

<?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="#00ffe1"
    android:orientation="vertical">

        <com.recker.flybanner.FlyBanner
            android:id="@+id/fly"
            android:layout_width="match_parent"
            android:layout_height="200dp" />
        <android.support.v7.widget.RecyclerView
            android:id="@+id/clzz_rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

fenl_item_layout子布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:gravity="center"
    android:padding="5dp"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_cls_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/tv_cls_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="京东超市"
        android:layout_below="@+id/iv_cls_icon"/>

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值