Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • RecyclerView with Event Listener

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 789
    Comment on it

    RecyclerView is the more refine and flexible version of ListView. It is available in v7 supporting package. For using RecyclerView you need to import "" package.
    In order to add RecyclerView into your application, first you need to add RecyclerView in your xml.

    <!-- A RecyclerView with some commonly used attributes -->
    xmlns:android="" />

    Now in your activity class declare the following references:-

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    In onCreate method

    mRecyclerView = (RecyclerView) findViewById(;
    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
      // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
         mRecyclerView.setItemAnimator(new DefaultItemAnimator());
     // specify an adapter
     mAdapter = new MyAdapter(myDataset);

    Now create an adapter class that extends RecyclerView.Adapter class. Inside this class there is an inner class that extends RecyclerView.ViewHolder. The ViewHolder class provides a reference to the view of each data item. The below example also shows how to add listener and some default animation of RecyclerView.

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private List<String> mDataset;
        // Provide a reference to the views for each data item
        // Complex data items may need more than one view per item, and
        // you provide access to all the views for a data item in a view holder
        public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
            // each data item is just a string in this case
            public TextView mTextView;
            public AdapterClickListener adapterClickListener;
            public ViewHolder(View v, AdapterClickListener adapterClickListener) {
                this.adapterClickListener = adapterClickListener;
                mTextView = (TextView) v.findViewById(;
            public void onClick(View v) {
                adapterClickListener.onAadapterItemClick(v, getAdapterPosition());
            public boolean onLongClick(View v) {
                adapterClickListener.onLongClick(v, getAdapterPosition());
                return false;
            public static interface AdapterClickListener {
                public void onAadapterItemClick(View view, int position);
                public void onLongClick(View view, int position);
        // Provide a suitable constructor (depends on the kind of dataset)
        public MyAdapter(List<String> myDataset) {
            mDataset = myDataset;
        // Create new views (invoked by the layout manager)
        public MyAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent,
                                                       int viewType) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.my_text_view, parent, false);
            // set the view's size, margins, paddings and layout parameters
            ViewHolder vh = new ViewHolder(v, new ViewHolder.AdapterClickListener() {
                public void onAadapterItemClick(View view, int position) {           
                    if (position != -1) {
                        mDataset.add(position, "add");
                public void onLongClick(View view, final int position) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(parent.getContext());
                    builder.setMessage("Are you really want to delete this contact");
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                    builder.setNegativeButton("No", null).show();
            return vh;
        // Replace the contents of a view (invoked by the layout manager)
        public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            holder.mTextView.setText(mDataset.get(position) + "");
        // Return the size of your dataset (invoked by the layout manager)
        public int getItemCount() {
            return mDataset.size();

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: