MVP
步骤
1、第一步先将数据实体类写出来(Modle层)
public class Bean {
/**
* ret : 1
* data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1669","food_str":"大虾 葱 生姜 植物油 料酒","num":1669},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1591","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1591},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1544","food_str":"QQ糖 牛奶 芒果","num":1544},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1425","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1425},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1419","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1419},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1341","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1341},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1252","food_str":"银耳 苹果 红糖","num":1252},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1221","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1221},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1151","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1151},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1144","food_str":"红薯粉 肉 姜 蒜 花椒","num":1144},{"id":"2262","title":"糖醋肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg","collect_num":"1078","food_str":"猪肉 红椒 黄椒 洋葱 蛋清","num":1078},{"id":"9971","title":"鱼香豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg","collect_num":"1010","food_str":"豆腐 木耳 胡萝卜 香葱 番茄酱","num":1010},{"id":"10172","title":"干煸四季豆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg","collect_num":"993","food_str":"四季豆 干辣椒 蒜头 酱油 糖","num":993},{"id":"2685","title":"胡萝卜肉末蒸蛋","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg","collect_num":"926","food_str":"胡萝卜 肉 蛋 生抽 盐","num":926},{"id":"9972","title":"虎皮青椒","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg","collect_num":"892","food_str":"青辣椒 大蒜 香醋 白糖 生抽","num":892},{"id":"10437","title":"叉烧排骨","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg","collect_num":"803","food_str":"排骨 李锦记叉烧酱 植物油 清水 油菜","num":803},{"id":"2892","title":"\u201c五行\u201d彩蔬汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg","collect_num":"760","food_str":"黑木耳 玉米 牛蒡 胡萝卜 西兰花","num":760},{"id":"2348","title":"麻辣肉丝面","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg","collect_num":"758","food_str":"面条 肉丝 淀粉 酱油 辣椒","num":758},{"id":"33783","title":"美人豆浆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg","collect_num":"757","food_str":"黄豆 红豆 绿豆 黑豆 黑米","num":757},{"id":"10044","title":"土豆炖翅根","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg","collect_num":"756","food_str":"土豆 翅根 葱 姜 料酒","num":756}]
*/
private int ret;
private List<DataBean> data;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* id : 8289
* title : 油焖大虾
* pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
* collect_num : 1669
* food_str : 大虾 葱 生姜 植物油 料酒
* num : 1669
*/
private String id;
private String title;
private String pic;
private String collect_num;
private String food_str;
private int num;
public DataBean(String title, String pic) {
this.title = title;
this.pic = pic;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getCollect_num() {
return collect_num;
}
public void setCollect_num(String collect_num) {
this.collect_num = collect_num;
}
public String getFood_str() {
return food_str;
}
public void setFood_str(String food_str) {
this.food_str = food_str;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
@Override
public String toString() {
return "DataBean{" +
"id='" + id + '\'' +
", title='" + title + '\'' +
", pic='" + pic + '\'' +
", collect_num='" + collect_num + '\'' +
", food_str='" + food_str + '\'' +
", num=" + num +
'}';
}
}
@Override
public String toString() {
return "Bean{" +
"ret=" + ret +
", data=" + data +
'}';
}
}
2、写一个MainMolde用来传输下载成功的字符串
public interface MainModle {
void success(Bean count);
}
3、写网络下载 Retrofit+RxJava 用来下载数据
public class DataModle {
private String url = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=40&page=1";
StringsInterface stringsInterface;
public DataModle(StringsInterface stringsInterface) {
this.stringsInterface = stringsInterface;
}
public void getData(){
Retrofit.Builder builder = new Retrofit.Builder();
RetrofitInterface anInterface = builder.baseUrl("http://www.qubaobei.com/ios/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(new OkHttpClient())
.build().create(RetrofitInterface.class);
anInterface.ob(url).subscribeOn(Schedulers.io()).subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bean>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Bean bean) {
stringsInterface.Strings(bean);
}
});
}
}
4、写一个modle层与presenter之间的接口
public interface StringsInterface {
void Strings(Bean count);
}
5、接下来就要写Presenter层
public class IPresenter implements StringsInterface {
DataModle dataModle = new DataModle(this);
MainModle mainModle;
public IPresenter(MainModle mainModle) {
this.mainModle = mainModle;
}
public void getData(){
dataModle.getData();
}
@Override
public void Strings(Bean count) {
mainModle.success(count);
}
}
6、写一个Retrofit用来下载的接口
public interface RetrofitInterface {
@GET
Observable<Bean> ob(@Url String url);
}
7、最后写View层,MainActivity 去实现MainModle来拿到modle和presenter下载的数据
public class MainActivity extends AppCompatActivity implements MainModle {
private RecyclerView recyclerView;
private List<Bean> list = new ArrayList<>();
private adapter adapters;
private IPresenter iPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
iPresenter = new IPresenter(this);
iPresenter.getData();
}
@Override
public void success(Bean count) {
List<Bean.DataBean> data = count.getData();
adapters = new adapter(this,data);
recyclerView.setAdapter(adapters);
}
}
MainActivity布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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=".view.activity.MainActivity"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
item行布局文件:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/imageView"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="20sp"/>
</LinearLayout>
适配器代码:
public class adapter extends RecyclerView.Adapter<adapter.MyViewHolder> {
private Context context;
private List<Bean.DataBean> list;
public adapter(Context context, List<Bean.DataBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
Glide.with(context).load(list.get(i).getPic()).into(myViewHolder.imageView);
myViewHolder.textView.setText(list.get(i).getTitle());
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
textView = itemView.findViewById(R.id.textView);
}
}
}