本文主要是介绍【BaseRVFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseRVFragment,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一个基于recycleview的fragment的封装,基于mvp,dagger2,retrofit,以及rxjava
简单用法
- 创建fragment继承BaseRVFragment
- 泛型传入Presenter,以及数据bean,实现View层接口(展示信息方法)
- 单列获取本类实例
- 更多用法参见具体项目
功能
封装要点
- 泛型传入Presenter继承BaseContract.BasePresenter(Presenter绑定与解绑),和数据bean对象。
- 继承BaseFragment()。
- 实现自己封装了的recycleview适配器加载更多,刷新和点击事件接口。
- dagger2创建presenter,attahView绑定和onDestroyView销毁presenter。
- initAdapter传入适配器及是否刷新加载更多。
- 重写加载跟多,刷新方法,加载错误的方法
源码
package com.daqsoft.baseproject.base;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import com.daqsoft.baseproject.R;
import com.daqsoft.baseproject.component.AppComponent;
import com.daqsoft.baseproject.view.recycleview.EasyRecyclerView;
import com.daqsoft.baseproject.view.recycleview.adapter.OnLoadMoreListener;
import com.daqsoft.baseproject.view.recycleview.adapter.RecyclerArrayAdapter;
import com.daqsoft.baseproject.view.recycleview.swipe.OnRefreshListener;
import java.lang.reflect.Constructor;
import javax.inject.Inject;
import butterknife.BindView;/*** Created by yanbo on 2017/5/26.* 基础BaseRecycleViewFragment* T2是数据类型*/public abstract class BaseRVFragment <T1 extends BaseContract.BasePresenter,T2> extends BaseFragment implements OnLoadMoreListener,OnRefreshListener,RecyclerArrayAdapter.OnItemClickListener{//控制器@Injectprotected T1 mPresenter;//自己的recycleview@BindView(R.id.recyclerview)protected EasyRecyclerView mRecyclerView;protected RecyclerArrayAdapter<T2> mAdapter;protected int start = 0;protected int limit = 20;/*** [此方法不可再重写]**/@Overridepublic void attachView() {if (mPresenter !=null){mPresenter.attachView(this);}}protected void initAdapter(boolean refreshable, boolean loadmoreable) {if (mRecyclerView != null) {mRecyclerView.setLayoutManager(new LinearLayoutManager(getSupportActivity()));mRecyclerView.setItemDecoration(ContextCompat.getColor(activity, R.color.common_divider_narrow), 1, 0, 0);mRecyclerView.setAdapterWithProgress(mAdapter);}if (mAdapter != null) {mAdapter.setOnItemClickListener(this);mAdapter.setError(R.layout.common_error_view).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {mAdapter.resumeMore();}});if (loadmoreable) {mAdapter.setMore(R.layout.common_more_view, this);mAdapter.setNoMore(R.layout.common_nomore_view);}if (refreshable && mRecyclerView != null) {mRecyclerView.setRefreshListener(this);}}}protected void initAdapter(Class<? extends RecyclerArrayAdapter<T2>> clazz, boolean refreshable, boolean loadmoreable) {mAdapter = (RecyclerArrayAdapter<T2>) createInstance(clazz);initAdapter(refreshable, loadmoreable);}public Object createInstance(Class<?> cls) {Object obj;try {Constructor c1 = cls.getDeclaredConstructor(Context.class);c1.setAccessible(true);obj = c1.newInstance(mContext);} catch (Exception e) {obj = null;}return obj;}@Overridepublic void onLoadMore() {}@Overridepublic void onRefresh() {mRecyclerView.setRefreshing(true);}protected void loaddingError() {if (mAdapter.getCount() < 1) { // 说明缓存也没有加载,那就显示errorview,如果有缓存,即使刷新失败也不显示errormAdapter.clear();}mAdapter.pauseMore();mRecyclerView.setRefreshing(false);mRecyclerView.showTipViewAndDelayClose("似乎没有网络哦");}@Overridepublic void onDestroyView() {super.onDestroyView();if (mPresenter != null)mPresenter.detachView();}}
这篇关于【BaseRVFragment】基于MVP+Dagger2+Retrofit+Rxjava框架之BaseRVFragment的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!