title: Create RecyclerView IN FRAGMENT
body: <p>I want to create this UI in recyclerView in Fragment and how to call from other fragment.<br>
    It has TextView and ImageView.I am getting error in Adapter ViewHolder Line.
       I have done this.Can any one help me?</p>
  
  <p><a href="https://i.stack.imgur.com/f1Rt6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f1Rt6.png" alt="enter image description here"></a></p>
  
  <p>ModelAdapter.java  </p>
  
  <pre><code>   public class ModelAdapter extends       RecyclerView.Adapter&lt;ModelAdapter.MyViewHolder&gt; {
  
    private List&lt;ItemData&gt; itemsData;
    public ImageView carBrandImg;
  public TextView carBrandName;
  
  
  
  public class MyViewHolder extends RecyclerView.ViewHolder {
  
  
  
      public MyViewHolder(View itemView) {
          super(itemView);
          carBrandImg = (ImageView) itemView.findViewById(R.id.carBrandImg);
          carBrandName = (TextView) itemView.findViewById(R.id.carBrandName);
      }
  }
  
  
  
  
  
  public ModelAdapter(ItemData[] itemsData) {
      this.itemsData = itemsData;
  }
  
  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View itemView = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.row_model, parent, false);
  
      return new MyViewHolder(itemView);
  }
  
  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {
      ItemData brand = itemsData.get(position);
      MyViewHolder.carBrandImg.setImageResource(brand.getImg());
      MyViewHolder.carBrandName.setText(brand.getText());
  
  }
  
  @Override
  public int getItemCount() {
      return itemsData.size();
  }
  }
  </code></pre>
  
  <p>ItemData.java</p>
  
  <pre><code>public class ItemData {
  String text,img;
  
  
  public String getText() {
      return text;
  }
  
  public void setText(String text) {
      this.text = text;
  }
  
  public String getImg() {
      return img;
  }
  
  public void setImg(String img) {
      this.img = img;
  }
  
  public ItemData(String text, String img) {
  
      this.text = text;
      this.img = img;
  }
  }
  </code></pre>
  
  <p>MyFragment.java</p>
  
  <pre><code>  public class MyFragment extends Fragment {
  
  
  public MyFragment() {
      // Required empty public constructor
  }
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
  
      View rootView = inflater.inflate(R.layout.fragment_my, container, false);
      // 1. get a reference to recyclerView
      RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.ls);
  
      // 2. set layoutManger
      recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
  
      // this is data fro recycler view
      ItemData itemsData[] = {
              new ItemData("Indigo",R.drawable.abc),
              new ItemData("Red",R.drawable.xyz),
              new ItemData("Blue",R.drawable.pqr),
              new ItemData("Green",R.drawable.def),
  
      };
  
  
      // 3. create an adapter
      ModelAdapter mAdapter = new ModelAdapter(itemsData);
      // 4. set adapter
      recyclerView.setAdapter(mAdapter);
      // 5. set item animator to DefaultAnimator
      recyclerView.setItemAnimator(new DefaultItemAnimator());
  
  
      return rootView;
  }
   }
  </code></pre>
  
  <p>fragment_my.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"&gt;
  
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="70dp"
      android:id="@+id/headermodel"
      android:background="#9294a3"&gt;
  
      &lt;TextView
          android:layout_width="35dp"
          android:text="1"
          android:textColor="#c2bbbb"
          android:gravity="center"
          android:textSize="30sp"
          android:layout_gravity="center"
          android:layout_marginLeft="15dp"
  
          android:layout_height="35dp" /&gt;
  
      &lt;TextView
          android:layout_width="35dp"
          android:text="2"
          android:textColor="#fdfcfc"
          android:gravity="center"
          android:textSize="30sp"
          android:layout_gravity="center"
          android:layout_marginLeft="15dp"
  
          android:layout_height="35dp" /&gt;
  
      &lt;TextView
          android:layout_width="220dp"
          android:layout_height="match_parent"
          android:text="Pick Your Model"
          android:textSize="20sp"
          android:paddingLeft="15dp"
          android:textColor="#faf8f8"
          android:gravity="center_vertical" /&gt;
  
  
      &lt;TextView
          android:layout_width="35dp"
          android:text="3"
          android:textColor="#c2bbbb"
          android:gravity="center"
          android:textSize="30sp"
          android:layout_gravity="center"
          android:layout_marginLeft="15dp"
  
          android:layout_height="35dp" /&gt;
  
  
  
  &lt;/LinearLayout&gt;
  
  &lt;android.support.v7.widget.RecyclerView
      android:layout_width="fill_parent"
      android:id="@+id/ls"
      android:layout_below="@+id/headermodel"
      android:layout_height="fill_parent"&gt;
  
  &lt;/android.support.v7.widget.RecyclerView&gt;
  
   &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>row_model.xml</p>
  
  <pre><code>  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"&gt;
  
  
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="50dp"
          android:layout_margin="10dp"
          android:layout_marginStart="40dp"
          android:orientation="horizontal"&gt;
  
          &lt;ImageView
              android:id="@+id/carBrandImg"
              android:layout_width="70dp"
              android:layout_height="match_parent" /&gt;
  
          &lt;TextView
              android:id="@+id/carBrandName"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center_vertical"
              android:paddingLeft="20dp"
              android:text="Renault"
              android:textColor="#2e2727"
              android:textSize="20sp" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>