这篇文章仅仅是一个初识MVP的小白展示列表项目总结,关于MVP知识点本片文章先不谈,通过本篇文章能简单应用达到效果即可。
效果
image.png
复制粘贴即可
1、创建项目
2、创建包名
啥也不寻思先给他上一波包名(model、view、presenter、callback、api、bean、adapter),这里我用Recyclerview展示所以用到适配器。
image.png
3、给每个包创建相对应的java文件即可
image.png
4、看代码
(1)依赖
// recyclerview 依赖
implementation 'com.android.support:recyclerview-v7:29.0.2'
// 加载图片
implementation "com.github.bumptech.glide:glide:4.8.0"
//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.4.0'//Retrofit依赖
implementation 'com.squareup.retrofit2:converter-gson:2.4.0' // 可选依赖,解析json字符所用
//rxjava
implementation "io.reactivex.rxjava2:rxjava:2.1.3" // 必要rxjava2依赖
implementation "io.reactivex.rxjava2:rxandroid:2.0.1" // 必要rxandrroid依赖,切线程时需要用到
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' // 必要依赖,和Rxjava和Retrofit结合必须用到,下面会提到
(2)网络权限
(3)api
这里是get请求 ,post请求我还没资源(随后会更retrofit以及Rxjava的使用)
/**
* @auther Eerdunsang
* @date 2020/9/14
* @time 19:13
*/
public interface ShowServer {
// http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1 美食
String baseUrl = "http://gank.io/api/"; //福利
// String baseUrl = "http://www.qubaobei.com/";
//福利
@GET("data/%E7%A6%8F%E5%88%A9/20/18")
Observable getData();
// //美食
// @GET("ios/cf/dish_list.php?stage_id=1&limit=20&page=1")
// Observable getData();
}
(4)bean (自动生成)
测试接口 1 :http://gank.io/api/data/%E7%A6%8F%E5%88%A9/20/18
测试接口 2 :http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1
(5)callback
/**
* @auther Eerdunsang
* @date 2020/9/14
* @time 19:14
*/
public interface ShowCallBack {
void onSeccess(T t);
void onFail(F f);
}
(6)model
接口
/**
* @auther Eerdunsang
* @date 2020/9/14
* @time 19:25
*/
public interface ShowModel {
void getData(ShowCallBack callBack);
}
实现类
/**
* @auther Eerdunsang
* @date 2020/9/14
* @time 19:30
*/
public class ImpShowModel implements ShowModel {
@Override
public void getData(final ShowCallBack callBack) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(ShowServer.baseUrl)
.build();
//创建接口对象
ShowServer showServer = retrofit.create(ShowServer.class);
showServer.getData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ShowBean showBean) {
if (showBean != null) {
// if (showBean.getData().size() > 0 && showBean.getData() != null) { //美食
if (showBean.getResults().size() > 0 && showBean.getResults() != null) { //福利
callBack.onSeccess(showBean);
} else {
callBack.onFail("数据集合null");
}
} else {
Log.i("tag", "model实现类: " + "数据对象为null");
}
}
@Override
public void onError(Throwable e) {
Log.i("tag", "网络错误 Model实现类 :" + e.getMessage());
}
@Override
public void onComplete() {
}
});
}
}
(7)presenter
接口
/**
* @auther Eerdunsang
* @date 2020/9/14
* @time 19:28
*/
public interface ShowPrecenter {
void getData();
}
实现类
/**
* @auther Eerdunsang
* @date 2020/9/14
* @time 19:30
*/
public class ImpShowPrecenter implements ShowPrecenter, ShowCallBack {
private ShowView showView;
private ImpShowModel impShowModel;
public ImpShowPrecenter(ShowView showView, ImpShowModel impShowModel) {
this.showView = showView;
this.impShowModel = impShowModel;
}
@Override
public void getData() {
if (impShowModel != null) {
impShowModel.getData(this);
}
}
@Override
public void onSeccess(Object o) {
ShowBean showBean = (ShowBean) o;
showView.onSeccess(showBean);
}
@Override
public void onFail(Object o) {
showView.onFail(o);
}
}
(8)View
/**
* @auther Eerdunsang
* @date 2020/9/14
* @time 19:25
*/
public interface ShowView {
void onSeccess(T t);
void onFail(F f);
}
(9)adapter
没有写子条目的点击事件(XML 在下面往下翻)
/**
* @auther Eerdunsang
* @date 2020/9/14
* @time 19:46
*/
public class ShowAdapter extends RecyclerView.Adapter {
private Context context;
// private List datasBeans; // 美食
private List datasBeans; // 福利
public ShowAdapter(Context context, List datasBeans) {
this.context = context;
this.datasBeans = datasBeans;
}
@NonNull
@Override
public MyShow onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyShow(LayoutInflater.from(context).inflate(R.layout.show_tab, parent, false));
}
@Override
public void onBindViewHolder(@NonNull MyShow holder, int position) {
Log.i("tag", "onBindViewHolder: "+datasBeans.size());
// ShowBean.DataBean resultsBean = datasBeans.get(position); //美食
ShowBean.ResultsBean resultsBean = datasBeans.get(position);
// Glide.with(context).load(resultsBean.getPic()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.show);//美食
Glide.with(context).load(resultsBean.getUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(holder.show);//福利
// holder.text.setText(resultsBean.getFood_str());//美食
holder.text.setText(resultsBean.getType());//福利
}
@Override
public int getItemCount() {
return datasBeans.size();
}
public class MyShow extends RecyclerView.ViewHolder {
private ImageView show;
private TextView text;
public MyShow(@NonNull View view) {
super(view);
show = view.findViewById(R.id.show_image);
text = view.findViewById(R.id.show_tv);
}
}
}
(10)MainActivity
public class MainActivity extends AppCompatActivity implements ShowView {
private ImpShowPrecenter impShowPrecenter;
// private List showBeans = new ArrayList<>();//美食
private List showBeans = new ArrayList<>();//福利
private RecyclerView showRlv;
private ShowAdapter adapter;
private LinearLayout ll;
private ImageView headIv;
private TextView nameTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
impShowPrecenter = new ImpShowPrecenter(this, new ImpShowModel());
impShowPrecenter.getData();
}
private void initView() {
showRlv = (RecyclerView) findViewById(R.id.show_rlv);
ll = (LinearLayout) findViewById(R.id.ll);
headIv = (ImageView) findViewById(R.id.head_iv);
nameTv = (TextView) findViewById(R.id.name_tv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
showRlv.setLayoutManager(linearLayoutManager);
adapter = new ShowAdapter(MainActivity.this, showBeans);
showRlv.setAdapter(adapter);
}
@Override
public void onSeccess(Object o) {
ShowBean showBean = (ShowBean) o;
// List results = showBean.getData();//美食
// showBeans.addAll(results);//美食
// Glide.with(this).load(results.get(0).getPic()).into(headIv);//美食
// nameTv.setText(results.get(0).getFood_str());//美食
List results = showBean.getResults();//福利
showBeans.addAll(results);//福利
Glide.with(this).load(results.get(0).getUrl()).into(headIv);//福利
nameTv.setText(results.get(0).getType());//福利
adapter.notifyDataSetChanged();
}
@Override
public void onFail(Object o) {
String error = (String) o;
Log.i("tag", "MainActivity onFail: " + error);
}
}
(11)XML
activity
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:layout_below="@id/ll"
android:id="@+id/show_rlv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:id="@+id/ll"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
android:id="@+id/head_iv"
android:src="@drawable/ic_launcher_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
android:id="@+id/name_tv"
android:text="name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
adapter
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:id="@+id/show_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_launcher_foreground" />
android:id="@+id/show_tv"
android:layout_marginTop="30dp"
android:layout_alignTop="@id/show_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/show_image"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/show_image"
android:gravity="center"
android:text="你好世界" />
有哪些地方不懂的留言,在这里着重说下一个问题。现在手机基本都是9.0以上的所以运行代码之前还得配置清单文件,不然代码没问题,数据请求不出来,好的话项目可以运行但是白板,坏的直接运行不起来。配置环境我也写了,参考 :https://www.jianshu.com/p/05c43ea2314b