商品详情

来源:互联网 发布:软件测试自学视频教程 编辑:程序博客网 时间:2024/04/30 00:45

activity_main

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"   >    <TextView        android:padding="15dp"        android:gravity="center_horizontal"        android:textSize="23sp"        android:text="商品详情"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"/>    <TextView        android:background="#000000"        android:layout_width="match_parent"        android:layout_height="1dp"        android:layout_weight="0"/>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="4">    <android.support.v4.view.ViewPager        android:id="@+id/viewPager"        android:layout_gravity="center_horizontal"        android:layout_width="match_parent"        android:layout_height="match_parent"        />        <LinearLayout            android:id="@+id/linear_layout"            android:layout_marginBottom="10dp"            android:layout_centerHorizontal="true"            android:orientation="horizontal"            android:layout_alignParentBottom="true"            android:layout_width="100dp"            android:layout_height="50dp"></LinearLayout>    </RelativeLayout>    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="2">    <TextView        android:id="@+id/text_title"        android:textSize="23sp"        android:layout_marginLeft="30dp"        android:layout_marginTop="30dp"        android:text="锤子"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_marginLeft="30dp"        android:id="@+id/text_price"        android:textSize="23sp"        android:text="199"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:textColor="#ff00"        android:layout_marginLeft="30dp"        android:id="@+id/text_bargainPrice"        android:textSize="23sp"        android:text="99"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    </LinearLayout>    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1">        <TextView            android:background="#000000"            android:layout_width="match_parent"            android:layout_height="1dp"            android:layout_weight="0"/>        <TextView            android:onClick="true"            android:id="@+id/jiaruBtn"            android:padding="15dp"            android:gravity="center_horizontal"            android:textSize="23sp"            android:text="加入购物车"            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"/>    </LinearLayout></LinearLayout>

MainActivity主类

