A very simple example of Android RecyclerView
In this post, we will create a basic item list using RecyclerView and implementing the View.OnClickListener. It will display a list of TextViews with item number, when an row is clicked, a log message will be printed with the item number of the view that’s clicked.
Add the RecyclerView support library in your gradle dependencies
compile 'com.android.support:recyclerview-v7:23.1.0'
activity_main.xml, layout file for the main activity which will hold the RecyclerView in this example. You can replace the default generated code with the following.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:id="@+id/item_list" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical" /> </LinearLayout>
list_item.xml, layout file for each of the row in the RecyclerView.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/row_item" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center"/> </LinearLayout>
Item.java, the item class for each item in the list, it can be any other custom class. For demonstration purpose, this class is very simple, only have one field, the name.
public class Item { private String name; public Item(String n) { name = n; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
ItemArrayAdapter.java, this is the adapter for the RecyclerView. This adapter is also a very simple and contains only the minium required methods, constructor and ViewHolder.
import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.codexpedia.list.viewholder.R; import java.util.ArrayList; public class ItemArrayAdapter extends RecyclerView.Adapter<ItemArrayAdapter.ViewHolder> { //All methods in this adapter are required for a bare minimum recyclerview adapter private int listItemLayout; private ArrayList<Item> itemList; // Constructor of the class public ItemArrayAdapter(int layoutId, ArrayList<Item> itemList) { listItemLayout = layoutId; this.itemList = itemList; } // get the size of the list @Override public int getItemCount() { return itemList == null ? 0 : itemList.size(); } // specify the row layout file and click for each row @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(listItemLayout, parent, false); ViewHolder myViewHolder = new ViewHolder(view); return myViewHolder; } // load data in each row element @Override public void onBindViewHolder(final ViewHolder holder, final int listPosition) { TextView item = holder.item; item.setText(itemList.get(listPosition).getName()); } // Static inner class to initialize the views of rows static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView item; public ViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); item = (TextView) itemView.findViewById(R.id.row_item); } @Override public void onClick(View view) { Log.d("onclick", "onClick " + getLayoutPosition() + " " + item.getText()); } } }
MainActivity.java, the main activity, you can replace the default generated code in the MainActivity.java with the following.
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.DefaultItemAnimator; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.codexpedia.list.viewholder.R; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initializing list view with the custom adapter ArrayList <Item> itemList = new ArrayList<Item>(); ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(R.layout.list_item, itemList); recyclerView = (RecyclerView) findViewById(R.id.item_list); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setAdapter(itemArrayAdapter); // Populating list items for(int i=0; i<100; i++) { itemList.add(new Item("Item " + i)); } } }
Search within Codexpedia
Search the entire web