相关视频教程在某站上面(🔍浩宇软件开发)
1. 涉及到的技术点
- Fragment的使用
- RecyclerView列表控件的使用
- Adapter适配器的使用
2. 代码实现过程
- 分类首页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为对应的分类商品列表
- 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));
}
});
}
}
因为是以文章的形式呈现,没法做到无缝衔接所有的代码,本项目的视频教程可在某站搜【🔍浩宇软件开发】