Skip to main content

Android Recycler view

RecyclerView is like ListView and GridView which is used to represent collection of items. RecyclerView is recommended when the app needs to display large amount of data or data that change frequently. RecyclerView helps to implement a user interface that runs efficiently.
Let's take an example to show a list of names using recyclerView.
1. Create a new project.
2. Open app-level build.gradle and add the following dependency-
dependencies {    
    compile 'com.android.support:recyclerview-v7:25.0.+'    }
3. Open activity_main.xml and add RecyclerView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="com.gunjan.andriodcodesnippets.MainActivity">
   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/rv_news"
       android:scrollbars="none">
   </android.support.v7.widget.RecyclerView>   </LinearLayout>
4. Create a new layout item_layout.xml to represent each item of list
<?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="wrap_content">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="25sp"
        android:id="@+id/tv_name"/>
</LinearLayout>
(You can customize this layout by adding images, button or other widgets according to your needs)
5. We need to add a custom adapter that creates view holder to represent one item's data. Create a new class named MyCustomAdapter and type the following code- 
public class MyCustomAdapter extends RecyclerView.Adapter<MyCustomAdapter.ViewHolder> {

    ArrayList<String> arrayListNames;
    Context context;
    
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        context = parent.getContext();
        return new ViewHolder(itemView);
    }
    
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        String name = arrayListNames.get(position);
        //Assign name to the textView
        holder.tv_name.setText(name);
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_name;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
        }
    }

    public MyCustomAdapter(ArrayList<String> arrayListNames) {
        this.arrayListNames = arrayListNames;
    }
}
6. Open MainActivity.java and type the following code-
public class MainActivity extends AppCompatActivity{
   
    RecyclerView recyclerView;
    MyCustomAdapter myCustomAdapter;
    ArrayList<String> names;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        names=new ArrayList<>();
        myCustomAdapter=new MyCustomAdapter(names);

        recyclerView=(RecyclerView)findViewById(R.id.rv_news);
        LinearLayoutManager llm=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(llm);
        recyclerView.setAdapter(myCustomAdapter);

        getNames();        
    }

    private void getNames() {
        names.add("Max");
        names.add("Sunny");
        names.add("Shaurya");
        names.add("Arya");
        names.add("Sam");
        names.add("Jay");
        names.add("Ankit");
        names.add("Rahul");
        names.add("Nancy");
        names.add("Molly");

        myCustomAdapter.notifyDataSetChanged();
    }
}
7. Run the project.

Comments

Popular Posts