我决定制作一个电子商务商店应用程序,并且我想包含两个或多个相同的布局,其中包含一个回收器视图,该视图从 java 代码中获取数据(现在)。
我尝试为它们添加不同的 ID,但我没有看到产品
我想包含的布局(horizontal_scroll_layout.xml):
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_margin="8dp"
android:paddingBottom="8dp"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/horizontal_scroll_layout_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Most Popular"
android:fontFamily="@font/cera_pro_medium"
android:textColor="#000000"
android:textAlignment="center"
android:textSize="17dp"
app:layout_constraintBottom_toBottomOf="@+id/horizontal_scroll_view_more"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/horizontal_scroll_view_more" />
<TextView
android:id="@+id/horizontal_scroll_view_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:fontFamily="@font/cera_pro_regular"
android:text="View More"
android:textColor="@color/colorPrimary"
android:textSize="14dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/horizontal_product_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/horizontal_scroll_view_more" />
</androidx.constraintlayout.widget.ConstraintLayout>
我在做什么(fragment_home.xml):
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout">
<include android:id="@+id/test1"
layout="@layout/horrizontal_scroll_layout" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout">
<include
android:id="@+id/test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/horrizontal_scroll_layout" />
</LinearLayout>
Java 文件代码 (HomeFragment.java):
//////////// HS Product Layout (HS = Horizontal Scroll)
hSlayoutTextView = view.findViewById(R.id.horizontal_scroll_layout_title);
hSViewMoreTextView = view.findViewById(R.id.horizontal_scroll_view_more);
hSRecyclerView = view.findViewById(R.id.horizontal_product_recycler_view);
List<HorizontalProductScrollModel> horizontalProductScrollModelList = new ArrayList<>();
horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.drawable.brd, "₹35", "₹40", "English Oven Premium \n" + "Sandwich Bread", "350 g"));
.
.
.
.
.
horizontalProductScrollModelList.add(new HorizontalProductScrollModel(R.drawable.brd, "₹35", "₹40", "English Oven Premium \n" + "Sandwich Bread", "350 g"));
HorizontalProductScrollAdapter horizontalProductScrollAdapter = new HorizontalProductScrollAdapter(horizontalProductScrollModelList);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
hSRecyclerView.setLayoutManager(linearLayoutManager);
hSRecyclerView.setAdapter(horizontalProductScrollAdapter);
horizontalProductScrollAdapter.notifyDataSetChanged();
/////// HS Product Layout
适配器类 (HorizontalProductScrollAdapter.java)
公共类 HorizontalProductScrollAdapter 扩展 RecyclerView.Adapter {
private List<HorizontalProductScrollModel> horizontalProductScrollModelList;
public HorizontalProductScrollAdapter(List<HorizontalProductScrollModel> horizontalProductScrollModelList) {
this.horizontalProductScrollModelList = horizontalProductScrollModelList;
}
@NonNull
@Override
public HorizontalProductScrollAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_scroll_item_layout, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull HorizontalProductScrollAdapter.ViewHolder viewHolder, int position) {
int resource = horizontalProductScrollModelList.get(position).getProductImage();
String price = horizontalProductScrollModelList.get(position).getProductPrice();
String mrp = horizontalProductScrollModelList.get(position).getProductMRP();
String name = horizontalProductScrollModelList.get(position).getProductName();
String weight = horizontalProductScrollModelList.get(position).getProductWeight();
viewHolder.setProductImage(resource);
viewHolder.setProductPrice(price);
viewHolder.setProductMRP(mrp);
viewHolder.setProductName(name);
viewHolder.setProductWeight(weight);
}
@Override
public int getItemCount() {
return horizontalProductScrollModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private ImageView productImage;
private TextView productPrice;
private TextView productMRP;
private TextView productName;
private TextView productWeight;
public ViewHolder(@NonNull View itemView) {
super(itemView);
productImage = itemView.findViewById(R.id.hs_product_image);
productPrice = itemView.findViewById(R.id.hs_product_price);
productMRP = itemView.findViewById(R.id.hs_product_mrp);
productName = itemView.findViewById(R.id.hs_product_name);
productWeight = itemView.findViewById(R.id.hs_product_weight);
}
private void setProductImage(int resource){
productImage.setImageResource(resource);
}
private void setProductPrice(String price){
productPrice.setText(price);
}
private void setProductMRP(String mrp) {
productMRP.setText(mrp);
}
private void setProductName(String name) {
productName.setText(name);
}
private void setProductWeight(String weight) {
productWeight.setText(weight);
}
}
}
我看到了两个文本视图,但没有看到任何产品。为了更好地参考我想要实现的目标,我在下面添加了两个屏幕截图的链接:
我得到了什么:https ://snag.gy/uKfVT4.jpg 我想要什么:https ://snag.gy/n2GHyg.jpg
编辑:日志显示此 E/RecyclerView:未连接适配器;跳过布局我在哪里附加它?