相关视频教程在某站上面(🔍浩宇软件开发)
1. 涉及到的技术点
- Intent启动意图,Activity页面之间的跳转
- intent.putExtra(‘xxxx’),Activity之间传值
- RelativeLayout相对布局的使用
2.代码实现过程
- 在上集中,已经实现了商品分类+商品分类列表,在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
- 商品详情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>
在布局中,为了使【加入购物车】按钮,一直在底部。不能使用线性布局,设置按钮离上边的距离,因为不同的屏幕的大小,离上边的距离是不一样的,所以使用相对布局,使按钮一直在底部
- 详情页如何接收跳转传过来的值
//获取传递的数据
productInfo = (ProductInfo) getIntent().getSerializableExtra("productInfo");
在这里使用getSerializableExtra()来接收一个对象的值,如果只接收一个String类型的,可以使用getStringExtra()
- 详情页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();
}
});
}
}