Muhammad Talmeez
Muhammad Talmeez

Reputation: 5

Why layout is not attach in RecyclerView Adapter class?

I have the following setup for a recyclerview.

Now the problem is my layout is not attached with the recyclerview adapter, meaning it is not showing when I enter the layout name in the recyclerview adapter class.

fragment_service.xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="8dp" />

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/layout1" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal" />

</LinearLayout>

list_view_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="4">

    <android.support.v7.widget.CardView
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="0.7"
       xmlns:card_view="http://schemas.android.com/apk/res-auto"
       card_view:cardBackgroundColor="#EEEBEB"
       card_view:cardElevation="2dp"
       android:layout_margin="5dp"
       card_view:cardUseCompatPadding="false">

    <LinearLayout
        android:weightSum="2.5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:text="Textview"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.5"
            android:weightSum="4"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv1"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/btn_plumbing_services"
                />
            <ImageView
                android:id="@+id/iv2"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/btn_plumbing_services"
                />
            <ImageView
                android:id="@+id/iv3"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/btn_plumbing_services"
                />

            <ImageView
                android:id="@+id/iv4"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:src="@drawable/btn_plumbing_services"
                />

        </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:weightSum="4"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"/>
        <TextView
            android:id="@+id/tv2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"/>
        <TextView
            android:id="@+id/tv3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"/>
        <TextView
            android:id="@+id/tv4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"/>

    </LinearLayout>


    </LinearLayout>


  </android.support.v7.widget.CardView>

</LinearLayout>

listFragServiceAdapter.java

    public class listFragServiceAdapter extends RecyclerView.Adapter {

    private List<listItems> serviceList;

    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView title,tv1,tv2,tv3,tv4;
        public ImageView iv1,iv2,iv3,iv4;

        public MyViewHolder(View view){
            super(view);
        }

    }

    public listFragServiceAdapter(List<listItems> serviceList) {
        this.serviceList = serviceList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_view_item, viewGroup, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

    }

    @Override
    public int getItemCount() {
        return 0;
    }
}

The main problem in the following part of the code, In second line list_view_item layout is not inflated. Even I can't enter the layout name.I don't know why?

public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_view_item, viewGroup, false);

        return new MyViewHolder(itemView);
    }

Upvotes: 0

Views: 926

Answers (1)

kaushalyap
kaushalyap

Reputation: 13617

Update:

Since there is no autocomplete for item layout, probable causes with fixes

  • item layout not located in the res > layout : Fix - move the item layout to res > layout
  • If file is in right location, then Android Studio/InteliJ not picking up the file: Fix - Go to File > Invalidate Caches/Restart... and click Invalidate and Restart in the dialog.

Reason for item layout not being inflated because you return 0; in getItemCount method.

@Override
public int getItemCount() {
    return 0;
}

In order to fix this set item count to size of the serviceList.

@Override
public int getItemCount() {
    return serviceList.size();
}

Upvotes: 1

Related Questions