Recycle View in Android

Hello All, We are going to know something about using Recyclerview in Android. Recyclerview Introduction: • Recyclerview is the most advanced version of list view, used to display collection of data in an efficient way. • Any page which needs a list of items to be displayed consumes more memory/reference to create more number of views and load it in a list

Recycle View in Android
• This is a major issue in the apps’ performance when considering apps with feeds or data listing • To fix this issue, Recyclerview are used generally. • This will use only a certain number of views and recycle it for displaying huge amount of feeds or listing data • Recyclerview provides Layout managers to display data. There are three types of layout managers
  1. LinearLayoutManager
  2. GridLayoutManager
  3. StaggeredGridLayoutManager
• Recyclerview provides animation effect on Adding and Deleting data by using RecyclerView.setItemAnimator() method • RecyclerView.ItemDecoration method is used to add divider between two items. Implementation: 1. Create Project→ New Project → Empty activity → finish 2. Add following dependencies in gradle file.
dependencies {
compile 'com.android.support:recyclerview-v7:23.4.0'
}
3. Add a Recycler View in your xml layout as below
 <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent">
 </android.support.v7.widget.RecyclerView>
4.Set Layout manager to Recyclerview like below
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
5.Before adding data adapter to Recyclerview create getter and setter to data items. Create java class DataModel.java and declare the name and department
DataModel.java
package com.example.myapplication;
public class DataModel {
    String name;
    String Department;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDepartment() {
        return Department;
    }
    public void setDepartment(String department) {
        Department = department;
    }
}
6. Create a layout like row_item.Xml with the below code
 <?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="match_parent" android:padding="16dp" android:orientation="vertical">
    <TextView
        android:id="@+id/name"
        android:text="name"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="department"/>
</LinearLayout>
7.Create Recycler adapter class DataAdapter.java like below
package com.example.myapplication;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class DataAdapter extends RecyclerView.Adapter {
    ArrayListmdatalist;
    public DataAdapter(ArrayList datalist) {
        this.mdatalist=datalist;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DataModel dataModel=mdatalist.get(position);
        holder.name.setText(dataModel.getName());
        holder.department.setText(dataModel.getDepartment());
    }
    @Override
    public int getItemCount() {
        return mdatalist.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView name, department;
        public ViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.name);
            department = (TextView) view.findViewById(R.id.department);
        }
    }
}
8.Set data to adaptor like the below activity
public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    DataModel datamodel;
    ArrayList datalist=new ArrayList<>();
    DataAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitymain);
        recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this));
        adapter=new DataAdapter(datalist);
        recyclerView.setAdapter(adapter);
        data();
    }
    private void data() {
        datamodel = new DataModel("Nilanchala Panigrahy","MEDICAL");
        datalist.add(datamodel);
        datamodel = new DataModel("Panigrahy Kendry","IT");
        datalist.add(datamodel);
        datamodel = new DataModel("Haasan Panigrahy","WAMDS");
        datalist.add(datamodel);
        datamodel = new DataModel("Neshany","IT");
        datalist.add(datamodel);
        datamodel = new DataModel("Helen atmir","WAMDS");
        datalist.add(datamodel);
        datamodel = new DataModel("Rabhu Ademoh","IT");
        datalist.add(datamodel);
        datamodel = new DataModel("Kunal Chawla","MEDICAL");
        datalist.add(datamodel);
        datamodel = new DataModel("Ankit pandey","FINANCE");
        datalist.add(datamodel);
        datamodel = new DataModel("Akinyemi john","IT");
        datalist.add(datamodel);
        datamodel = new DataModel("Denis Shahov","WAMDS");
        datalist.add(datamodel);
        adapter.notifyDataSetChanged();
    }
}
9. Adding divider to recycler view is done like below
public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private Drawable mDivider;
    public DividerItemDecoration(Context context) {
        mDivider = context.getResources().getDrawable(R.drawable.line_divider);
    }
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}
10. Run the program and get an output like below   recycle-view

Write Your Comment

Only registered users can write comments. Please, log in or register