展示数据使用:recyclerview,retrofit,greendao,butterknife,eventbus,fresco。实现效果图列表。MVP模式。

实现思路必须步骤。新框架愿各位同门进行学习,思路清析一下。

一,首先添加依赖。

compile 'com.squareup.retrofit2:converter-gson:2.3.0'

implementation 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'org.greenrobot:greendao:3.2.0'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'org.greenrobot:eventbus:3.1.1'
compile 'com.facebook.fresco:fresco:1.5.0'
二。添加网络权限;
  <uses-permission android:name="android.permission.INTERNET"/>
三。接下来就是类的创建。

      1,对对数据类的封装。创建bean类。

 

public class Bean {


    /**
     * code : 200
     * msg : success
     * data : {"category":[{"id":"16","cat_name":"按功效","is_leaf":"0","children":[{"id":"17","cat_name":"补水保湿","is_leaf":"1"},{"id":"31","cat_name":"舒缓修护","is_leaf":"1"},{"id":"19","cat_name":"控油去痘","is_leaf":"1"},{"id":"18","cat_name":"美白提亮","is_leaf":"1"},{"id":"20","cat_name":"紧致抗皱","is_leaf":"1"}]},{"id":"6","cat_name":"按属性","is_leaf":"0","children":[{"id":"38","cat_name":"面膜","is_leaf":"0"},{"id":"39","cat_name":"润肤水","is_leaf":"1"},{"id":"40","cat_name":"润肤乳","is_leaf":"1"},{"id":"24","cat_name":"洁面乳","is_leaf":"0"},{"id":"35","cat_name":"其他","is_leaf":"0"},{"id":"33","cat_name":"实惠套装","is_leaf":"1"},{"id":"9","cat_name":"贴式面膜","is_leaf":"0"},{"id":"10","cat_name":"睡眠面膜","is_leaf":"0"},{"id":"23","cat_name":"泥浆面膜","is_leaf":"0"},{"id":"41","cat_name":"男士","is_leaf":"0"}]},{"id":"11","cat_name":"按肤质","is_leaf":"0","children":[{"id":"14","cat_name":"混合性肤质","is_leaf":"1"},{"id":"13","cat_name":"中性肤质","is_leaf":"1"},{"id":"29","cat_name":"干性肤质","is_leaf":"1"},{"id":"28","cat_name":"油性肤质","is_leaf":"1"},{"id":"15","cat_name":"痘痘肤质","is_leaf":"1"},{"id":"37","cat_name":"敏感肤质","is_leaf":"0"}]}],"goodsBrief":[{"id":"121","goods_name":"经典款丨美白嫩肤面膜7片","shop_price":49.9,"market_price":99,"goods_img":"https://image.yunifang.com/yunifang/images/goods/121/goods_img/17062610568378169043195978.jpg","reservable":false,"efficacy":"美白嫩肤 层层葆鲜","stock_number":0,"restrict_purchase_num":0},{"id":"684","goods_name":"解救肌渴丨花粹水润面膜套装10片","shop_price":29.9,"market_price":139,"goods_img":"https://image.yunifang.com/yunifang/images/goods/684/goods_img/17062610401397749701177609.jpg","reservable":false,"efficacy":"水润充盈 鲜嫩少女肌","stock_number":0,"restrict_purchase_num":0},{"id":"772","goods_name":"热销爆款丨清润莹亮黑膜套装21片","shop_price":99.9,"market_price":297,"goods_img":"https://image.yunifang.com/yunifang/images/goods/772/goods_img/171016095389114258195255761.jpg","reservable":false,"efficacy":"自然莹亮  水感瓷肌","stock_number":0,"restrict_purchase_num":0},{"id":"5","goods_name":"亮采金桂花眼膜贴60片","shop_price":69,"market_price":129,"goods_img":"https://image.yunifang.com/yunifang/images/goods/5/goods_img/17103118031591202108007689.jpg","reservable":false,"efficacy":"补水靓眼 改善熊猫眼","stock_number":0,"restrict_purchase_num":0},{"id":"343","goods_name":"美白嫩肤润肤水150ml","shop_price":69,"market_price":119,"goods_img":"https://image.yunifang.com/yunifang/images/goods/343/goods_img/170626103423611420000294568.jpg","reservable":false,"efficacy":"补水保湿 美白嫩肤","stock_number":0,"restrict_purchase_num":0},{"id":"9","goods_name":"玫瑰滋养矿物睡眠面膜180g","shop_price":59.9,"market_price":99,"goods_img":"https://image.yunifang.com/yunifang/images/goods/9/goods_img/170626112611018894167156705.jpg","reservable":false,"efficacy":"镇店之宝 彻夜补水","stock_number":0,"restrict_purchase_num":0},{"id":"10","goods_name":"美白嫩肤睡眠面膜180g","shop_price":69,"market_price":119,"goods_img":"https://image.yunifang.com/yunifang/images/goods/10/goods_img/170626112190919312919673075.jpg","reservable":false,"efficacy":"水润一夜 美白透亮","stock_number":0,"restrict_purchase_num":0},{"id":"446","goods_name":"芦荟补水保湿凝胶150g","shop_price":49.9,"market_price":89,"goods_img":"https://image.yunifang.com/yunifang/images/goods/446/goods_img/17112211433972519259384989.jpg","reservable":false,"efficacy":"滋养肌肤 自然水润","stock_number":0,"restrict_purchase_num":0},{"id":"16","goods_name":"玫瑰滋养保湿四件套","shop_price":169.9,"market_price":259.9,"goods_img":"https://image.yunifang.com/yunifang/images/goods/16/goods_img/17062611152592656236701367.jpg","reservable":false,"efficacy":"一整套源源补水","stock_number":0,"restrict_purchase_num":0},{"id":"8","goods_name":"全新升级丨美白嫩肤面膜20片","shop_price":129.9,"market_price":359,"goods_img":"https://image.yunifang.com/yunifang/images/goods/8/goods_img/170818182578717903477668239.jpg","reservable":false,"efficacy":"真美白 匠心造","stock_number":0,"restrict_purchase_num":0},{"id":"14","goods_name":"矿物泥浆鼻膜60g","shop_price":55,"market_price":69,"goods_img":"https://image.yunifang.com/yunifang/images/goods/14/goods_img/170626111762918631255244111.jpg","reservable":false,"efficacy":"收敛毛孔 改善黑头","stock_number":0,"restrict_purchase_num":0},{"id":"189","goods_name":"清爽平衡护肤三件套","shop_price":99.9,"market_price":179.9,"goods_img":"https://image.yunifang.com/yunifang/images/goods/189/goods_img/1706261046499486999388441.jpg","reservable":false,"efficacy":"深层清洁 平衡水油","stock_number":0,"restrict_purchase_num":0},{"id":"11","goods_name":"清爽平衡矿物泥浆面膜260g","shop_price":99,"market_price":99,"goods_img":"https://image.yunifang.com/yunifang/images/goods/11/goods_img/170626112085420599974158029.jpg","reservable":false,"efficacy":"口碑泥浆 清爽控油","stock_number":0,"restrict_purchase_num":0},{"id":"4","goods_name":"黑玫瑰矿物洁面乳100ml","shop_price":49,"market_price":59,"goods_img":"https://image.yunifang.com/yunifang/images/goods/4/goods_img/17062611262464209534577694.jpg","reservable":false,"efficacy":"温和清洁 提亮肤色","stock_number":0,"restrict_purchase_num":0},{"id":"559","goods_name":"竹炭净透矿物泥浆面膜110g","shop_price":59,"market_price":99,"goods_img":"https://image.yunifang.com/yunifang/images/goods/559/goods_img/170627164784010618075663103.jpg","reservable":false,"efficacy":"控油净肤 细腻毛孔","stock_number":0,"restrict_purchase_num":0}]}
     */

