1. 程式人生 > >兩個recyclerview+mvp

兩個recyclerview+mvp

效果圖: 兩個recyclerview
在這裡插入圖片描述
mycallback裡面寫

public interface MyCallBack<T> {
    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<T> {
    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<MyTopBean.ResultsBean> list = new ArrayList<>();
    private List<BottomBean.DataBean> 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) {

    }
}