基于谷歌todo-mvp写的例子

来源:互联网 发布:使命召唤ol宏数据 编辑:程序博客网 时间:2024/06/14 19:36

基于google todo-mvp写的例子

例子需求很简单,就是请求网络数据,然后以列表的形式显示在页面中,效果如下图:
这里写图片描述

代码结构图

1.基类

BasePresenter.java

public interface BasePresenter {    void subscribe();    void unsubscribe();}

BaseView.java

public interface BaseView<T> {    void setPresenter(T presenter);}

2.数据

1.数据模型类TopListOfAll,直接采用GsonFormat生成(getter、setter去掉了)

public class TopListOfAll {    private int totalCount;    private int pageCount;    private List<TopListsBean> topLists;    public static class TopListsBean {        private int id;        private String topListNameCn;        private String topListNameEn;        private int type;        private String summary;    }}

2.TopListDataSource.java,对数据操作的接口,这里只是一个简单的例子,所以就只有请求数据一个操作

public interface TopListDataSource {    Observable<TopListOfAll> getTopList(int pageIndex);}

具体实现类:

public class TopListRemoteDataSource implements TopListDataSource {    private static TopListRemoteDataSource INSTANCE;    public static TopListRemoteDataSource getInstance() {        if (INSTANCE == null) {            INSTANCE = new TopListRemoteDataSource();        }        return INSTANCE;    }    // Prevent direct instantiation.    private TopListRemoteDataSource() {    }    @Override    public Observable<TopListOfAll> getTopList(int pageIndex) {        return ApiManage.getInstence().getApiService().getTopListOfAll(pageIndex);    }}

3.Activity、Contract、Fragment

接下来就是重要部分了,主要是ShowContract(契约类)、ShowActivity、ShowFragment、ShowPresenter

1.ShowContract.java,将View的动作和Presenter的动作写在一起一目了然

public interface ShowContract {    interface View extends BaseView<Presenter>{        void setLoadingProgress(boolean isShowProgressBar);        void showTopList(TopListOfAll topList);        void showLoadingTopListError(String msg);    }    interface Presenter extends BasePresenter{        void addTopList();    }}

2.ShowActivity.java,主要负责创建一个fragment和presenter,所有内容均在fragment中实现,activity相当于一个管理者

public class ShowActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_show);        ShowFragment showFragment = (ShowFragment) getSupportFragmentManager()                .findFragmentById(R.id.contentFrame);        if (showFragment == null){            showFragment = ShowFragment.newInstance("");            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),                    showFragment,R.id.contentFrame);        }        // Create the presenter        new ShowPresenter(TopListRemoteDataSource.getInstance(),showFragment);    }}

ShowFragment.java

public class ShowFragment extends Fragment implements ShowContract.View{    private ShowContract.Presenter mPresenter;    private TopListAdapter mTopListAdapter;    private ProgressBar mProgressBar;    private TextView loadFailedView;    private TextView noDataView;    public static ShowFragment newInstance(@Nullable String args){        Bundle arguments = new Bundle();        arguments.putString("args",args);        ShowFragment showFragment = new ShowFragment();        showFragment.setArguments(arguments);        return showFragment;    }    @Override    public void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        mTopListAdapter = new TopListAdapter(getActivity(),null);    }    @Override    public void onResume() {        super.onResume();        mPresenter.subscribe();    }    @Override    public void onPause() {        super.onPause();        mPresenter.unsubscribe();    }    @Override    public void setPresenter(ShowContract.Presenter presenter) {        mPresenter = presenter;    }    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,                             @Nullable Bundle savedInstanceState) {        View root = inflater.inflate(R.layout.fragment_show, container, false);        RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.rv_toplist);        recyclerView.setHasFixedSize(true);        recyclerView.setLayoutManager(                new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));        recyclerView.setAdapter(mTopListAdapter);        recyclerView.addItemDecoration(                new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST));        mProgressBar = (ProgressBar) root.findViewById(R.id.progressBar);        loadFailedView = (TextView) root.findViewById(R.id.load_failed);        noDataView = (TextView) root.findViewById(R.id.load_no_data);        return root;    }    @Override    public void setLoadingProgress(boolean isShowProgressBar) {        if (isShowProgressBar){            mProgressBar.setVisibility(View.VISIBLE);        }else {            mProgressBar.setVisibility(View.GONE);        }    }    @Override    public void showTopList(TopListOfAll topList) {        if (topList.getTopLists() != null && topList.getTopLists().size() > 0){            loadFailedView.setVisibility(View.GONE);            noDataView.setVisibility(View.GONE);            mTopListAdapter.replaceData(topList.getTopLists());        }else {            noDataView.setVisibility(View.VISIBLE);        }    }    @Override    public void showLoadingTopListError(String msg) {        loadFailedView.setVisibility(View.VISIBLE);        noDataView.setVisibility(View.GONE);        mProgressBar.setVisibility(View.GONE);    }}

ShowPresenter.java

public class ShowPresenter implements ShowContract.Presenter {    private TopListRemoteDataSource mTopListRemoteDataSource;    private ShowContract.View mShowView;    private final CompositeSubscription mSubscriptions;    public ShowPresenter(TopListRemoteDataSource topListRemoteDataSource,ShowContract.View showView) {        this.mTopListRemoteDataSource = topListRemoteDataSource;        this.mShowView = showView;        mSubscriptions = new CompositeSubscription();        mShowView.setPresenter(this);    }    @Override    public void subscribe() {        addTopList();    }    @Override    public void unsubscribe() {        mSubscriptions.clear();    }    @Override    public void addTopList() {        mShowView.setLoadingProgress(true);        mSubscriptions.add(mTopListRemoteDataSource                .getTopList(1)                .subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Observer<TopListOfAll>() {                    @Override                    public void onCompleted() {                        mShowView.setLoadingProgress(false);                    }                    @Override                    public void onError(Throwable e) {                        mShowView.showLoadingTopListError(e.getMessage());                    }                    @Override                    public void onNext(TopListOfAll topListOfAll) {                        mShowView.showTopList(topListOfAll);                    }                })        );    }}

本人是刚入门的菜鸟,以上是学习的笔记,若有错误或不足之处,欢迎指正~~
本文全部代码可以点这里:http://download.csdn.net/detail/u011797571/9714406

可以直接下载google官方Demo学习:https://github.com/googlesamples/android-architecture

1 0
原创粉丝点击