以下是Android简单封装一个MVP基类的流程:
第一步:创建BaseView和BasePresenter接口
BaseView接口中定义了通用的UI操作方法,例如显示进度条、隐藏进度条、显示错误信息等等。BasePresenter接口定义了通用的数据获取方法,例如获取数据、刷新数据等等。
下面是示例代码:
public interface BaseView {
void showLoading();
void hideLoading();
void showError(String error);
}
public interface BasePresenter<T extends BaseView> {
void attachView(T view);
void detachView();
}
第二步:创建BaseActivity和BasePresenterImpl类
创建BaseActivity类,并实现BaseView接口,在其中定义通用的UI操作方法。其中需要将UI操作交给BasePresenterImpl类处理,BasePresenterImpl实现了BasePresenter接口,将具体的数据获取方法交给了子类实现。
下面是示例代码:
public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {
protected T mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
mPresenter = createPresenter();
mPresenter.attachView(this);
initView();
}
@Override
protected void onDestroy() {
super.onDestroy();
mPresenter.detachView();
}
protected abstract int getLayoutId();
protected abstract T createPresenter();
protected abstract void initView();
@Override
public void showLoading() {
// 显示进度条
}
@Override
public void hideLoading() {
// 隐藏进度条
}
@Override
public void showError(String error) {
// 显示错误信息
}
}
public abstract class BasePresenterImpl<T extends BaseView> implements BasePresenter<T> {
protected T mView;
@Override
public void attachView(T view) {
mView = view;
}
@Override
public void detachView() {
mView = null;
}
}
第三步:创建具体的Presenter
创建具体的Presenter类,继承自BasePresenterImpl,并实现具体的数据获取方法。例如,以下是获取天气信息的Presenter示例代码:
public class WeatherPresenter extends BasePresenterImpl<WeatherView> {
public void getWeather(String city) {
mView.showLoading();
// 发送网络请求获取天气信息
mView.hideLoading();
mView.showWeatherInfo(weatherInfo);
}
}
第四步:创建具体的View
创建具体的View类,继承自BaseActivity,并实现具体的UI操作方法。例如,以下是显示天气信息的View示例代码:
public class WeatherActivity extends BaseActivity<WeatherPresenter> implements WeatherView {
@Override
protected int getLayoutId() {
return R.layout.activity_weather;
}
@Override
protected WeatherPresenter createPresenter() {
return new WeatherPresenter();
}
@Override
protected void initView() {
// 初始化UI控件
}
@Override
public void showWeatherInfo(WeatherInfo weatherInfo) {
// 显示天气信息
}
}
示例1:获取天气信息
现在我们已经完成了MVP基类的封装,接下来就可以用具体的示例来演示如何获取天气信息。
首先,我们需要在WeatherPresenter中添加获取天气信息的方法:
public class WeatherPresenter extends BasePresenterImpl<WeatherView> {
public void getWeather(String city) {
mView.showLoading();
// 发送网络请求获取天气信息
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.weather.com.cn/")
.addConverterFactory(GsonConverterFactory.create())
.build();
WeatherService weatherService = retrofit.create(WeatherService.class);
Call<WeatherInfo> call = weatherService.getWeather(city);
call.enqueue(new Callback<WeatherInfo>() {
@Override
public void onResponse(Call<WeatherInfo> call, Response<WeatherInfo> response) {
mView.hideLoading();
mView.showWeatherInfo(response.body());
}
@Override
public void onFailure(Call<WeatherInfo> call, Throwable t) {
mView.hideLoading();
mView.showError("获取天气信息失败:" + t.getMessage());
}
});
}
}
然后,在WeatherActivity中调用getWeather方法,例如在按钮的点击事件中:
public class WeatherActivity extends BaseActivity<WeatherPresenter> implements WeatherView {
@BindView(R.id.city_edit_text)
EditText cityEditText;
@BindView(R.id.search_button)
Button searchButton;
@Override
protected int getLayoutId() {
return R.layout.activity_weather;
}
@Override
protected WeatherPresenter createPresenter() {
return new WeatherPresenter();
}
@Override
protected void initView() {
ButterKnife.bind(this);
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city = cityEditText.getText().toString();
mPresenter.getWeather(city);
}
});
}
...
}
示例2:获取新闻列表
接下来,我们使用另外一个示例演示如何获取新闻列表。
首先,我们需要创建NewsPresenter类,实现具体的数据获取方法:
public class NewsPresenter extends BasePresenterImpl<NewsView> {
public void getNewsList(int pageIndex) {
mView.showLoading();
// 发送网络请求获取新闻列表
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.xxx.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
NewsService newsService = retrofit.create(NewsService.class);
Call<List<News>> call = newsService.getNewsList(pageIndex);
call.enqueue(new Callback<List<News>>() {
@Override
public void onResponse(Call<List<News>> call, Response<List<News>> response) {
mView.hideLoading();
mView.showNewsList(response.body());
}
@Override
public void onFailure(Call<List<News>> call, Throwable t) {
mView.hideLoading();
mView.showError("获取新闻列表失败:" + t.getMessage());
}
});
}
}
然后,在NewsActivity中调用getNewsList方法,例如在onCreate方法中:
public class NewsActivity extends BaseActivity<NewsPresenter> implements NewsView {
private RecyclerView mRecyclerView;
private NewsAdapter mNewsAdapter;
@Override
protected int getLayoutId() {
return R.layout.activity_news;
}
@Override
protected NewsPresenter createPresenter() {
return new NewsPresenter();
}
@Override
protected void initView() {
mRecyclerView = findViewById(R.id.recycler_view);
mNewsAdapter = new NewsAdapter(this);
mRecyclerView.setAdapter(mNewsAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mPresenter.getNewsList(0);
}
@Override
public void showNewsList(List<News> newsList) {
mNewsAdapter.setNewsList(newsList);
}
...
}
以上就是Android简单封装一个MVP基类的完整攻略,希望对您有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android简单封装一个MVP基类流程详解 - Python技术站