RecyclerView Example with OnItemClick

RecyclerView is used to show the list of items. If you have more than one with the same layout then you can use recyclerview. It has many benefits but the most important one is it only renders the item which is visible to the user. 

This example will check how can we work with the recycler view in the android application and implement the click-listener for the item.

Gradle Changes:- Add this Gradle to your project 
compile 'com.android.support:recyclerview-v7:+'

The thing, we are going to perform:-
1) First add the Gradle file in your project to use the recycler view.
2) Create one layout file and add the text view to show data.
3) Create one new java file for the recycler adapter.

1) Let's add the recycler-view in the XML file.

<RelativeLayout >
    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"     
        android:id="@+id/recyleview"/>
</RelativeLayout>

2) After adding the recycler view we have to create the item for it. So let's create an item for it. For now, we are taking a text-view to show the data.
Recyler_helper.xml:- 

<LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/textname"/>
</LinearLayout>

3) Let bind the layout file with the activity and configure the recycler-view, so that we can check the data. We have to create the array to show the data and set the layout manager which helps the recycler-view to populate the data in a given manner. 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override    
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyleview);

        ArrayList<String> myarray=new ArrayList<>();
        myarray.add("Help");
        myarray.add("Delete");
        myarray.add("Favorite");
        myarray.add("Like");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new MyRecylerAdpter(myarray, this));
}
    @Override    

    public void onClick(View v) {
        Toast.makeText(this,"click"+ v.getTag(),Toast.LENGTH_SHORT).show();
    }
}

In the end, create the adapter for the recycler-view and register the click of the item.

public class MyRecylerAdpte extends RecyclerView.Adapter<MyRecylerAdpter.ViewHolder {

View.OnClickListener listener;
ArrayList<String> item;

public MyRecylerAdpter(ArrayList<String> Item,View.OnClickListener listener) {
        this.item=Item;
        this.listener=listener;  
 }

@Override    
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyler_helper,null);
        ViewHolder viewHolder=new ViewHolder(v);
        return  viewHolder;
}

@Override   
public void onBindViewHolder(ViewHolder holder, int position) {
        holder.txtname.setText(item.get(position));
        holder.txtname.setTag(item.get(position)+"");
        holder.txtname.setOnClickListener(listener);
}

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

public static class ViewHolder extends RecyclerView.ViewHolder {

  public TextView txtname;

    public ViewHolder(View itemLayoutView) {
            super(itemLayoutView);
            txtname = (TextView) itemLayoutView.findViewById(R.id.textname);
     }
  }
}

We are passing the activity reference to binding the item click in the activity, you can pass any reference like fragment, activity, or interface to bind the click listener and get the item click at the right place. I will post more blogs to the recycler-view,  so it gets easy to use and understand.

Output : -
      









Comments