    private int code;
    private String msg;
    private DataBean data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public static class DataBean {
        private List<CategoryBean> category;
        private List<GoodsBriefBean> goodsBrief;

        public List<CategoryBean> getCategory() {
            return category;
        }

        public void setCategory(List<CategoryBean> category) {
            this.category = category;
        }

        public List<GoodsBriefBean> getGoodsBrief() {
            return goodsBrief;
        }

        public void setGoodsBrief(List<GoodsBriefBean> goodsBrief) {
            this.goodsBrief = goodsBrief;
        }

        public static class CategoryBean {
            /**
             * id : 16
             * cat_name : 按功效
             * is_leaf : 0
             * children : [{"id":"17","cat_name":"补水保湿","is_leaf":"1"},{"id":"31","cat_name":"舒缓修护","is_leaf":"1"},{"id":"19","cat_name":"控油去痘","is_leaf":"1"},{"id":"18","cat_name":"美白提亮","is_leaf":"1"},{"id":"20","cat_name":"紧致抗皱","is_leaf":"1"}]
             */

            private String id;
            private String cat_name;
            private String is_leaf;
            private List<ChildrenBean> children;

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public String getCat_name() {
                return cat_name;
            }

            public void setCat_name(String cat_name) {
                this.cat_name = cat_name;
            }

            public String getIs_leaf() {
                return is_leaf;
            }

            public void setIs_leaf(String is_leaf) {
                this.is_leaf = is_leaf;
            }

            public List<ChildrenBean> getChildren() {
                return children;
            }

            public void setChildren(List<ChildrenBean> children) {
                this.children = children;
            }

            public static class ChildrenBean {
                /**
                 * id : 17
                 * cat_name : 补水保湿
                 * is_leaf : 1
                 */

                private String id;
                private String cat_name;
                private String is_leaf;

                public String getId() {
                    return id;
                }

                public void setId(String id) {
                    this.id = id;
                }

                public String getCat_name() {
                    return cat_name;
                }

                public void setCat_name(String cat_name) {
                    this.cat_name = cat_name;
                }

                public String getIs_leaf() {
                    return is_leaf;
                }

                public void setIs_leaf(String is_leaf) {
                    this.is_leaf = is_leaf;
                }
            }
        }
    }
}
2.创建连接bean—— GoodsBriefBean 类。
public  class GoodsBriefBean {
    /**
     * id : 121
     * goods_name : 经典款丨美白嫩肤面膜7片
     * shop_price : 49.9
     * market_price : 99.0
     * goods_img : https://image.yunifang.com/yunifang/images/goods/121/goods_img/17062610568378169043195978.jpg
     * reservable : false
     * efficacy : 美白嫩肤 层层葆鲜
     * stock_number : 0
     * restrict_purchase_num : 0
     */
    @Id(autoincrement = true)
    private Long iid;
    private String id;
    private String goods_name;
    private double shop_price;
    private double market_price;
    private String goods_img;
    private boolean reservable;
    private String efficacy;
    private int stock_number;
    private int restrict_purchase_num;
    public GoodsBriefBean() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getGoods_name() {
        return goods_name;
    }

