When it's come to display list of items in android the best and ease way is to use RecyclerView.
RecyclerView is android widget which uses ViewHolder patter to save the inflated views,so that when user scroll the list then views are recycle and not created again.
Recycling of views is very use full from memory and performance perspective.
So,In this tutorial,
We gone a build simple example,we create MyRecyclerViewAdapter which extends from RecyclerView.Adapter class,this class is the adapter class which hold the data and view which we display in RecyclerView.
We need layout which represent list item,which we create in word_list_item.xml file.
We Also create activity_main.xml and MainActivity.Java to display Item List.
TrendyProgrammer's Code is well commented so you can find out what is going on in code,by just Reading it.
So Let's Look the Code:
MyRecyclerViewAdapter.Java
word_list_item.xml
MainActivity.Java
activity_main.xml
Please Leave your Comments and Let Us Know Your Thoughts About This Tutorial.
RecyclerView is android widget which uses ViewHolder patter to save the inflated views,so that when user scroll the list then views are recycle and not created again.
Recycling of views is very use full from memory and performance perspective.
So,In this tutorial,
We gone a build simple example,we create MyRecyclerViewAdapter which extends from RecyclerView.Adapter class,this class is the adapter class which hold the data and view which we display in RecyclerView.
We need layout which represent list item,which we create in word_list_item.xml file.
We Also create activity_main.xml and MainActivity.Java to display Item List.
TrendyProgrammer's Code is well commented so you can find out what is going on in code,by just Reading it.
So Let's Look the Code:
MyRecyclerViewAdapter.Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.trendyprogrammer.recylerviewdemo; | |
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.LinkedList; | |
import static com.example.android.recyclerview.R.string.clickedText; | |
/** | |
* MyRecylerViewAdapter class , shows how to implement a simple Adapter for RecyclerView. | |
* And also shows how to implement click event handler for each item in th viewHolder | |
*/ | |
public class MyRecyclerViewAdapter | |
extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> { | |
private final LinkedList<String> listOfWord; | |
private final LayoutInflater inflater; | |
public MyViewHolder extends RecyclerView.ViewHolder | |
implements View.OnClickListener | |
{ | |
public final TextView wordItemView; | |
final MyViewHolder adapter; | |
/** | |
* Let's create the custome view holder to hold the view | |
* which display in RecyclerView | |
* | |
* @param itemView The view in which we display the data. | |
* @param adapter the adapter object which manages view and | |
* data for the RecyclerView | |
* | |
*/ | |
public MyViewHolder(View itemView, MyRecyclerViewAdapter adapter) { | |
super(itemView); | |
wordItemView = (TextView) itemView.findViewById(R.id.textViewWord); | |
this.adapter = adapter; | |
itemView.setOnClickListener(this); | |
} | |
@Override | |
public void onClick (View v){ | |
//Let's change the textView when user click the RecyclerView item. | |
String text = v.getContext().getString(clickedText) | |
wordItemView.setText(text); | |
} | |
} | |
public WordListAdapter(Context context, LinkedList<String> wordList) { | |
inflater = LayoutInflater.from(context); | |
this.listOfWord = wordList; | |
} | |
/** | |
* Inflates an item view and returns a new view holder that contains it. | |
* Called when the RecyclerView needs a new view holder to represent an item. | |
* | |
* @param parent The view group that holds the item views. | |
* @param viewType Used to distinguish views, if more than one | |
* type of item view is used. | |
* @return a view holder. | |
*/ | |
@Override | |
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
// Inflate an item view. | |
View itemView = mInflater.inflate(R.layout.word_list_item, parent, false); | |
return new MyViewHolder(itemView, this); | |
} | |
/** | |
* Let's inflate the new View and create new View Holder that contain it. | |
* this method called when Recycler View need new View Holder to represent | |
* new item | |
* | |
* @param parant The view group perent that holds the item views. | |
* @param position the position of the item start with 0 in the RecyclerView. | |
*/ | |
@Override | |
public void onBindViewHolder(MyViewHolder holder, int postion) { | |
//Let's get the data from list . | |
String currentObj = listOfWord.get(position); | |
// Let' add the data to the view Holder. | |
holder.wordItemView.setText(currentObj); | |
} | |
/* | |
* Return the size of the list of items | |
* | |
* @return size list size | |
*/ | |
@Override | |
public int getItemCount() { | |
return listOfWord.size(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
android:padding="4dp"> | |
<TextView | |
android:id="@+id/textViewWord" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:textSize="24sp" | |
android:textStyle="bold"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.trendyprogrammer.recylerviewdemo; | |
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.View; | |
import java.util.LinkedList; | |
/** | |
* MainActivity class, Implements RecyclerView that display a list of words. | |
* - When you click the item we mark the item as clicked. | |
* - When you click the fab button we add new word to the list of word. | |
*/ | |
public class MainActivity extends AppCompatActivity{ | |
private final LinkedList<String> wordList = new LinkedList<>(); | |
private RecyclerView recyclerView; | |
private MyRecyclerViewAdapter myRecyclerViewAdapter; | |
@Override | |
public void onCreate(Bundle savedInstanceState){ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//put initial item data into the word list. | |
for(int i = 0 ; i < 20 ; i++){ | |
wordList.addLast("Word" + i); | |
} | |
// Create the RecyclerView | |
recyclerView = (RecyclerView) findViewById(R.id.recyclerView); | |
// Let's create the adapter object and supply the data to be displayed. | |
myRecyclerViewAdapter = new MyRecyclerViewAdapter(this,wordList); | |
// Connect the adapte with the recyclerView. | |
recyclerView.setAdapter(myRecyclerViewAdapter); | |
//Let's set the recyclerView default layout manager | |
recyclerView.setLayoutManager(new LinearLayoutManager(this)); | |
//Let's create the floating action button click handler for adding new | |
//list item. | |
FloatingActionButton fab = (FloatingActionbutton) | |
findViewById(R.id.button_fab); | |
fab.setOnClickListener(new View.OnClickListener(){ | |
int listWordSize = wordList.size(); | |
// Let's add new word to the wordList. | |
wordList.addLast("Word"+ listWordSize)); | |
//Notify the adapter that new data is inserted. | |
recyclerView.getAdapter().notifyItemInserted(listWordSize); | |
//Let's scroll to the buttom. | |
recyclerView.smoothScrollToPosition(listWordSize); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.design.widget.CoordinatorLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recyclerview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
</android.support.v7.widget.RecyclerView> | |
<android.support.design.widget.FloatingActionButton | |
android:id="@+id/button_fab" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="bottom|end" | |
android:layout_margin="14dp" | |
android:clickable="true" | |
android:src="@drawable/ic_icon" /> | |
</android.support.design.widget.CoordinatorLayout> |