Question Detail

How to implement recyclerView android?

6 years ago Views 1619 Visit Post Reply

Thread Reply

Bili Greed

- 6 years ago

Below is the RecyclerView widget with necessary attributes.

< android:id="@+id/recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"/>

add dependencies in your gradle file

dependencies { compile '' compile '' }

With the latest version of build tools, Android Studio is creating two layout files for each activity. For main activity, it created activity_main.xml (contains CoordinatorLayout and AppBarLayout) and content_VBagemain.xml (for the actual content). Open content_VBagemain.xml and the recycler view widget.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="" xmlns:tools="" xmlns:app="" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/activity_main" tools:context=".MainActivity"> android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" />

4. Open colors.xml located under res ⇒ values and add below colors.


<?xml version="1.0" encoding="utf-8"?>


    <color name="colorPrimary">#3F51B5</color>

    <color name="colorPrimaryDark">#303F9F</color>

    <color name="colorAccent">#FF4081</color>

    <color name="year">#999999</color>

    <color name="title">#222222</color>



2. Writing the Adapter Class

After adding the RecyclerView widget, let’s start writing the adapter class to render the data. The RecyclerView adapter is same as ListView but the override methods are different.

5. Create a class named and declare title, genre and year. Also add the getter/setter methods to each variable.

package qna.vbagetech.example.recyclerview; public class Movie {     private String title, genre, year;     public Movie() {     }     public Movie(String title, String genre, String year) {         this.title = title;         this.genre = genre;         this.year = year;     }     public String getTitle() {         return title;     }     public void setTitle(String name) {         this.title = name;     }     public String getYear() {         return year;     }     public void setYear(String year) {         this.year = year;     }     public String getGenre() {         return genre;     }     public void setGenre(String genre) {         this.genre = genre;     } }

6. Create an layout xml named vbmovie_list_row.xml with the below code. This layout file renders a single row in recycler view by displaying movie name, genre and year of release.


<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=""     android:layout_width="match_parent"     android:layout_height="match_parent"     android:focusable="true"     android:paddingLeft="16dp"     android:paddingRight="16dp"     android:paddingTop="10dp"     android:paddingBottom="10dp"     android:clickable="true"     android:background="?android:attr/selectableItemBackground"     android:orientation="vertical">     <TextView         android:id="@+id/title"         android:textColor="@color/title"         android:textSize="16dp"         android:textStyle="bold"         android:layout_alignParentTop="true"         android:layout_width="match_parent"         android:layout_height="wrap_content" />       <TextView         android:id="@+id/genre"         android:layout_below="@id/title"         android:layout_width="match_parent"         android:layout_height="wrap_content" />       <TextView         android:id="@+id/year"         android:textColor="@color/year"         android:layout_width="wrap_content"         android:layout_alignParentRight="true"         android:layout_height="wrap_content" /> </RelativeLayout>

7. Now create a class named and add the below code. Here onCreateViewHolder()method inflates movie_list_row.xml. In onBindViewHolder() method the appropriate movie data (title, genre and year) set to each row.

package qna.vbagetech.example.recyclerview;   import; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;   import java.util.List; public class VBAdapter extends RecyclerView.Adapter<VBAdapter.MyViewHolder> {     private List<Movie> moviesList;     public class MyViewHolder extends RecyclerView.ViewHolder {         public TextView title_vb, year_vb, genre_vb;           public MyViewHolder(View view) {             super(view);             title_vb = (TextView) view.findViewById(;             genre_vb = (TextView) view.findViewById(;             year_vb = (TextView) view.findViewById(;         }     }     public MoviesAdapter(List<Movie> moviesList) {         this.moviesList = moviesList;     }     @Override     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {         View itemView = LayoutInflater.from(parent.getContext())                 .inflate(R.layout.vbmovie_list_row, parent, false);           return new MyViewHolder(itemView);     }       @Override     public void onBindViewHolder(MyViewHolder holder, int position) {         Movie movie = moviesList.get(position); holder.title_vb.setText(movie.getTitle()); holder.genre_vb.setText(movie.getGenre()); holder.year_vb.setText(movie.getYear());     }       @Override     public int getItemCount() {         return moviesList.size();     } }

8. Now open and do the below changes. Here prepareMovieData() method adds sample data to list view.

package qna.vbagetech.example.recyclerview; import android.content.Context; import android.os.Bundle; import; import; import; import; import; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity {     private List<Movie> movieList = new ArrayList<>();     private RecyclerView recyclerView;     private MoviesAdapter mAdapter;       @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         Toolbar toolbar = (Toolbar) findViewById(;         setSupportActionBar(toolbar);           recyclerView = (RecyclerView) findViewById(;           mAdapter = new MoviesAdapter(movieList);         RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());         recyclerView.setLayoutManager(mLayoutManager);         recyclerView.setItemAnimator(new DefaultItemAnimator());         recyclerView.setAdapter(mAdapter);           prepareMovieData();     }       private void prepareMovieData() {         Movie movie = new Movie("Mad Max: Fury Road", "Action & Adventure", "2015");         movieList.add(movie);           movie = new Movie("Inside Out", "Animation, Kids & Family", "2015");         movieList.add(movie);           movie = new Movie("Star Wars: Episode VII - The Force Awakens", "Action", "2015");         movieList.add(movie);           movie = new Movie("Shaun the Sheep", "Animation", "2015");         movieList.add(movie);           movie = new Movie("The Martian", "Science Fiction & Fantasy", "2015");         movieList.add(movie);           movie = new Movie("Mission: Impossible Rogue Nation", "Action", "2015");         movieList.add(movie);           movie = new Movie("Up", "Animation", "2009");         movieList.add(movie);           movie = new Movie("Star Trek", "Science Fiction", "2009");         movieList.add(movie);           movie = new Movie("The LEGO Movie", "Animation", "2014");         movieList.add(movie);           movie = new Movie("Iron Man", "Action & Adventure", "2008");         movieList.add(movie);           movie = new Movie("Aliens", "Science Fiction", "1986");         movieList.add(movie);           movie = new Movie("Chicken Run", "Animation", "2000");         movieList.add(movie);           movie = new Movie("Back to the Future", "Science Fiction", "1985");         movieList.add(movie);           movie = new Movie("Raiders of the Lost Ark", "Action & Adventure", "1981");         movieList.add(movie);           movie = new Movie("Goldfinger", "Action & Adventure", "1965");         movieList.add(movie);           movie = new Movie("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014");         movieList.add(movie);           mAdapter.notifyDataSetChanged();     } }