使用Android实现,商品推荐页面有四个商品跳转到其中一个商品的详情页面,跳转使用intent,详情页面包括商品的名称、价格、介绍。推荐页面四个商品使用的gridview与fragment。详情页的activity与fragment也要给出

时间: 2024-02-06 21:10:54 浏览: 25
首先,我们需要在推荐页面的布局文件中添加一个GridView来显示四个商品。我们可以使用以下代码: ```xml <GridView android:id="@+id/grid_view" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:padding="10dp" /> ``` 接下来,我们需要创建一个Fragment来显示GridView。我们可以使用以下代码: ```java public class ProductFragment extends Fragment { private GridView mGridView; public ProductFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_product, container, false); mGridView = view.findViewById(R.id.grid_view); // Set adapter for GridView ProductAdapter adapter = new ProductAdapter(getActivity(), getProductList()); mGridView.setAdapter(adapter); // Set click listener for GridView items mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // Launch ProductDetailActivity with selected product Intent intent = new Intent(getActivity(), ProductDetailActivity.class); intent.putExtra("product", getProductList().get(position)); startActivity(intent); } }); return view; } // Method to get list of products private List<Product> getProductList() { List<Product> productList = new ArrayList<>(); productList.add(new Product("Product 1", "$10.00", "Product 1 description.")); productList.add(new Product("Product 2", "$20.00", "Product 2 description.")); productList.add(new Product("Product 3", "$30.00", "Product 3 description.")); productList.add(new Product("Product 4", "$40.00", "Product 4 description.")); return productList; } } ``` 在这个Fragment中,我们设置了一个ProductAdapter来显示GridView中的商品,并在点击GridView项时启动ProductDetailActivity。 接下来,我们需要创建一个ProductAdapter来显示GridView中的商品。我们可以使用以下代码: ```java public class ProductAdapter extends ArrayAdapter<Product> { public ProductAdapter(Context context, List<Product> products) { super(context, 0, products); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_product, parent, false); } Product product = getItem(position); TextView nameTextView = convertView.findViewById(R.id.name_text_view); TextView priceTextView = convertView.findViewById(R.id.price_text_view); nameTextView.setText(product.getName()); priceTextView.setText(product.getPrice()); return convertView; } } ``` 在这个ProductAdapter中,我们设置了一个item_product布局文件来显示GridView中的每个商品的名称和价格。 现在,我们需要创建一个ProductDetailActivity来显示选定商品的详细信息。我们可以使用以下代码: ```java public class ProductDetailActivity extends AppCompatActivity { private Product mProduct; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product_detail); // Get selected product from intent mProduct = getIntent().getParcelableExtra("product"); // Set title of activity to product name setTitle(mProduct.getName()); // Set product details TextView nameTextView = findViewById(R.id.name_text_view); TextView priceTextView = findViewById(R.id.price_text_view); TextView descriptionTextView = findViewById(R.id.description_text_view); nameTextView.setText(mProduct.getName()); priceTextView.setText(mProduct.getPrice()); descriptionTextView.setText(mProduct.getDescription()); } } ``` 在这个ProductDetailActivity中,我们获取从推荐页面传递的选定商品,并将其详细信息显示在屏幕上。 最后,我们需要创建一个fragment_product布局文件来显示ProductFragment中的GridView,以及一个item_product布局文件来显示每个商品的名称和价格,以及一个activity_product_detail布局文件来显示选定商品的详细信息。我们可以使用以下代码: fragment_product.xml: ```xml <GridView android:id="@+id/grid_view" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="2" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:padding="10dp" /> ``` item_product.xml: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <TextView android:id="@+id/name_text_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/price_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" /> </LinearLayout> ``` activity_product_detail.xml: ```xml <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/name_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:textStyle="bold" /> <TextView android:id="@+id/price_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/description_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> </ScrollView> ``` 现在,我们已经完成了Android应用程序中商品推荐页面和商品详情页面的创建。

相关推荐

最新推荐

recommend-type

《Google Android开发入门与实战》.pdf

11.2.3 华丽转身——实现多页面跳转 203 11.2.4 始终为用户做好服务——增加service 203 11.2.5 细节决定成败——完善应用细节 203 11.3 成功就在眼前——应用测试和发布 204 11.3.1 只欠东风——应用...
recommend-type

Android开发资料合集-World版!

9.4.ANDROID屏幕适应的四个原则 195 9.5、ANDROID常用单位 196 9.6、取得屏幕信息 197 9.7、横竖屏 197 9.8、程序完全全屏 200 9.8.1锁屏锁键盘 200 9.9、程序的开机启动 201 9.10、动态START页面 208 9.11、彻底...
recommend-type

【车牌识别】 GUI BP神经网络车牌识别(带语音播报)【含Matlab源码 668期】.zip

Matlab领域上传的视频均有对应的完整代码,皆可运行,亲测可用,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描视频QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作
recommend-type

【作业视频】六年级第1讲--计算专项训练(2022-10-28 22-51-53).mp4

【作业视频】六年级第1讲--计算专项训练(2022-10-28 22-51-53).mp4
recommend-type

3文件需求申请单.xls

3文件需求申请单.xls
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

云原生架构与soa架构区别?

云原生架构和SOA架构是两种不同的架构模式,主要有以下区别: 1. 设计理念不同: 云原生架构的设计理念是“设计为云”,注重应用程序的可移植性、可伸缩性、弹性和高可用性等特点。而SOA架构的设计理念是“面向服务”,注重实现业务逻辑的解耦和复用,提高系统的灵活性和可维护性。 2. 技术实现不同: 云原生架构的实现技术包括Docker、Kubernetes、Service Mesh等,注重容器化、自动化、微服务等技术。而SOA架构的实现技术包括Web Services、消息队列等,注重服务化、异步通信等技术。 3. 应用场景不同: 云原生架构适用于云计算环境下的应用场景,如容器化部署、微服务
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。