RecyclerView结合mvp使用中关于adapter和Ipresenter的先后顺序

adapter初始化只会调用构造方法,newsIpresenter初始化只会调用构造方法,然后到调用newsIpresenter的subscribe()-->getNews(),因为获取数据是在Schedulers.io()线程中,........,-->subscribe()获取到数据-->NewsRecyclerAdapter中的onCreateViewHolder()和onBindViewHolder()

先看NewsFragment中,执行第7行只会初始化并不会调用adapter中的其他方法,第8行也是,执行第9行会调用newsIpresenter对象中的subscribe方法。
两种都可以:

1    @Override
2    public View onCreateView(LayoutInflater inflater, ViewGroup container,
3                             Bundle savedInstanceState) {
4        View view = inflater.inflate(R.layout.fragment_nba, container, false);
5        ButterKnife.bind(this, view);
6
7        adapter = new NewsRecyclerAdapter(getActivity());
8        newsIpresenter = new NewsIpresenterImpl(this);
9        newsIpresenter.subscribe();
10
11//        recyleView.setLayoutManager(new LinearLayoutManager(getActivity()));
12       recyleView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
13       recyleView.setAdapter(adapter);

        return view;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_nbalive, container, false);
        ButterKnife.bind(this, view);

        adapter = new NBALiveRecyclerAdapter(getActivity());
        ipresenter = new NBALiveIpresenterImpl(this);

//        recyleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyleView.setLayoutManager(new GridLayoutManager(getActivity(),2));
        recyleView.setAdapter(adapter);
        ipresenter.subscribe();

        return view;
    }

你可能感兴趣的:(RecyclerView结合mvp使用中关于adapter和Ipresenter的先后顺序)