    public void setGoods_name(String goods_name) {
        this.goods_name = goods_name;
    }

    public double getShop_price() {
        return shop_price;
    }

    public void setShop_price(double shop_price) {
        this.shop_price = shop_price;
    }

    public double getMarket_price() {
        return market_price;
    }

    public void setMarket_price(double market_price) {
        this.market_price = market_price;
    }

    public String getGoods_img() {
        return goods_img;
    }

    public void setGoods_img(String goods_img) {
        this.goods_img = goods_img;
    }

    public boolean isReservable() {
        return reservable;
    }

    public void setReservable(boolean reservable) {
        this.reservable = reservable;
    }

    public String getEfficacy() {
        return efficacy;
    }

    public void setEfficacy(String efficacy) {
        this.efficacy = efficacy;
    }

    public int getStock_number() {
        return stock_number;
    }

    public void setStock_number(int stock_number) {
        this.stock_number = stock_number;
    }

    public int getRestrict_purchase_num() {
        return restrict_purchase_num;
    }

    public void setRestrict_purchase_num(int restrict_purchase_num) {
        this.restrict_purchase_num = restrict_purchase_num;
    }

    public Long getIid() {
        return this.iid;
    }

    public void setIid(Long iid) {
        this.iid = iid;
    }

    public boolean getReservable() {
        return this.reservable;
    }
}
3.创建参数Bean。

public class EventBean {
    public String url;
    public String title;

    public EventBean(String url, String title) {
        this.url = url;
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public String getTitle() {
        return title;
    }
}
4, IApplication初始化类的创建。

       

public class IApplication extends Application {

    public static IGetDataBase iGetDataBase;
    public static DaoSession session;
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://m.yunifang.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        iGetDataBase = retrofit.create(IGetDataBase.class);


        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"test1");

        Database database =  helper.getWritableDb();


        session = new DaoMaster(database).newSession();

    }
}
三,实现Model包中的类。

          1,第一步创建MainModel接口·

    public interface MainModel {

  }  
2,创建 MainModelImpl类。

public class MainModelImpl {
    /**
     * get 请求
     * @param callBack
     */
    public void getData(final ModelCallBack callBack){
        Call<Bean> call = IApplication.iGetDataBase.get("96333","bf3386e14fe5bb0bcef234baebca2414");
       call.enqueue(new Callback<Bean>() {
           @Override
           public void onResponse(Call<Bean> call, Response<Bean> response) {
               Bean bean = response.body();
               callBack.onSuccess(bean);

               IApplication.session.getGoodsBriefBeanDao().insertInTx(bean.getData().getGoodsBrief());
           }

           @Override
           public void onFailure(Call<Bean> call, Throwable t) {
             callBack.onFailure(new Exception(""));
           }
       });
    }
    /**
     * post 请求
     */
    public void  postData(final ModelCallBack callBack){
        Call<Bean> call = IApplication.iGetDataBase.get("96333","bf3386e14fe5bb0bcef234baebca2414");
        call.enqueue(new Callback<Bean>() {
            @Override

            public void onResponse(Call<Bean> call, Response<Bean> response) {
                Bean bean = response.body();
                callBack.onSuccess(bean);
            }

            @Override
            public void onFailure(Call<Bean> call, Throwable t) {
            callBack.onFailure(new Exception("") );
            }
        });

    }
}
3, 创建ModelCallBack接口。

