目录
一、在主Fragment.xml中内嵌RecyclerView布局
前言
讲讲为什么这么设置,主要是因为你有展示一列一列数据的时候,这时候你可以在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;
}
}
最后展示: