Android_实现商品详情的展示页及布局

okhttp封装及logge拦截器:
http://blog.csdn.net/qq_40087961/article/details/78828811

,,记得要导入依赖及权限

商品详情页的model层

public class RecyclerDetailsModel {
    //自己定义的方法
    public void receive(String pid, final IRecyclerDetailsModel iRecyclerDetailsModel) {
        String url="http://120.27.23.105/product/getProductDetail";
        HashMap hashMap = new HashMap<>();
        hashMap.put("pid", pid);
        OkHttpUtils.getInstance().doPost(url, hashMap, new CallBack() {
            @Override
            public void onSuccess(String str) {
                if (str != null) {
                    Gson gson = new Gson();
                    RecyclerDetailsBean rdBean = gson.fromJson(str, RecyclerDetailsBean.class);
                    if (rdBean != null) {
                        iRecyclerDetailsModel.onSuccess(rdBean);
                    }
                }
            }

            @Override
            public void onFailed(String message) {
                iRecyclerDetailsModel.onFailed();
            }
        });
    }

}

商品详情页的model层的借口

public interface IRecyclerDetailsModel {

    void onSuccess(RecyclerDetailsBean rdBean);

    void onFailed();

}

商品详情页的view层接口

public interface IRecyclerDetailsPresenter {

    void onSuccess(RecyclerDetailsBean rdBean);

    void onFailed();

}

商品详情页的presenter层

public class RecyclerDetailsPresenter implements IRecyclerDetailsModel {

    private RecyclerDetailsModel recyclerDetailsModel;
    private IRecyclerDetailsPresenter irecyclerDetailsPresenter;

    public RecyclerDetailsPresenter(IRecyclerDetailsPresenter irecyclerDetailsPresenter) {
        this.irecyclerDetailsPresenter = irecyclerDetailsPresenter;
        recyclerDetailsModel = new RecyclerDetailsModel();
    }

    public void receive(String pid) {
        recyclerDetailsModel.receive(pid, this);
    }

    @Override
    public void onSuccess(RecyclerDetailsBean rdBean) {
        irecyclerDetailsPresenter.onSuccess(rdBean);
    }

    @Override
    public void onFailed() {
        irecyclerDetailsPresenter.onFailed();
    }
}

主要的view层

public class RecyclerDetailsActivity extends AppCompatActivity implements IRecyclerDetailsPresenter,IAddShopCarPresenter{
    private int pid = 71;
    private RecyclerDetailsPresenter recyclerDetailsPresenter;
    private ListView listView;
    private RecyclerDetailsBean.DataBean data;
    private TextView title;
    private TextView price;
    private String urlImg;
    private String[] split;
    private String urlTitle;
    private double urlPrice;
    private ArrayList lists = new ArrayList();
    private ViewPager viewPager;
    private DetailsAdapter adapter;
    private Button join;
    private int pids;
    private AddShopCarPresenter addShopCarPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        initview();
        //Intent intent = getIntent();
        //pid = intent.getIntExtra("pid", this.pid);
        //获取商品详情页的p层
        recyclerDetailsPresenter = new RecyclerDetailsPresenter(this);
        //调用商品详情页的获取pid的方法
        recyclerDetailsPresenter.receive(pid + "");
        //将商品添加到购物车的p层
        addShopCarPresenter = new AddShopCarPresenter(this);
    }

    /**
     * 获取控件的方法
     *
     * */
    private void initview() {
        //轮播展示图片的控件
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        //点击加入购物车的控件
        join = (Button) findViewById(R.id.join);
        title = (TextView) findViewById(R.id.title);
        price = (TextView) findViewById(R.id.price);
    }
    /*
    * 实现成功的方法
    * */
    @Override
    public void onSuccess(RecyclerDetailsBean rdBean) {
        /**
         * 从集合获取数据,并赋值
         */
        data = rdBean.getData();
        pids = data.getPid();
        urlImg = data.getImages();
        split = urlImg.split("\\|");
        urlTitle = data.getTitle();
        urlPrice = data.getPrice();
        title.setText(urlTitle);
        price.setText(urlPrice + "");
        /**
         * 将图片做成无限轮播
         * 配置适配器
         */
        adapter = new DetailsAdapter(RecyclerDetailsActivity.this, split);
        viewPager.setAdapter(adapter);
        /**
         * 点击加入购物车的按钮
         */
        join.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                /*Toast.makeText(RecyclerDetailsActivity.this, ImageLoaderApp.sql.getInt("id", 0)+"", Toast.LENGTH_SHORT).show();*/
                //传入加入购物车的字段
                addShopCarPresenter.receive("android", 3381+"", pids + "");
            }
        });
    }

    @Override
    public void onSuccess(String msg) {
        Toast.makeText(RecyclerDetailsActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailed() {

    }

    /**
     * 跳转购物车
     * @param view
     */
    public void shopcar(View view){
        Intent intent = new Intent(RecyclerDetailsActivity.this, ShoppingCartActivity.class);
        startActivity(intent);
    }
}

图片轮播适配器

/**
 * 详情页的图片的无限轮播
 */

public class DetailsAdapter extends PagerAdapter {

    private Context context;
    private String[] split;

    public DetailsAdapter(Context context, String[] split) {
        this.context = context;
        this.split = split;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //添加图片到布局中
        View view = View.inflate(context, R.layout.layout_detailviewpager, null);
        ImageView img = view.findViewById(R.id.img);
        Glide.with(context).load(split[position%split.length]).into(img);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
}

RecyclerDetailsActivity 的xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="250dp">android.support.v4.view.ViewPager>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/viewPager"
        android:text="标题"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:text="价格"
        android:textColor="#F00"
        android:textSize="22sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        >
        <Button
            android:id="@+id/shopcar"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:text="购物车"
            android:onClick="shopcar"
            />
        <Button
            android:id="@+id/join"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:text="加入购物车" />

    LinearLayout>


RelativeLayout>

适配器的布局

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

    <ImageView
        android:id="@+id/img"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

RelativeLayout>

你可能感兴趣的:(购物车-android)