public interface ModelCallBack {
    public void onSuccess(Bean bean);
    public void onFailure(Exception e);

   }
四。实现presenter包中的类。
    1,创建MainPresenter类。
public class MainPresenter {
    private IMainView iView ;
    private MainModelImpl mainModel ;
    public MainPresenter(IMainView view){
        this.iView = view;
        this.mainModel = new MainModelImpl();

    }
    public void get(){

        mainModel.getData(new ModelCallBack() {
            @Override
            public void onSuccess(Bean bean) {

                if(iView != null){
                    iView.onSuccess(bean);
                }
            }

            @Override
            public void onFailure(Exception e) {
                if(iView != null){
                    iView.onFailure(e);
                }
            }
        });
    }

    public void post(){
        mainModel.postData(new ModelCallBack() {
            @Override
            public void onSuccess(Bean bean) {
                if(iView != null){
                    iView.onSuccess(bean);
                }
            }

            @Override
            public void onFailure(Exception e) {
                if(iView != null){
                    iView.onFailure(e);
                }
            }
        });
    }

}
五。实现view包中类。

         1,创建IMainView类。

public interface IMainView {
    public void onSuccess(Bean bean);
    public void onFailure(Exception e);
}

六。创建adapter得类。

     创建IApdater适配器类。

  

public class IApdater extends RecyclerView.Adapter<IApdater.IViewHolder>{
    Context context;

    List<GoodsBriefBean> list ;


    public IApdater(Context context) {
        this.context = context;
    }


    public void addData(Bean bean){
        if(list == null){
            list = new ArrayList<>();
        }
        list.addAll(bean.getData().getGoodsBrief());
        notifyDataSetChanged();
    }

    @Override
    public IApdater.IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View view = View.inflate(context, R.layout.layout, null);

        return new IViewHolder(view);
    }

    @Override
    public void onBindViewHolder(IApdater.IViewHolder holder, final int position) {


        //holder.adapterSimpledraweeview.setImageURI("https://image.yunifang.com/yunifang/images/goods/121/goods_img/17062610568378169043195978.jpg");
        holder.adapterSimpledraweeview.setImageURI(list.get(position).getGoods_img());

        holder.adapterTextview.setText(list.get(position).getGoods_name());

        holder.adapterTextview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EventBus.getDefault().postSticky(new EventBean(list.get(position).getGoods_img(),list.get(position).getGoods_name()));


                context.startActivity(new Intent(context, Main2Activity.class));

            }
        });


    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    static class IViewHolder  extends RecyclerView.ViewHolder{
        @BindView(R.id.adapter_simpledraweeview)
        SimpleDraweeView adapterSimpledraweeview;
        @BindView(R.id.adapter_textview)
        TextView adapterTextview;

        IViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
七。创建MainActivity类。
public class MainActivity extends Activity implements IMainView {

    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    private MainPresenter presenter;
    private IApdater adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        presenter = new MainPresenter(this);
        presenter.get();
        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);

        adapter = new IApdater(this);

        recycleview.setLayoutManager(manager);
        recycleview.setAdapter(adapter);


        List<GoodsBriefBean> listBeans =  IApplication.session.getGoodsBriefBeanDao().loadAll();

        for(GoodsBriefBean bean : listBeans){
            System.out.println(bean.toString());
        }



    }
    @Override
    public void onSuccess(Bean bean) {
        adapter.addData(bean);

    }

    @Override
    public void onFailure(Exception e) {

    }
}
八。创建IGetDataBase请求接口。
 
public interface IGetDataBase {
    //random=96333&encode=bf3386e14fe5bb0bcef234baebca2414
    @GET("/yunifang/mobile/category/list")
    Call<Bean> get(@Query("random") String key,@Query("encode") String encode);

    /**
     * post 璇锋眰
     * @param key
     * @return
     */
    @FormUrlEncoded
    @POST("/weixin/query")
    Call<Bean> post(@Field("key") String key);


}
九。最后添加两布局。
1,activity布局。
              
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.lianxizongjie.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
   2.条目布局创建laout。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/adapter_simpledraweeview"
    android:layout_width="100dp"
    android:layout_height="100dp" />
    <TextView
        android:id="@+id/adapter_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
这就完成了数据显示。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值