Updated 28 November 2019
RecyclerView
RecyclerView is a successor view group of ListView and GridView.It has different features from ListView and GridView which makes it powerful.In this blog we will discuss about RecyclerView and its Adapter.
Creating New Project
1. In Android Studio, go to File ⇒ New Project and fill all the details required to create a new project. When it prompts to select a default activity, select Blank Activity and proceed.
2. Open build.gradle and add recycler view dependency.com.android.support:recyclerview-v7:28.0.0 and rebuild the project.
dependencies {
//…
//RecyclerView
implementation ‘com.android.support:recyclerview-v7:28.0.0’
}
create view RecyclerView in activity_main.xml
1 2 3 4 |
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> |
The Card Layout: Card Layout is treated as an item of RecyclerView.
person_list.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto"> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22sp" android:textStyle="bold" /> <TextView android:id="@+id/occupation" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22sp" android:layout_marginBottom="22dp" /> </LinearLayout> |
The Data Class: The class that holds information about recyclerView
1 2 3 4 5 6 7 8 9 |
public class Person { String name,occupation; public Person(String name , String occupation) { this.name = name; this.occupation = occupation; } } |
The ViewHolder: ViewHolder holds information about card layout .
1 2 3 4 5 6 7 8 |
public class MyViewHolder extends RecyclerView.ViewHolder { TextView name,occupation; public MyViewHolder(@NonNull View itemView) { super(itemView); name=itemView.findViewById(R.id.name); occupation=itemView.findViewById(R.id.occupation); } } |
The Adapter: It is responsible for inflating the view and view holder ,binding data to view . It extends RecyclerView.Adapter .There are three main method of RecyclerView.Adapter which are necessary to override
1.onCreateViewHolder: This method only called when new view created. If views are recycled then there is no need to create new view.
2.onBindViewHolder:It sets data to particular items of the RecyclerView.It called every time when it scrolled
3.getItemCount: which Returns the length of the RecyclerView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private List<Person> list; public MyAdapter(List<Person> list){ this.list=list; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent , int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.person_list, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(@NonNull MyViewHolder holder , int position) { Person person=list.get(position); holder.name.setText(person.name); holder.occupation.setText(person.occupation); } @Override public int getItemCount() { return list.size(); } public class MyViewHolder extends RecyclerView.ViewHolder { TextView name,occupation; public MyViewHolder(@NonNull View itemView) { super(itemView); name=itemView.findViewById(R.id.name); occupation=itemView.findViewById(R.id.occupation); } } } |
LayoutManagers
We use LayoutManager class for specifying position of recyclerView’s item.There are three ways of displaying RecyclerView data.
LinearLayoutManager
shows items in a vertical or horizontal scrolling list.GridLayoutManager
shows items in a grid.StaggeredGridLayoutManager
shows items in a staggered grid.
1 2 |
RecyclerView recyclerView=findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext())); |
Set Adapter to RecyclerView
we need to set adapter to recyclerView which we created . we pass list in constructor of MyAdapter
recyclerView.setAdapter(new MyAdapter(list));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class MainActivity extends AppCompatActivity { private List<Person> list=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView=findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext())); recyclerView.setAdapter(new MyAdapter(list)); personData(); } private void personData() { list.add(new Person("John","Student")); list.add(new Person("micheal","singer")); list.add(new Person("harry","engineer")); list.add(new Person("adiclob","chef")); list.add(new Person("sir Do'd","Athlete")); list.add(new Person("Sachin","Cricketer")); list.add(new Person("Jack","Scientist")); list.add(new Person("Hamid","Comedian")); list.add(new Person("Harsh","Student")); } } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
1 comments