Androidstudio开发,购物商城app 实现商品详情页(六)

相关视频教程在某站上面(🔍浩宇软件开发)

1. 涉及到的技术点

  1. Intent启动意图,Activity页面之间的跳转
  2. intent.putExtra(‘xxxx’),Activity之间传值
  3. RelativeLayout相对布局的使用

2.代码实现过程

  1. 在上集中,已经实现了商品分类+商品分类列表,在HomeFragment.java中,添加列表点击事件
        //recyclerView点击事件
        mRightListAdapter.setOnItemClickListener(new RightListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(ProductInfo productInfo, int position) {
                //跳转传值
                Intent intent  =new Intent(getActivity(), ProductDetailsActivity.class);
                //intent 传递对象的时候,实体类一定要 implements Serializable
                intent.putExtra("productInfo",productInfo);
                startActivity(intent);

            }
        });

在上面的代码中,intent.putExtra()传递的是一个对象,在传递对象的时候,相对应的实体一定要实现 Serializable

  1. 商品详情activity_product_details.xml布局
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProductDetailsActivity">


    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/my_light_primary"
            app:navigationIcon="@drawable/baseline_arrow_back_24"
            app:title="详情"
            app:titleTextColor="@color/white" />


        <ImageView
            android:id="@+id/product_img"
            android:layout_width="match_parent"
            android:layout_height="240dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/img_product_1" />


        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">


            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">


                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="5dp"
                    android:src="@drawable/baseline_currency_yen_24" />


                <TextView
                    android:id="@+id/product_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="198"
                    android:textColor="#FF6D00"
                    android:textSize="24sp"
                    android:textStyle="bold" />

            </androidx.appcompat.widget.LinearLayoutCompat>


            <TextView
                android:id="@+id/product_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="劲仔鹌鹑蛋248g袋装卤蛋办公室零食休闲小吃小蛋圆圆盐焗香辣卤香" />


            <TextView
                android:id="@+id/product_details"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:lineSpacingExtra="6dp"
                android:text="品牌:喵馋师系列:美味混合款零食礼包送女友口味:豪华款【买一箱发两箱】共200包品牌零食+开心鸭礼盒
包装种类:礼盒装产地:中国大陆省份:河南省
是否为有机食品:否包装方式:散装净含量:12g
生产许可证编号:SC11241172800647厂名:河南上口娃食品有限公司厂址:河南省驻马店市遂平县产业聚集区经一路向北200米
保质期:240"
                android:textColor="#999999" />


        </androidx.appcompat.widget.LinearLayoutCompat>


    </androidx.appcompat.widget.LinearLayoutCompat>


    <Button
        android:id="@+id/addCar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:text="加入购物车" />
</RelativeLayout>

在布局中,为了使【加入购物车】按钮,一直在底部。不能使用线性布局,设置按钮离上边的距离,因为不同的屏幕的大小,离上边的距离是不一样的,所以使用相对布局,使按钮一直在底部

  1. 详情页如何接收跳转传过来的值
   //获取传递的数据
   productInfo = (ProductInfo) getIntent().getSerializableExtra("productInfo");

在这里使用getSerializableExtra()来接收一个对象的值,如果只接收一个String类型的,可以使用getStringExtra()

  1. 详情页ProductDetailsActivity.java 具体实现
public class ProductDetailsActivity extends AppCompatActivity {

    private ImageView product_img;
    private TextView product_title;
    private TextView product_price;
    private TextView product_details;

    private ProductInfo productInfo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_details);

        //获取传递的数据
        productInfo = (ProductInfo) getIntent().getSerializableExtra("productInfo");

        //返回
        findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //初始化控件
        product_img = findViewById(R.id.product_img);
        product_title = findViewById(R.id.product_title);
        product_price = findViewById(R.id.product_price);
        product_details = findViewById(R.id.product_details);

        //设置数据
        if (null != productInfo) {
            product_img.setImageResource(productInfo.getProduct_img());
            product_title.setText(productInfo.getProduct_title());
            product_details.setText(productInfo.getProduct_details());
            product_price.setText(productInfo.getProduct_price() + "");
        }


        //加入购物车
        findViewById(R.id.addCar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(ProductDetailsActivity.this)
                        .setTitle("确认是否加入到购物车?")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserInfo userInfo = UserInfo.getUserInfo();
                                if (userInfo != null) {
                                    //加入到购物车
                                    int row = CarDbHelper.getInstance(ProductDetailsActivity.this).addCar(userInfo.getUsername(), productInfo.getProduct_id(), productInfo.getProduct_img(), productInfo.getProduct_title(), productInfo.getProduct_price());
                                    if (row > 0) {
                                        Toast.makeText(ProductDetailsActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                        finish();
                                    } else {
                                        Toast.makeText(ProductDetailsActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                                    }

                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        })
                        .show();

            }
        });


    }
}

3. 运行效果图

请添加图片描述

4.参考学习文章

  1. RelativeLayout(相对布局) https://www.runoob.com/w3cnote/android-tutorial-relativelayout.html
  2. LinearLayout(线性布局) https://www.runoob.com/w3cnote/android-tutorial-linearlayout.html
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值