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
    • 816
    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 "android.support.v7.widget.RecyclerView" 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 -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/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(R.id.my_recycler_view);
    
    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);
    
    
    
      // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
         mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    
     // specify an adapter
     mAdapter = new MyAdapter(myDataset);
     mRecyclerView.setAdapter(mAdapter);
    

    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) {
                super(v);
                this.adapterClickListener = adapterClickListener;
                mTextView = (TextView) v.findViewById(R.id.text);
    
                v.setOnClickListener(this);
                v.setOnLongClickListener(this);
            }
    
            @Override
            public void onClick(View v) {
                adapterClickListener.onAadapterItemClick(v, getAdapterPosition());
            }
    
            @Override
            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)
        @Override
        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() {
                @Override
                public void onAadapterItemClick(View view, int position) {           
                    if (position != -1) {
                        mDataset.add(position, "add");
                        notifyItemInserted(position);
                    }
                }
    
                @Override
                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() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mDataset.remove(position);
                            notifyItemRemoved(position);
                        }
                    });
                    builder.setNegativeButton("No", null).show();
    
                }
            });
            return vh;
        }
    
        // Replace the contents of a view (invoked by the layout manager)
        @Override
        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)
        @Override
        public int getItemCount() {
            return mDataset.size();
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: