列表

一,依赖

//retrofit2的依赖
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
//rxJava2的依赖
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex.rxjava2:rxjava:2.1.7'

//拦截器的依赖
compile 'com.squareup.okhttp3:logging-interceptor:3.9.1'
//recyclerview的依赖
implementation 'com.android.support:recyclerview-v7:26.1.0'
//eventbus的依赖
compile 'org.greenrobot:eventbus:3.0.0'
//Fresco的依赖
compile 'com.facebook.fresco:fresco:1.5.0'
//ButterKnifr依赖
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'


二,权限

<uses-permission android:name="android.permission.INTERNET" />
 
三,main 布局
 
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ywf.bwei.com.lianxi2.Main2Activity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/products_rlv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
 
product_rlv_item  布局
 
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/product_lineat"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="horizontal">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/products_sdv"
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            app:placeholderImage="@mipmap/ic_launcher" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tv_product_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:padding="5dp"
                android:text="¥15151.0" />

            <TextView
                android:id="@+id/tv_product_subhead"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:padding="5dp"
                android:text="高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!" />

            <TextView
                android:id="@+id/tv_product_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:padding="5dp"
                android:text="一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机" />
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />
</RelativeLayout>
 
bean 层 ProductBean
 
 
public class ProductBean {
    private String msg;
    private String code;
    private String page;
    private List<DataBean> data;

    public String getMsg() {
        return msg;
    }

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

    public String getCode() {
        return code;
    }

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

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public List<DataBean> getData() {
        return data;
    }

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

    public static class DataBean {

        private int bargainPrice;
        private String createtime;
        private String detailUrl;
        private String images;
        private int itemtype;
        private int pid;
        private double price;
        private int pscid;
        private int salenum;
        private int sellerid;
        private String subhead;
        private String title;

        public int getBargainPrice() {
            return bargainPrice;
        }

        public void setBargainPrice(int bargainPrice) {
            this.bargainPrice = bargainPrice;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getDetailUrl() {
            return detailUrl;
        }

        public void setDetailUrl(String detailUrl) {
            this.detailUrl = detailUrl;
        }

        public String getImages() {
            return images;
        }

        public void setImages(String images) {
            this.images = images;
        }

        public int getItemtype() {
            return itemtype;
        }

        public void setItemtype(int itemtype) {
            this.itemtype = itemtype;
        }

        public int getPid() {
            return pid;
        }

        public void setPid(int pid) {
            this.pid = pid;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public int getPscid() {
            return pscid;
        }

        public void setPscid(int pscid) {
            this.pscid = pscid;
        }

        public int getSalenum() {
            return salenum;
        }

        public void setSalenum(int salenum) {
            this.salenum = salenum;
        }

        public int getSellerid() {
            return sellerid;
        }

        public void setSellerid(int sellerid) {
            this.sellerid = sellerid;
        }

        public String getSubhead() {
            return subhead;
        }

        public void setSubhead(String subhead) {
            this.subhead = subhead;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }
}
 
三,MyApp 
public class MyApp extends Application {

    public static SharedPreferences preferences;
    public static SharedPreferences.Editor edit;    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);

        preferences = getSharedPreferences("user", MODE_PRIVATE);
        edit = preferences.edit();
    }
}

四,model 层   IProductModel

public interface IProductModel {
    public void getProduct(String pscid, String page, OnNetListener<ProductBean> onNetListener);
}


model 层 ProductModel

public class ProductModel implements IProductModel {

    @Override
    public void getProduct(String pscid, String page, final OnNetListener<ProductBean> onNetListener) {
        ServiceApi serviceApi = RetrofitHelper.getServiceApi();
        serviceApi.getProduct(pscid, page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ProductBean>() {
                    @Override
                    public void accept(ProductBean productBean) throws Exception {
                        onNetListener.onSuccess(productBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        onNetListener.onFailure(throwable);
                    }
                });
    }
}
 
persenter 层 ProductPresenter

public class ProductPresenter {
    private IProductView iProductView;
    private final IProductModel iProductModel;

    public ProductPresenter(IProductView iProductView) {
        this.iProductView = iProductView;
        iProductModel = new ProductModel();
    }

    public void getProduct() {
        iProductModel.getProduct("39", "1", new OnNetListener<ProductBean>() {
            @Override
            public void onSuccess(ProductBean productBean) {
                if (productBean.getCode().equals("0")) {
                    iProductView.showProduct(productBean);
                }
            }

            @Override
            public void onFailure(Throwable throwable) {

            }
        });
    }

}

view 层 IProductView
 
public interface IProductView {
    public void showProduct(ProductBean productBean);

}
 UrlsApi 的类
public class UrlsApi {
    public static final String BASE_URL = "https://www.zhaoapi.cn/";
    public static final String PRODUCTS = "product/getProducts";//商品列表

}
ServiceApi 的 类
public interface ServiceApi {

    @GET(UrlsApi.PRODUCTS)
    Observable<ProductBean> getProduct(@Query("pscid") String pscid, @Query("page") String page);
}

main 函数
 
public class Main2Activity extends AppCompatActivity implements IProductView {
    @BindView(R.id.products_rlv)
    RecyclerView productsRlv;
    private ProductPresenter productPresenter;
    private ProductElvAdapter productElvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        productsRlv.setLayoutManager(new LinearLayoutManager(this));
        productPresenter = new ProductPresenter(this);
        productPresenter.getProduct();

    }

    @Override
    public void showProduct(ProductBean productBean) {
        final List<ProductBean.DataBean> data = productBean.getData();
        productElvAdapter = new ProductElvAdapter(this, data);
        productsRlv.setAdapter(productElvAdapter);
        productElvAdapter.setOnClick(new ProductElvAdapter.OnClick() {
            @Override
            public void Onclick(String pid) {
              /*  Intent intent = new Intent(Main2Activity.this, ProductDetailActivity.class);
                intent.putExtra("pid", pid);
                startActivity(intent);*/
            }
        });
    }

}

adapter 层 ProductElvAdapter

 
public class ProductElvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<ProductBean.DataBean> dataList;
    private LayoutInflater inflater;
    private OnClick onClick;
    private int pid;

    public interface OnClick {
        void Onclick(String pid);
    }

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public ProductElvAdapter(Context context, List<ProductBean.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.product_rlv_item, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;

        final ProductBean.DataBean dataBean = dataList.get(position);
        myViewHolder.tvProductPrice.setText("¥" + dataBean.getPrice());
        myViewHolder.tvProductSubhead.setText(dataBean.getSubhead());
        myViewHolder.tvProductTitle.setText(dataBean.getTitle());
        String[] images = dataBean.getImages().split("\\!");
        myViewHolder.productSdv.setImageURI(images[0]);
        pid = dataBean.getPid();
        myViewHolder.pLineat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.Onclick(pid+"");
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_product_price)
        TextView tvProductPrice;
        @BindView(R.id.tv_product_subhead)
        TextView tvProductSubhead;
        @BindView(R.id.tv_product_title)
        TextView tvProductTitle;
        @BindView(R.id.products_sdv)
        SimpleDraweeView productSdv;
        @BindView(R.id.product_lineat)
        RelativeLayout pLineat;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值