>>分享Android开发相关的技术 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 21916 个阅读者 刷新本主题
 * 贴子主题:  Android_实现商品详情的展示页及布局 回复文章 点赞(0)  收藏  
作者:日月光华    发表时间:2024-04-23 05:44:49     消息  查看  搜索  好友  邮件  复制  引用

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<String, String> 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<String> lists = new ArrayList<String>();
    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).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>

————————————————
版权声明:本文为CSDN博主「程序员的美腻」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40087961/article/details/78859070


程序猿的技术大观园:www.javathinker.net
  Java面向对象编程-->输入与输出(下)
  JavaWeb开发-->开发JavaMail Web应用
  JSP与Hibernate开发-->JPA API的高级用法
  Java网络编程-->基于UDP的数据报和套接字
  精通Spring-->通过Vuex进行状态管理
  Vue3开发-->虚拟DOM和render()函数
  Android开发教程之Java开发环境配置和运行第一个程序
  Android网络编程之WebKit应用
  Android网络开发-请求队列-性能提升解决方案
  Android中shape的使用
  android 手势操作GestureDetector
  Android中的几个布局
  Android SDCard UnMounted 流程分析
  Android Adapter使用范例
  ]android:gravity / android:layout_Gravity 的区别
  Android数据存储之Content Providers
  Android ListView高度问题
  Android Socket通信
  android 系统自带的卡帧警告
  Android 应用程序组件
  Android中shape的使用-Kofi
  更多...
 IPIP: 已设置保密
树形列表:   
1页 1条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。