1. 程式人生 > >九宮格網路請求資料

九宮格網路請求資料

寫在fragment裡面
在這裡插入圖片描述在這裡插入圖片描述

R.layout.home_fragment.xml佈局

<GridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:numColumns="3"></GridView>
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>

HomePageFragment佈局(九宮格 資料請求)

package com.bw.meituanmoni.fragment;

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.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.bw.meituanmoni.R;
import com.bw.meituanmoni.adapter.HomeGridAdapter;
import com.bw.meituanmoni.adapter.HomeRecyAdapter;
import com.bw.meituanmoni.bean.Shop;
import com.bw.meituanmoni.bean.ShopBean;
import com.bw.meituanmoni.fristmvp.presenter.IPresenter;
import com.bw.meituanmoni.fristmvp.view.IView;

import java.util.ArrayList;
import java.util.List;

/**

  • Created by lenovo on 2018-12-19.
    */

public class HomePageFragment extends Fragment implements IView{
private GridView mGridView;
private RecyclerView recyview;
private List<ShopBean.DataBean> mList;
private List<Shop.DataBean> mBeanList;
private HomeGridAdapter mAdapter;
private HomeRecyAdapter mRecyAdapter;
private IPresenter mIPresenter;

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

    recyview=view.findViewById(R.id.recyview);
    mGridView=view.findViewById(R.id.grid_view);
    RecyclerView.LayoutManager manager=new LinearLayoutManager(getContext());
    recyview.setLayoutManager(manager);

    mList=new ArrayList<>();
    mBeanList=new ArrayList<>();
    mAdapter=new HomeGridAdapter(getContext(),mList);
    mRecyAdapter=new HomeRecyAdapter(getContext(),mBeanList);
    mGridView.setAdapter(mAdapter);
    recyview.setAdapter(mRecyAdapter);
    mIPresenter=new IPresenter();
    mIPresenter.attach(this);
    mIPresenter.getshop();
    mIPresenter.getShoprecy();
    return view;
}

@Override
public void getShops(List<ShopBean.DataBean> list) {
    if (list!=null){
        mList.clear();
        mList.addAll(list);
        mAdapter.notifyDataSetChanged();
    }
}

@Override
public void getRecy(List<Shop.DataBean> list) {
    if (list!=null){
        mBeanList.clear();
        mBeanList.addAll(list);
        mRecyAdapter.notifyDataSetChanged();
    }
}



@Override
public void onfailed(Exception e) {

}

}

HomeGridAdapter介面卡

package com.bw.meituanmoni.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bw.meituanmoni.R;
import com.bw.meituanmoni.bean.ShopBean;

import java.util.List;

/**

  • Created by lenovo on 2018-12-19.
    */

public class HomeGridAdapter extends BaseAdapter{
private Context mContext;
private List<ShopBean.DataBean> mList;

public HomeGridAdapter(Context context, List<ShopBean.DataBean> list) {
    mContext = context;
    mList = list;
}

@Override
public int getCount() {
    return mList.size();
}

@Override
public Object getItem(int position) {
    return mList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder=null;
    if (convertView==null){
        holder=new ViewHolder();
        convertView=  View.inflate(mContext, R.layout.item_img,null);
        holder.img=convertView.findViewById(R.id.img);
        convertView.setTag(holder);
    }else{
        holder= (ViewHolder) convertView.getTag();
    }
    Glide.with(mContext).load(mList.get(position).getIcon()).into(holder.img);
    return convertView;
}

class ViewHolder{
          ImageView img;
}

}

item_img

<?xml version="1.0" encoding="utf-8"?>



HomeRecyAdapter介面卡

package com.bw.meituanmoni.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bw.meituanmoni.R;
import com.bw.meituanmoni.bean.Shop;

import java.util.List;

/**

  • Created by lenovo on 2018-12-19.
    */

public class HomeRecyAdapter extends RecyclerView.Adapter<HomeRecyAdapter.ViewHolder>{
private Context mContext;
private List<Shop.DataBean> mList;

public HomeRecyAdapter(Context context, List<Shop.DataBean> list) {
    mContext = context;
    mList = list;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = View.inflate(mContext, R.layout.item_shop, null);
    ViewHolder holder=new ViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    Glide.with(mContext).load(mList.get(position).getPic_url()).into(holder.img);
    holder.title.setText(mList.get(position).getName());
}

@Override
public int getItemCount() {
    return mList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    private ImageView img;
    private TextView title;
    public ViewHolder(View itemView) {
        super(itemView);
        img=itemView.findViewById(R.id.img);
        title=itemView.findViewById(R.id.title);
    }
}

}

item_shop

<?xml version="1.0" encoding="utf-8"?>




HttpUtils

package com.bw.meituanmoni.utils;

import android.graphics.YuvImage;
import android.os.Handler;

import com.bw.meituanmoni.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 lenovo on 2018-12-19.
    */

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()
            .url(url)
            .get()
            .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 string=response.body().string();
            Gson gson=new Gson();
            final Object o=gson.fromJson(string,type);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    callBack.onsuccess(o);
                }
            });
        }
    });

}

}

首頁的mvp

IModel

package com.bw.meituanmoni.fristmvp.model;

import com.bw.meituanmoni.callback.ICallBack;
import com.bw.meituanmoni.utils.HttpUtils;

import java.lang.reflect.Type;

/**

  • Created by lenovo on 2018-12-19.
    */

public class IModel {
public void getShop(String url, ICallBack callBack, Type type){
HttpUtils.getInstance().get(url,callBack,type);
}
}

IView

package com.bw.meituanmoni.fristmvp.view;

import com.bw.meituanmoni.bean.Shop;
import com.bw.meituanmoni.bean.ShopBean;

import java.util.List;

/**

  • Created by lenovo on 2018-12-19.
    */

public interface IView {
void getShops(List<ShopBean.DataBean> list);
void getRecy(List<Shop.DataBean> list);
void onfailed(Exception e);

}

IPresenter

package com.bw.meituanmoni.fristmvp.presenter;

import com.bw.meituanmoni.bean.Shop;
import com.bw.meituanmoni.bean.ShopBean;
import com.bw.meituanmoni.callback.ICallBack;
import com.bw.meituanmoni.fristmvp.model.IModel;
import com.bw.meituanmoni.fristmvp.view.IView;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

/**

  • Created by lenovo on 2018-12-19.
    */

public class IPresenter {
private IView iv;
private IModel mIModel;

public void attach(IView iv){
this.iv=iv;
mIModel=new IModel();
}

public void getshop(){
Type type=new TypeToken(){}.getType();
mIModel.getShop(“http://www.zhaoapi.cn/product/getCatagory”, new ICallBack() {
@Override
public void onsuccess(Object o) {
ShopBean shopBean= (ShopBean) o;
if (shopBean!=null){
iv.getShops(shopBean.getData());
}
}

       @Override
       public void onfailed(Exception e) {

       }
   },type);

}
public void getShoprecy(){
Type type=new TypeToken(){}.getType();
mIModel.getShop(“http://www.wanandroid.com/tools/mockapi/6523/restaurants_offset_0_limit_4”, new ICallBack() {
@Override
public void onsuccess(Object o) {
Shop shop= (Shop) o;
if (shop!=null){
iv.getRecy(shop.getData());
}
}

       @Override
       public void onfailed(Exception e) {

       }
   },type);

}
}

ICallBack

package com.bw.meituanmoni.callback;

/**

  • Created by lenovo on 2018-12-19.
    */

public interface ICallBack {
void onsuccess(Object o);
void onfailed(Exception e);
}