2

I decided to make an e-commerce store app and I want to include two or more same layouts which contains a recycler view which takes it's data from java code (for now).

I tried to add different ids for both of them but I don't see the products

Layout I want to include (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>

What I'm doing (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 File Code (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

Adapter Class (HorizontalProductScrollAdapter.java)

public class HorizontalProductScrollAdapter extends 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);
    }
}

}

I see the two text views but I don't see any of the products. For better reference of what I want to achieve, I've added a link below with both the screenshots:

What I am getting: https://snag.gy/uKfVT4.jpg What I want: https://snag.gy/n2GHyg.jpg

EDIT: Log shows this E/RecyclerView: No adapter attached; skipping layout where do I attach it?

Lavish
  • 21
  • 3

0 Answers0