两个recyclerview+mvp

效果图: 两个recyclerview
两个recyclerview+mvp_第1张图片
mycallback里面写

public interface MyCallBack {
    void setData(T data);

    void setError(T error);
}

model接口

public interface Model  {
    void getData(String url,int typr,MyCallBack callBack);
}

model接口的实现类

public class ModelImpl implements Model {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String s = (String) msg.obj;
            Gson gson = new Gson();
            switch (type){
                case 1:
                    MyTopBean topBean = gson.fromJson(s, MyTopBean.class);
                    myCallBack.setData(topBean);
                    break;
                case 2:
                    BottomBean bottomBean = gson.fromJson(s, BottomBean.class);
                    myCallBack.setData(bottomBean);
                    break;
            }
        }
    };

    private int type;
    private MyCallBack myCallBack;

    @Override
    public void getData(String url,int type,MyCallBack callBack) {
        this.type = type;
        this.myCallBack = callBack;
        OkHttp.getInstance().getAsync(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                handler.sendMessage(handler.obtainMessage(0,string));
            }
        });
    }
}

view层

public interface IView {
    void getData(T data);

    void getError(T error);
}

presences层接口

public interface Presenter {
    void startRequest(String url,int type);
}

p层两个实现类

public class Presenter2  implements Presenter {
    private ModelImpl model;
    private IView iView;

    public Presenter2(IView iView) {
        this.iView = iView;
        model = new ModelImpl();
    }


    @Override
    public void startRequest(String url, final int type) {
        model.getData(url, type, new MyCallBack() {
            @Override
            public void setData(Object data) {
                switch (type){
                    case 2:
                        iView.getData(data);
                        break;
                }
            }

            @Override
            public void setError(Object error) {

            }
        });
    }

第二个

public class PresenterImpl implements Presenter {

    private ModelImpl model;
    private IView iView;

    public PresenterImpl(IView iView) {
        this.iView = iView;
        model = new ModelImpl();
    }

    @Override
    public void startRequest(String url, final int type) {
        model.getData(url, type, new MyCallBack() {
            @Override
            public void setData(Object data) {
                switch (type){
                    case 1:
                        iView.getData(data);
                        break;
                }
            }

            @Override
            public void setError(Object error) {

            }
        });
    }

fragment里面

public class PlusOneFragment extends Fragment implements IView{

    private RecyclerView recy1;
    private RecyclerView recy2;
    private String url = "https://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1";
    private String url2 = "http://www.xieast.com/api/news/news.php?page=1%22";
    private List list = new ArrayList<>();
    private List mList = new ArrayList<>();
    private TopAdapter topAdapter;
    private BottomAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_plus_one, container, false);
        initView(view);
        PresenterImpl presenter = new PresenterImpl(this);
        presenter.startRequest(url,1);

        Presenter2 presenter2 = new Presenter2(this);
        presenter2.startRequest(url2,2);
        return view;
    }


    private void initView(View view) {
        recy1 = (RecyclerView) view.findViewById(R.id.recy1);
        recy2 = (RecyclerView) view.findViewById(R.id.recy2);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        recy1.setLayoutManager(gridLayoutManager);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        recy2.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void getData(Object data) {
        if(data instanceof MyTopBean){
            MyTopBean data1 = (MyTopBean) data;
            list.addAll(data1.getResults());
            topAdapter = new TopAdapter(list, getContext());
            recy1.setAdapter(topAdapter);
            topAdapter.notifyDataSetChanged();
        }

        if(data instanceof BottomBean){
            BottomBean  data1 = (BottomBean) data;
            BottomBean bottomBean = new BottomBean();
            mList.addAll(data1.getData());
            adapter = new BottomAdapter(mList,getActivity());
            recy2.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getError(Object error) {

    }
}

你可能感兴趣的:(Android)