Introduction
RecyclerView is one of the most important Views in Android development. Its name itself is a definition. It recycles a View. And, what is that view? It’s a card. The card holds the data of the user, and the RecyclerView displays it.
Think of your visiting card. It carries your information. Now, you make a stamp and it acts like a Recycler and prints your information on the card.
The relation of RecyclerView and CardView in Android is the same. Let us look at the crucial steps required to build your best View application.
- RecyclerView.
- CardView.
- Adapter.
- Holder.
- Data (mostly in list form).
- Send data to the Adapter class and assign that adapter to RecyclerView.
Step 1- Make a RecyclerView
In activity.xml, write the following code.
- <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view_doctor_schedual" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="vertical" />// This is just a sample script. Paste your real code (javascript or HTML) here. if ('this_is'==/an_example/){of_beautifier();}else{var a=b?(c%d):e[f];}
- RecyclerView mSchedualRecyclerView;
- private static RecyclerView.Adapter adapter;
- private RecyclerView.LayoutManager layoutManager;
- private void initRecyclerView() {
- mSchedualRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_doctor_schedual);
- mSchedualRecyclerView.setHasFixedSize(true);
- mSchedualRecyclerView.setItemAnimator(new DefaultItemAnimator());
- layoutManager = new LinearLayoutManager(this);
- mSchedualRecyclerView.setLayoutManager(layoutManager);
- }

- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
- <android.support.v7.widget.CardView android:id="@+id/card_view" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" card_view:cardBackgroundColor="@android:color/transparent" card_view:cardElevation="0dp">
- <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
- <TextView android:id="@+id/text_view_name" android:layout_width="match_parent" android:layout_height="wrap_content" />
- <TextView android:id="@+id/text_view_address" android:layout_width="match_parent" android:layout_height="wrap_content" />
- </LinearLayout>
- </android.support.v7.widget.CardView>
- </LinearLayout>
- public class Customer {
- public static String mCustomerName;
- public static String mCustomerAddress;
- }
- public class CustomerAdapter {}
- public class CustomerAdapter {
- public class CustomerViewHolder extends RecyclerView.ViewHolder {
- public CustomerViewHolder(View itemView) {
- super(itemView);
- }
- }
- }
- public class CustomerAdapter extends RecyclerView.Adapter < CustomerAdapter.CustomerViewHolder > {
- @Override
- public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- return null;
- }
- @Override
- public void onBindViewHolder(CustomerViewHolder holder, int position) {}
- @Override
- public int getItemCount() {
- return 0;
- }
- public class CustomerViewHolder extends RecyclerView.ViewHolder {
- public CustomerViewHolder(View itemView) {
- super(itemView);
- }
- }
- }
- @Override
- public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_layout_schedual, parent, false);
- return new CustomerViewHolder(view);
- }
- List < Customer > mCustomerList;
- public CustomerAdapter(List < Customer > customerList) {
- this.mCustomerList = customerList;
- }
- public class CustomerAdapter extends RecyclerView.Adapter < CustomerAdapter.CustomerViewHolder > {
- List < Customer > mCustomerList;
- public CustomerAdapter(List < Customer > customerList) {
- this.mCustomerList = customerList;
- }
- @Override
- public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_customer, parent, false);
- return new CustomerViewHolder(view);
- }
- @Override
- public void onBindViewHolder(CustomerViewHolder holder, int position) {
- Customer customer = mCustomerList.get(position);
- holder.tvName.setText(customer.mCustomerName);
- holder.tvAddress.setText(customer.mCustomerAddress);
- }
- @Override
- public int getItemCount() {
- return mCustomerList.size();
- }
- public class CustomerViewHolder extends RecyclerView.ViewHolder {
- TextView tvName, tvAddress;
- public CustomerViewHolder(View itemView) {
- super(itemView);
- this.tvName = (TextView) itemView.findViewById(R.id.text_view_name);
- this.tvAddress = (TextView) itemView.findViewById(R.id.text_view_address);
- }
- }
- }
- private List < Customer > setmCustomerList() {
- mCustomerList = new ArrayList < Customer > ();
- Customer customer = new Customer();
- customer.mCustomerName = "First Customer Name";
- customer.mCustomerAddress = "First CustomerAddress";
- customer = new Customer();
- mCustomerList.add(customer);
- customer.mCustomerName = "2nd Customer Name";
- customer.mCustomerAddress = "2nd CustomerAddress";
- customer = new Customer();
- mCustomerList.add(customer);
- customer.mCustomerName = "3rd Customer Name";
- customer.mCustomerAddress = "3rd CustomerAddress";
- mCustomerList.add(customer);
- customer = new Customer();
- customer.mCustomerName = "4th Customer Name";
- customer.mCustomerAddress = "4th CustomerAddress";
- mCustomerList.add(customer);
- return mCustomerList;
- }
- private void initRecyclerView() {
- mSchedualRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_doctor_schedual);
- mSchedualRecyclerView.setHasFixedSize(true);
- //setting animation
- mSchedualRecyclerView.setItemAnimator(new DefaultItemAnimator());
- layoutManager = new LinearLayoutManager(this);
- //binding layout with recycler view
- mSchedualRecyclerView.setLayoutManager(layoutManager);
- //assigning adapter to RecyclerView
- adapter = new CustomerAdapter(setmCustomerList());
- mSchedualRecyclerView.setAdapter(adapter);
- }

Raichand RayPosted Dec 16, 2017, 12:35 AM
This is a good tutorial of crud operation wth Recycler view and search view https://raichand-java.blogspot.in/2017/12/createretrieveupdate-and-delete-row-in.html