在Fragment使用RecyclerView(附代码)

目录

文章目录

前言

一、在主Fragment.xml中内嵌RecyclerView布局

二、另外设置item_product来实现具体一列设置

1.当然,你也可以使用databinding来进行数据绑定,我在这里只设置几个列,就不那么麻烦了

三、设置你的封装数据对象(product)


前言

讲讲为什么这么设置,主要是因为你有展示一列一列数据的时候,这时候你可以在Fragment中内嵌RecyclerView,其中的具体实现由item_product实现即可。详细看代码吧,多说无益。

一、在主Fragment.xml中内嵌RecyclerView布局

<androidx.recyclerview.widget.RecyclerView
         android:id="@+id/recyclerView"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

二、另外设置item_product来实现具体一列设置

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:ignore="ContentDescription">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="4dp"
        app:cardCornerRadius="5dp"
        app:cardElevation="0dp"
        app:cardUseCompatPadding="true">

        <androidx.constraintlayout.widget.ConstraintLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:src="@drawable/award" />

            <TextView
                android:id="@+id/nameTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:textColor="@color/black"
                android:textSize="20sp"
                app:layout_constraintStart_toEndOf="@+id/imageView"
                app:layout_constraintTop_toTopOf="@+id/imageView"
                tools:text="name" />

            <TextView
                android:id="@+id/boolprice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                app:layout_constraintStart_toStartOf="@+id/nameTextView"
                app:layout_constraintTop_toBottomOf="@+id/nameTextView"
                android:textColor="@{Integer.parseInt(String.valueOf(boolprice.getText())) >= 0 ? @android:color/holo_red_light : @android:color/holo_green_light}"
                tools:text="50.00%" />
            <TextView
                android:id="@+id/priceTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:fontFamily="@font/lalezar"
                android:textColor="@color/black"
                android:textSize="30sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="500" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</layout>
1.当然,你也可以使用databinding来进行数据绑定,我在这里只设置几个列,就不那么麻烦了

三、设置你的封装数据对象(product)

data class Product(
    val imageResource: Int,
    val name: String, 
    val boolprice : String, 
    val price: String)

四、设置适配器来进行数据与布局的更新显示ProductAdapter

class ProductAdapter(private val products: List<Product>) :
    RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {

    class ProductViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.imageView)
        val nameTextView: TextView = itemView.findViewById(R.id.nameTextView)
        val boolprice: TextView = itemView.findViewById(R.id.boolprice)
        val priceTextView: TextView = itemView.findViewById(R.id.priceTextView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_product, parent, false)
        return ProductViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
        val currentProduct = products[position]
        holder.imageView.setImageResource(currentProduct.imageResource)
        holder.nameTextView.text = currentProduct.name
        holder.boolprice.text = currentProduct.boolprice
        holder.priceTextView.text = currentProduct.price
    }

    override fun getItemCount(): Int {
        return products.size
    }
}

四、在Fragment中进行设置绑定,这时使用getSampleProducts()来获取数据信息

public class ProductFragment extends Fragment {

    private RecyclerView recyclerView;
    private ProductAdapter productAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product, container, false);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        productAdapter = new ProductAdapter(getSampleProducts());
        recyclerView.setAdapter(productAdapter);
        return view;
    }

   private List<Product> getSampleProducts() {
        // 可以在这里替换为实际产品数据
        List<Product> products = new ArrayList<>();
        products.add(new Product(R.drawable.lizhi, "荔枝", "+10%", "4.20"));
        products.add(new Product(R.drawable.boluo, "菠萝", "-5%", "1.38"));
        products.add(new Product(R.drawable.xiangjiao, "香蕉", "+20%", "1.98"));
        products.add(new Product(R.drawable.xigua, "西瓜", "-10%", "1.58"));
        products.add(new Product(R.drawable.mangguo, "芒果", "-15%", "3.88"));
        products.add(new Product(R.drawable.baicai, "白菜", "-5%", "2.58"));
        products.add(new Product(R.drawable.lianou, "莲藕", "+10%", "4.00"));
        products.add(new Product(R.drawable.lajiao, "辣椒", "-3%", "4.60"));
        products.add(new Product(R.drawable.shengjiang, "生姜", "-4%", "4.70"));
        products.add(new Product(R.drawable.dacong, "大葱", "+1%", "5.00"));
        return products;
    }
}

最后展示:

  • 17
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您介绍一下在 Android Studio 中如何在 Fragment使用 RecyclerView。 首先,在 Fragment 的布局文件中添加 RecyclerView 控件: ```xml <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 然后,在 Fragment代码中找到 RecyclerView 控件: ```java RecyclerView recyclerView = view.findViewById(R.id.recycler_view); ``` 接下来,需要为 RecyclerView 设置布局管理器和适配器: ```java // 设置布局管理器 recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); // 设置适配器 MyAdapter adapter = new MyAdapter(dataList); recyclerView.setAdapter(adapter); ``` 其中,`MyAdapter` 是您自己定义的 RecyclerView 适配器类,`dataList` 是您要显示的数据列表。 最后,您需要在 `MyAdapter` 中实现 RecyclerView 的 ViewHolder 和 onBindViewHolder 方法,以及返回数据列表的大小的方法。 ```java public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<String> mDataList; public MyAdapter(List<String> dataList) { mDataList = dataList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_layout, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { String data = mDataList.get(position); holder.mTextView.setText(data); } @Override public int getItemCount() { return mDataList.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(@NonNull View itemView) { super(itemView); mTextView = itemView.findViewById(R.id.text_view); } } } ``` 上面的代码中,`item_layout` 是您自己定义的 RecyclerView Item 的布局文件,`text_view` 是其中一个 TextView 控件的 ID。 这样,您就可以在 Fragment使用 RecyclerView 了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Imagine8877

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

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

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

打赏作者

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

抵扣说明:

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

余额充值