Simple Recyclerview Example

Posted on

It’s a uncomplicated instance of using RecyclerView to display a List inwards vertical as well as horizontal.


To use RecyclerView inwards your Android Studio project, you lot accept to Add Support Libraries of RecyclerView equally dependencies.

Create our custom Adapter as well as ViewHolder for RecyclerView, com.example.androidrecyclerview.RecyclerViewAdapter.java

package com.example.androidrecyclerview;  import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;  import java.util.ArrayList; import java.util.List;  world class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ItemHolder> {      person List<String> itemsName;     person OnItemClickListener onItemClickListener;     person LayoutInflater layoutInflater;      world RecyclerViewAdapter(Context context){         layoutInflater = LayoutInflater.from(context);         itemsName = novel ArrayList<String>();     }      @Override     world RecyclerViewAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {         View itemView = layoutInflater.inflate(R.layout.layout_item, parent, false);         supply novel ItemHolder(itemView, this);     }      @Override     world void onBindViewHolder(RecyclerViewAdapter.ItemHolder holder, int position) {         holder.setItemName(itemsName.get(position));     }      @Override     world int getItemCount() {         supply itemsName.size();     }      world void setOnItemClickListener(OnItemClickListener listener){         onItemClickListener = listener;     }      world OnItemClickListener getOnItemClickListener(){         supply onItemClickListener;     }      world interface OnItemClickListener{         world void onItemClick(ItemHolder item, int position);     }      world void add(int location, String iName){         itemsName.add(location, iName);         notifyItemInserted(location);     }      world void remove(int location){         if(location >= itemsName.size())             return;          itemsName.remove(location);         notifyItemRemoved(location);     }      world static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener{          person RecyclerViewAdapter parent;         TextView textItemName;          world ItemHolder(View itemView, RecyclerViewAdapter parent) {             super(itemView);             itemView.setOnClickListener(this);             this.parent = parent;             textItemName = (TextView) itemView.findViewById(R.id.item_name);         }          world void setItemName(CharSequence name){             textItemName.setText(name);         }          world CharSequence getItemName(){             supply textItemName.getText();         }          @Override         world void onClick(View v) {             lastly OnItemClickListener listener = parent.getOnItemClickListener();             if(listener != null){                 listener.onItemClick(this, getPosition());             }         }     } }  

And the layout for each jail cellular telephone inwards the RecyclerView, layout/layout_item.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:orientation="vertical">      <TextView         android:id="@+id/item_name"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textSize="40dp"/>  </LinearLayout>  

com.example.androidrecyclerview.MainActivity.java

package com.example.androidrecyclerview;  import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.widget.Toast;  world class MainActivity extends ActionBarActivity implements RecyclerViewAdapter.OnItemClickListener{      person RecyclerView myRecyclerView;     person LinearLayoutManager linearLayoutManager;     person RecyclerViewAdapter myRecyclerViewAdapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         //setContentView(R.layout.activity_main);          myRecyclerView = novel RecyclerView(this);          linearLayoutManager =                 novel LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);         /*         linearLayoutManager =                 novel LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);         */          myRecyclerViewAdapter = novel RecyclerViewAdapter(this);         myRecyclerViewAdapter.setOnItemClickListener(this);         myRecyclerView.setAdapter(myRecyclerViewAdapter);         myRecyclerView.setLayoutManager(linearLayoutManager);          setContentView(myRecyclerView);          //insert dummy items         myRecyclerViewAdapter.add(0, "Eric");         myRecyclerViewAdapter.add(1, "Android");         myRecyclerViewAdapter.add(0, "");         myRecyclerViewAdapter.add(2, "Google");         myRecyclerViewAdapter.add(3, "android dev");         myRecyclerViewAdapter.add(0, "android-er.blogspot.com");         myRecyclerViewAdapter.add(4, "Peter");         myRecyclerViewAdapter.add(4, "Paul");         myRecyclerViewAdapter.add(4, "Mary");         myRecyclerViewAdapter.add(4, "May");         myRecyclerViewAdapter.add(4, "Divid");         myRecyclerViewAdapter.add(4, "Frankie");     }      @Override     world void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {         Toast.makeText(this,                 seat + " : " + item.getItemName(),                 Toast.LENGTH_SHORT).show();     } }  

RecyclerView to display a List inwards vertical as well as horizontal Simple RecyclerView exampleDownload the files (Android Studio Format).


Android RecyclerView examples (step-by-step):


– Add Support Libraries of RecyclerView, CardView to Android Studio Project
– Simple RecyclerView instance (this post)
– Place RecyclerView inwards layout XML
– Add as well as take away items to RecyclerView, amongst default animation
– Implement custom ItemDecoration for RecyclerView
– LinearLayoutManager as well as GridLayoutManager for RecyclerView
– set SpanSizeLookup to GridLayoutManager of RecyclerView
– onDraw() as well as onDrawOver() of ItemDecoration for RecyclerView
Example using RecyclerView amongst CardView
– StaggeredGridLayoutManager (Google+ App-like) on RecyclerView
– RecyclerView + CardView instance amongst ImageView – listing available Drawable
– RecyclerView + CardView instance – listing organisation properties using System.getProperties()
– Intent.ACTION_OPEN_DOCUMENT to charge images inwards RecyclerView + CardView
– Gallery-like RecyclerView + CardView example
– RecyclerView + CardView example: amongst Button
– SwipeRefreshLayout, operate amongst RecyclerView