Androidstudio开发,购物商城app实现商品分类列表(五)

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

1. 涉及到的技术点

  1. Fragment的使用
  2. RecyclerView列表控件的使用
  3. Adapter适配器的使用

2. 代码实现过程

  1. 分类首页fragment_home.xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragment.HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/my_light_primary"
         app:title="首页"
        app:titleTextColor="@color/white"/>

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


        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="100dp"
            android:background="#f5f5f5"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/leftRecyclerView"
                tools:listitem="@layout/left_list_item"
                android:layout_width="match_parent"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                android:layout_height="match_parent"/>


        </androidx.appcompat.widget.LinearLayoutCompat>


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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rightRecyclerView"
                tools:listitem="@layout/right_list_item"
                android:layout_width="match_parent"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                android:layout_height="match_parent"/>

        </androidx.appcompat.widget.LinearLayoutCompat>


    </androidx.appcompat.widget.LinearLayoutCompat>



</androidx.appcompat.widget.LinearLayoutCompat>

在上面布局中,使用了两个RecyclerView列表控件,leftRecyclerView为商品分类,rightRecyclerView为对应的分类商品列表

  1. HomeFragment.java中代码实现
public class HomeFragment extends Fragment {

    private View rootView;
    private RecyclerView leftRecyclerView;
    private RecyclerView rightRecyclerView;
    private LeftListAdapter mLeftListAdapter;
    private RightListAdapter mRightListAdapter;

    private List<String> leftDataList =new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_home, container, false);

        //初始化控件
        leftRecyclerView = rootView.findViewById(R.id.leftRecyclerView);
        rightRecyclerView = rootView.findViewById(R.id.rightRecyclerView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        leftDataList.add("食品");
        leftDataList.add("饮料");
        leftDataList.add("服装");
        leftDataList.add("电器");
        leftDataList.add("日用品");
        leftDataList.add("化妆品");

        mLeftListAdapter =new LeftListAdapter(leftDataList);
        leftRecyclerView.setAdapter(mLeftListAdapter);


        mRightListAdapter =new RightListAdapter();
        rightRecyclerView.setAdapter(mRightListAdapter);
        //默认加载食品数据
        mRightListAdapter.setListData(DataService.getListData(0));

        //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);

            }
        });




        //recyclerView点击事件
        mLeftListAdapter.setLeftListOnClickItemListener(new LeftListAdapter.LeftListOnClickItemListener() {
            @Override
            public void onItemClick(int position) {
                mLeftListAdapter.setCurrentIndex(position);

                //点击左侧分类切换对应的列表数据
                mRightListAdapter.setListData(DataService.getListData(position));
            }
        });
    }
}

因为是以文章的形式呈现,没法做到无缝衔接所有的代码,本项目的视频教程可在某站搜【🔍浩宇软件开发】

3. 运行效果图

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浩宇软件开发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值