package com.example.productdetail_day16;import android.graphics.Paint;import android.os.Handler;import android.os.Message;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import com.example.productdetail_day16.adapter.ImageAdapter;import com.example.productdetail_day16.bean.ProductBean;import com.google.gson.Gson;import java.io.IOException;import java.security.cert.PolicyNode;import java.util.ArrayList;import java.util.List;import okhttp3.Call;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class MainActivity extends AppCompatActivity {private Handler handler = new Handler(){    @Override    public void handleMessage(Message msg) {        super.handleMessage(msg);        switch (msg.what){            case 0:                int currentItem = viewPager.getCurrentItem();                viewPager.setCurrentItem(currentItem+1);                handler.sendEmptyMessageDelayed(0,2000);                break;        }    }};    private TextView text_title;    private TextView text_price;    private TextView text_bargainPrice;    private ViewPager viewPager;    private TextView btnjiaru;    private List<ProductBean.DataBean> list;    private ImageAdapter imageAdapter;    private LinearLayout linearLayout;    private List<ImageView> listshape;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text_title = (TextView) findViewById(R.id.text_title);        text_price = (TextView) findViewById(R.id.text_price);        text_bargainPrice = (TextView) findViewById(R.id.text_bargainPrice);        viewPager = (ViewPager) findViewById(R.id.viewPager);        btnjiaru = (TextView) findViewById(R.id.jiaruBtn);        linearLayout = (LinearLayout) findViewById(R.id.linear_layout);        //text_price.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线        text_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG ); //中间横线(删除线)       // text_price.getPaint().setAntiAlias(true);// 抗锯齿        getData();        //点击加入购物车的方法        btnjiaru.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                //https://www.zhaoapi.cn/product/addCart?uid=71&pid=1                OkHttpClient client = new OkHttpClient();                Request request = new Request.Builder()                        .url("https://www.zhaoapi.cn/product/addCart?uid=71&pid=1")                        .build();                Call call = client.newCall(request);                call.enqueue(new Callback() {                    @Override                    public void onFailure(Call call, IOException e) {                    }                    @Override                    public void onResponse(Call call, Response response) throws IOException {                        final String body = response.body().string();                        runOnUiThread(new Runnable() {                            @Override                            public void run() {                                //吐司加入购物车成功                                Toast.makeText(MainActivity.this,body,Toast.LENGTH_SHORT).show();                            }                        });                    }                });            }        });    }    private void getData() {        OkHttpClient client = new OkHttpClient();        Request request = new Request.Builder()                .url("https://www.zhaoapi.cn/product/getProductDetail?pid=1")                .build();        Call call = client.newCall(request);        call.enqueue(new Callback() {           @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                    //成功的返回 string只能使用一次              // System.out.println("lalal:"+response.body().string());                String json = response.body().string();                Gson gson = new Gson();               ProductBean productBean = gson.fromJson(json, ProductBean.class);                if(list==null){                    list = new ArrayList<>();                }                list.add(productBean.getData());                runOnUiThread(new Runnable() {                    @Override                    public void run() {                        //设置控件显示                        text_title.setText(list.get(0).getTitle());                       text_price.setText("原价:"+ list.get(0).getPrice()+"");                        text_bargainPrice.setText("优惠价:"+ list.get(0).getBargainPrice()+"");                        initshape();                        viewPager.setCurrentItem(100000);                        setAdapter();                        handler.sendEmptyMessageDelayed(0,2000);                        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {                            @Override                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {                                for (int i =0;i<listshape.size();i++){                                    if(i==position%listshape.size()){                                        listshape.get(i).setImageResource(R.drawable.shape02);                                    }else{                                        listshape.get(i).setImageResource(R.drawable.shape01);                                    }                                }                            }                            @Override                            public void onPageSelected(int position) {                            }                            @Override                            public void onPageScrollStateChanged(int state) {                            }                        });                    }                });               }        }); }    public void setAdapter(){        if(imageAdapter==null) {            imageAdapter = new ImageAdapter(MainActivity.this, list);            viewPager.setAdapter(imageAdapter);        }else{            imageAdapter.notifyDataSetChanged();        }    }    //初始化小圆点的方法    private void initshape(){        //创建装着小圆点的集合        listshape = new ArrayList<>();        //清空布局和集合        linearLayout.removeAllViews();        listshape.clear();        for (int i=0;i<3;i++){            ImageView imageView = new ImageView(MainActivity.this);            if(i==0){                //如果当前是第一页,就设置选中的图片                imageView.setImageResource(R.drawable.shape02);            }else{                imageView.setImageResource(R.drawable.shape01);            }            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);            layoutParams.setMargins(9,0,9,0);            //添加到集合和布局里            listshape.add(imageView);            linearLayout.addView(imageView,layoutParams);        }    }}
ImageAdapter适配器

package com.example.productdetail_day16.adapter;import android.content.Context;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import com.example.productdetail_day16.MainActivity;import com.example.productdetail_day16.bean.ProductBean;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.List;/** * Created by Menglucywhh on 2017/11/17. */public class ImageAdapter extends PagerAdapter{    Context context;    List<ProductBean.DataBean> list;    public ImageAdapter(Context context, List<ProductBean.DataBean> list) {        this.list = list;        this.context = context;    }    @Override    public int getCount() {        return Integer.MAX_VALUE;    }    @Override    public boolean isViewFromObject(View view, Object object) {        return view==object;    }    @Override    public Object instantiateItem(ViewGroup container, int position) {        ImageView imageView = new ImageView(context);        imageView.setScaleType(ImageView.ScaleType.FIT_XY);        String images = list.get(0).getImages();        String[] split = images.split("\\|");        ImageLoader.getInstance().displayImage(split[position%split.length],imageView);        container.addView(imageView);        return imageView;    }    @Override    public void destroyItem(ViewGroup container, int position, Object object) {       // super.destroyItem(container, position, object);        container.removeView((View) object);    }}
app图片初始化

package com.example.productdetail_day16.applica;import android.app.Application;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;/** * Created by Menglucywhh on 2017/11/17. */public class App extends Application{    @Override    public void onCreate() {        super.onCreate();        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();        ImageLoader.getInstance().init(configuration);    }}
shope01
没选中小圆点

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <!--没选中的小圆点-->    <corners android:radius="5dp"/>    <size android:width="10dp" android:height="10dp"/>    <solid android:color="#000000"/></shape>

shape02

选中的

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <!--选中的小圆点-->    <corners android:radius="5dp"/>    <size android:width="10dp" android:height="10dp"/>    <solid android:color="#ff0000"/></shape>


bean

package com.example.productdetail_day16.bean;/** * Created by Menglucywhh on 2017/11/17. */public class ProductBean {    /**     * msg :     * seller : {"description":"我是商家17","icon":"http://120.27.23.105/images/icon.png","name":"商家17","productNums":999,"score":5,"sellerid":17}     * code : 0     * data : {"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","itemtype":1,"pid":1,"price":118,"pscid":1,"salenum":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}     */    private String msg;    private SellerBean seller;    private String code;    private DataBean data;    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }    public SellerBean getSeller() {        return seller;    }    public void setSeller(SellerBean seller) {        this.seller = seller;    }    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code;    }    public DataBean getData() {        return data;    }    public void setData(DataBean data) {        this.data = data;    }    public static class SellerBean {        /**         * description : 我是商家17         * icon : http://120.27.23.105/images/icon.png         * name : 商家17         * productNums : 999         * score : 5         * sellerid : 17         */        private String description;        private String icon;        private String name;        private int productNums;        private int score;        private int sellerid;        public String getDescription() {            return description;        }        public void setDescription(String description) {            this.description = description;        }        public String getIcon() {            return icon;        }        public void setIcon(String icon) {            this.icon = icon;        }        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        public int getProductNums() {            return productNums;        }        public void setProductNums(int productNums) {            this.productNums = productNums;        }        public int getScore() {            return score;        }        public void setScore(int score) {            this.score = score;        }        public int getSellerid() {            return sellerid;        }        public void setSellerid(int sellerid) {            this.sellerid = sellerid;        }    }    public static class DataBean {        /**         * bargainPrice : 111.99         * createtime : 2017-10-14T21:39:05         * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends         * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg         * itemtype : 1         * pid : 1         * price : 118         * pscid : 1         * salenum : 0         * sellerid : 17         * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下         * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g         */        private double bargainPrice;        private String createtime;        private String detailUrl;        private String images;        private int itemtype;        private int pid;        private int 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 int getPrice() {            return price;        }        public void setPrice(int 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;        }    }}


原创粉丝点击