Friday, 19 February 2016

RecyclerView in android studio


RecyclerView in the advanced version of listview.

RecyclerView simplifies the display and handle of large set of data.

Its coming with default animations.

RecyclerView provides layout managers for positioning items

RecyclerView uses a ViewHolder to store references to the views for one entry in the recyclerView.

RecyclerView example
  This is my example for listing items in the RecyclerView.

Adding Support Library
For using RecyclerView in your project you need to add the recycler view support library to your project. 
Add the following graddle dependency to project build.graddle file.

  compile 'com.android.support:appcompat-v7:23.1.1'  
   compile 'com.android.support:design:23.1.1'  
   compile 'com.android.support:recyclerview-v7:23.1.1'  


Activity Layout
Add android.support.v7.widget.RecyclerView into activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context="comre.example.velmurugan.recycleviewdemo.MainActivity">  
   <android.support.v7.widget.RecyclerView  
     android:id="@+id/recycleView"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="Hello World!" />  
 </RelativeLayout>  

Items.Java
 public class Items {  
   private String name;  
   private int price;  
   Items(String mName,int mPrice){  
     this.name = mName;  
     this.price = mPrice;  
   }  
   public int getPrice() {  
     return price;  
   }  
   public void setPrice(int price) {  
     this.price = price;  
   }  
   public String getName() {  
     return name;  
   }  
   public void setName(String name) {  
     this.name = name;  
   }  
 }  

ClickListener.java

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

RecyclerView Adapter
 public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.MyViewHolder> {  
   private List<Items> itemsList;  
    class MyViewHolder extends RecyclerView.ViewHolder{  
     public TextView name,price;  
     public MyViewHolder(View itemView) {  
       super(itemView);  
       name = (TextView) itemView.findViewById(R.id.tvName);  
       price = (TextView) itemView.findViewById(R.id.tvPrice);  
     }  
   }  
   ItemAdapter(List<Items> mItemList){  
     this.itemsList = mItemList;  
   }  
   @Override  
   public ItemAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false);  
     return new MyViewHolder(view);  
   }  
   @Override  
   public void onBindViewHolder(ItemAdapter.MyViewHolder holder, int position) {  
     Items item = itemsList.get(position);  
     holder.name.setText(item.getName());  
     holder.price.setText(String.valueOf(item.getPrice()));  
   }  
   @Override  
   public int getItemCount() {  
     return itemsList.size();  
   }  
 }  


RecyclerView touch listener
 public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {  
 private GestureDetector gestureDetector;  
 private ClickListener clickListener;  
 public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {  
     this.clickListener = clickListener;  
     gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {  
 @Override  
 public boolean onSingleTapUp(MotionEvent e) {  
     return true;  
     }  
 @Override  
 public void onLongPress(MotionEvent e) {  
     View child = recyclerView.findChildViewUnder(e.getX(), e.getY());  
     if (child != null && clickListener != null) {  
     clickListener.onLongClick(child, recyclerView.getChildPosition(child));  
     }  
     }  
     });  
     }  
 @Override  
 public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {  
     View child = rv.findChildViewUnder(e.getX(), e.getY());  
     if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {  
     clickListener.onClick(child, rv.getChildPosition(child));  
     }  
     return false;  
     }  
 @Override  
 public void onTouchEvent(RecyclerView rv, MotionEvent e) {  
     }  
 @Override  
 public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {  
     }  
     }  


Main Activity
In MainActivity setup the LayoutManager for the recycluarView.
The layout manager decides how the data in the RecyclerView is displayed.
Add the Touch Listener for the RecycularView.
And set the adapter for the recycularView.


 public class MainActivity extends AppCompatActivity {  
   private RecyclerView recyclerView;  
   private RecyclerTouchListener recycleCTouchListener;  
   private ItemAdapter itemAdapter;  
   private List<Items> itemsList;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     itemsList = new ArrayList<>();  
     recyclerView = (RecyclerView)findViewById(R.id.recycleView);  
     itemAdapter = new ItemAdapter(itemsList);  
     recyclerView.setHasFixedSize(true);  
     RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());  
     recyclerView.setLayoutManager(layoutManager);  
     recyclerView.setItemAnimator(new DefaultItemAnimator());  
     recyclerView.setAdapter(itemAdapter);  
     recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {  
       @Override  
       public void onClick(View view, int position) {  
         Toast.makeText(getApplicationContext(),itemsList.get(position).getName()+" selected ",Toast.LENGTH_SHORT).show();  
       }  
       @Override  
       public void onLongClick(View view, int position) {  
         Toast.makeText(getApplicationContext(),itemsList.get(position).getName()+" long pressed ",Toast.LENGTH_SHORT).show();  
       }  
     }));  
     prepareItems();  
   }  
   private void prepareItems(){  
     Items items = new Items("Pen",20);  
     itemsList.add(items);  
     items = new Items("Pencil",5);  
     itemsList.add(items);  
     items = new Items("Book",30);  
     itemsList.add(items);  
     items = new Items("ink",15);  
     itemsList.add(items);  
     items = new Items("scale",10);  
     itemsList.add(items);  
     itemAdapter.notifyDataSetChanged();  
   }  
 }  

Screenshot:


Download source code




No comments: