Android - 信息列表页面【仿】淘宝App

Android - 信息列表页面【仿】淘宝App_第1张图片net文件夹

ApiService.java

public interface ApiService {
    @GET("product/getProducts")
    Flowable detailsget(@QueryMap Map map);
    }

xml文件

activity_details.xml

"http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    .support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    .support.v7.widget.RecyclerView>

itemlist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="horizontal">

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/image"
        android:layout_width="120dp"
        android:layout_height="120dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:padding="10dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:text="TextView" />

        <TextView
            android:id="@+id/tv_price"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="TextView" />
    LinearLayout>
LinearLayout>

java文件

app文件夹:图片解析

//一定在记得在清单文件中注册
        android:name=".app.App"

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

bean文件夹:数据解析

DetailsBean .java

public class DetailsBean {
private String msg;
    private String code;
    private String page;
    private List 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 getData() {
        return data;
    }

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

    public static class DataBean {
     private double 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 double getBargainPrice() {
            return bargainPrice;
        }

        public void setBargainPrice(double 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;
        }
    }
}

model文件夹:数据层

DetailsModel .java

public class DetailsModel implements IModel {
    private DetailsPresenter presenter;

    public DetailsModel(DetailsPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void get(String baseurl, Map map) {
        Flowable flowable = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class).detailsget(map);
        presenter.getData(flowable);
    }
}

view文件夹:视图层

DetailsActivity.java

public class DetailsActivity extends AppCompatActivity implements IView {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    private DetailsPresenter detailsPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        ButterKnife.bind(this);
//        商品详情:https://www.zhaoapi.cn/product/getProductDetail
//        请求参数:pid  商品id; 返回参数:images 顶部图片列表;

//        商品列表:https://www.zhaoapi.cn/product/getProducts?pscid=39&page=1
//        请求参数:pscid=39 固定写死;page为页码,用于分页功能
        detailsPresenter = new DetailsPresenter(this);
        Map map = new HashMap<>();
        map.put("pscid", "39");
        map.put("page", "1");
        detailsPresenter.get("https://www.zhaoapi.cn/", map);
    }

    @Override
    public void onSuccess(Object o) {
        DetailsBean d = (DetailsBean) o;
        List data = d.getData();
        if (data != null) {
            LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            DetailsReAdapter detailsReAdapter = new DetailsReAdapter(this, data);
            recyclerview.setLayoutManager(manager);
            recyclerview.setAdapter(detailsReAdapter);
        }
    }

    @Override
    public void onFailed(Exception e) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (detailsPresenter != null) {
            detailsPresenter.detatch();
        }
    }

presenter文件夹:逻辑层

BasePresenter.java

public interface BasePresenter {
    void get(String baseurl, Map<String, String> map);

    void get2(String baseurl, Map<String, String> map);
}

DetailsPresenter.java

public class DetailsPresenter implements BasePresenter {
    private IView iv;
    private DisposableSubscriber disposableSubscriber;

    public DetailsPresenter(IView iv) {
        this.iv = iv;
    }

    @Override
    public void get(String baseurl, Map map) {
        DetailsModel model = new DetailsModel(this);
        model.get(baseurl, map);
    }

    @Override
    public void get2(String baseurl, Map map) {

    }

    public void getData(Flowable flowable) {
        disposableSubscriber = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(DetailsBean detailsBean) {
                        iv.onSuccess(detailsBean);
                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFailed((Exception) t);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //防止内存泄漏
    public void detatch() {
        if (iv != null) {
            iv = null;
        }
        if (disposableSubscriber != null) {
            if (!disposableSubscriber.isDisposed()) {
                disposableSubscriber.dispose();
            }
        }
    }

adapter文件夹:适配器

DetailsReAdapter.java

public class DetailsReAdapter extends RecyclerView.Adapter<DetailsReAdapter.ViewHolder> {
    private Context context;
    private List list;

    public DetailsReAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.itemlist, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        String[] split = list.get(position).getImages().split("!");
        holder.image.setImageURI(Uri.parse(split[0]));
        holder.tv_title.setText(list.get(position).getTitle());
        holder.tv_price.setText(list.get(position).getPrice() + "");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转页面
                //https://www.zhaoapi.cn/product/getProductDetail?source=android&pid=1
//                Intent intent = new Intent(context, DetaiActivity.class);
//                intent.putExtra("pid", list.get(position).getPid() + "");
//                context.startActivity(intent);
            }
        });
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_price;
        private TextView tv_title;
        private SimpleDraweeView image;

        public ViewHolder(View itemView) {
            super(itemView);
            //itemView一个条目的视图
            image = itemView.findViewById(R.id.image);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_price = itemView.findViewById(R.id.tv_price);
        }
    }

你可能感兴趣的:(Android,Studio)