Defining item click listener for RecyclerView in Android

Android RecyclerView doesn’t come with the Item Click Listener that you have with ListView. You will have to implement the item clicke listener yourself. Here is an example of how to implement the view holder item clicker listener in RecyclerView. In a nutshell, The Activity class will implement an interface for onClick event, this interface will be passed to the RecyclerView Adapter class, then the ViewHolder class in the RecyclerView will call onClick method defined in the interface, which will pass the view and position of the clicked item to the onClick implementation in the Activity class.

1. We will need a listener interface.

public interface OnItemClickListener {
    public void onClick(View view, int position);
}

2. In your ViewHolder class in your RecyclerView adapter, implement View.OnClickListener, bind the listener to the view. In the onClick method, call the onClick method of the interface OnItemClickListener. This should be passed in from your RecycyclerView’s constructor. The actual implementation of the onclick event will be from an activity or fragment that contains this RecyclerView. The important line here is clickListener.onClick(view, getPosition()); where clickListener is a global variable in your RecyclerView class, again it should’ve passed in from your RecyclerView’s constructor.

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView cityName;
    public ViewHolder(View view) {
        super(view);
        cityName = (TextView) view.findViewById(R.id.city_name);
        itemView.setOnClickListener(this); // bind the listener
    }
    @Override
    public void onClick(View view) {
        clickListener.onClick(view, getPosition()); // call the onClick in the OnItemClickListener
    }
}

3. The onClick implementation in the Activity class, the important line here is mAdapter.setClickListener(this); and the onClick method. The onClick method gets triggered from the ViewHolder’s onClick method in your RecyclerView class, which passes the view and position of the clicked item.

public class CityActivity extends Activity implements ItemClickListener {

    private RecyclerView mRecyclerView;
    private CityAdapter mAdapter;
    private List<City> cities;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        cities = CityManager.getInstance(this.getApplicationContext()).getCites();
        mRecyclerView = (RecyclerView)findViewById(R.id.list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mAdapter = new CityAdapter(cities, R.layout.row_city, this);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setClickListener(this); // Bind the listener
    }

    @Override
    public void onClick(View view, int position) {
        // The onClick implementation of the RecyclerView item click
        final City city = cities.get(position);
        Intent i = new Intent(this, CityviewActivity.class);
        i.putExtra("city", city.name);
        i.putExtra("desc", city.description);
        i.putExtra("image", city.imageName);
        Log.i("hello", city.name);
        startActivity(i);
    }
}

4. All together:
The interface class, ItemClickListener.java

public interface ItemClickListener {
    void onClick(View view, int position);
}

The RecyclerView Adapter class, CityAdapter.java

public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder>{

    private List<City> cities;
    private int rowLayout;
    private Context mContext;
    private ItemClickListener clickListener;

    public CityAdapter(List<City> cities, int rowLayout, Context context) {
        this.cities = cities;
        this.rowLayout = rowLayout;
        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        final City city = cities.get(position);
        viewHolder.cityName.setText(city.name);
        viewHolder.cityImage.setImageDrawable(mContext.getResources().getDrawable(city.getImageResourceId(mContext)));
    }

    @Override
    public int getItemCount() {
        return cities == null ? 0 : cities.size();
    }

    public void setClickListener(ItemClickListener itemClickListener) {
        this.clickListener = itemClickListener;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView cityName;
        public ImageView cityImage;
        public ViewHolder(View itemView) {
            super(itemView);
            cityName = (TextView) itemView.findViewById(R.id.city_name);
            cityImage = (ImageView)itemView.findViewById(R.id.city_image);
            itemView.setTag(itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
           if (clickListener != null) clickListener.onClick(view, getAdapterPosition());
        }
    }
}

The Activity class, CityActivity.java

public class CityActivity extends Activity implements ItemClickListener {
    private RecyclerView mRecyclerView;
    private CityAdapter mAdapter;
    private List<City> cities;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        cities = CityManager.getInstance(this.getApplicationContext()).getCites();
        mRecyclerView = (RecyclerView)findViewById(R.id.list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mAdapter = new CityAdapter(cities, R.layout.row_city, this);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setClickListener(this);
    }

    @Override
    public void onClick(View view, int position) {
        final City city = cities.get(position);
        Intent i = new Intent(this, CityviewActivity.class);
        i.putExtra("city", city.name);
        i.putExtra("desc", city.description);
        i.putExtra("image", city.imageName);
        Log.i("hello", city.name);
        startActivity(i);
    }
}

The layout files are not provided for above sample are not provided since this post is not about layout.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search