RecyclerView
Android L support library include two new widget : CardLayout and RecyclerView.
In this tutorial we will explain the RecyclerView widget.
Android L support library include two new widget : CardLayout and RecyclerView.
In this tutorial we will explain the RecyclerView widget.
- RecyclerView is more flexible than Listview.
- when we have a large amount of data to bind in list we use a ViewHolder in ListView . ViewHolder class hold the reference of UI component for each row item in ListView. In ListView, ViewHolder avoids looking up the UI components all the time the system showsa row in the List. RecyclerView forces us to use the ViewHolder Pattern.
- RecyclerView is more efficient and flexible than ListView.
RecyclerView Example
NOTE: Recycler View Widget available in Android L preview SDK.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:recyclerview-v7:+'
}
our project compile with this library i.e. available in dependencies.
RecyclerView Layout is available in main_activity.xml
activity_main.xml
<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" tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FF9933">
<TextView
android:id="@+id/tool_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RecyclerViewDemo"
android:textColor="#ffffff"
android:layout_marginLeft="30dp"/>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"/>
</RelativeLayout>
RecyclerView Implementation
MainActivity.java
package program.androidheight.recyclerviewdemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
Context context;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context= MainActivity.this;
recyclerView = (RecyclerView)findViewById(R.id.recycler_list);
RecyclerModal itemsData[] = {new RecyclerModal("Ramesh", "1234567890", R.drawable.ic_launcher),
new RecyclerModal("suresh", "2222222562", R.drawable.ic_launcher),
new RecyclerModal("prem", "2344322341", R.drawable.ic_launcher),
new RecyclerModal("yogesh", "4567765412", R.drawable.ic_launcher),
new RecyclerModal("niraj", "1234554321", R.drawable.ic_launcher),
};
// 2. set layoutManger
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 3. create an adapter
RecyclerAdapter mAdapter = new RecyclerAdapter(itemsData,context);
// 4. set adapter
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
Toast.makeText(context,"You have clicked Call" ,Toast.LENGTH_LONG).show();
break;
case R.id.msg:
Toast.makeText(context,"You have clicked SMS",Toast.LENGTH_LONG).show();
break;
}
return super.onContextItemSelected(item);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="program.androidheight.recyclerviewdemo" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailsActivity"
android:label="@string/title_activity_details" >
</activity>
</application>
</manifest>
OUTPUT
You can download project from Github.
NOTE: Recycler View Widget available in Android L preview SDK.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:recyclerview-v7:+'
}
our project compile with this library i.e. available in dependencies.
RecyclerView Layout is available in main_activity.xml
activity_main.xml
<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" tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#FF9933">
<TextView
android:id="@+id/tool_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RecyclerViewDemo"
android:textColor="#ffffff"
android:layout_marginLeft="30dp"/>
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar"/>
</RelativeLayout>
RecyclerView Implementation
MainActivity.java
package program.androidheight.recyclerviewdemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
Context context;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context= MainActivity.this;
recyclerView = (RecyclerView)findViewById(R.id.recycler_list);
RecyclerModal itemsData[] = {new RecyclerModal("Ramesh", "1234567890", R.drawable.ic_launcher),
new RecyclerModal("suresh", "2222222562", R.drawable.ic_launcher),
new RecyclerModal("prem", "2344322341", R.drawable.ic_launcher),
new RecyclerModal("yogesh", "4567765412", R.drawable.ic_launcher),
new RecyclerModal("niraj", "1234554321", R.drawable.ic_launcher),
};
// 2. set layoutManger
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 3. create an adapter
RecyclerAdapter mAdapter = new RecyclerAdapter(itemsData,context);
// 4. set adapter
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
Toast.makeText(context,"You have clicked Call" ,Toast.LENGTH_LONG).show();
break;
case R.id.msg:
Toast.makeText(context,"You have clicked SMS",Toast.LENGTH_LONG).show();
break;
}
return super.onContextItemSelected(item);
}
}
Most Important thing in RecyclerView is Adapter class
Adapter class is responsible to bind the data model to the UI component that renders the information.This adapter must extend a class called RecyclerView.Adapter.
RecyclerAdapter.java
package program.androidheight.recyclerviewdemo;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by PRABHU on 22-03-2015.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
public RecyclerModal [] modal;
private int position;
static Context context;
ContextMenu.ContextMenuInfo info;
public RecyclerAdapter(RecyclerModal [] modal, Context context){
this.modal = modal;
this.context =context;
}
public RecyclerAdapter(){
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int parent) {
View itemLayoutView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_layout,viewGroup, false);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
//set onClick listener on RecyclerView
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
//bind the UI in the row Item
viewHolder.txtViewName.setText(modal[position].getName());
viewHolder.txtViewNumber.setText(modal[position].getNumber());
viewHolder.imgViewIcon.setImageResource(modal[position].getImageUrl());
}
@Override
public int getItemCount() {
return modal.length;
}
//ViewHolder is used to hold the reference of each Ui of row Item
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnCreateContextMenuListener {
public TextView txtViewName,txtViewNumber;
public ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewName = (TextView) itemLayoutView.findViewById(R.id.name);
txtViewNumber = (TextView) itemLayoutView.findViewById(R.id.Number);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.image);
//set onclickListener
itemLayoutView.setOnClickListener(this);
//set onContextListener
itemLayoutView.setOnCreateContextMenuListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(context,DetailsActivity.class);
intent.putExtra("name",txtViewName.getText().toString());
intent.putExtra("number",txtViewNumber.getText().toString());
context.startActivity(intent);
Toast.makeText(RecyclerAdapter.context,"you have clicked Row "+getPosition(),Toast.LENGTH_LONG).show();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
new RecyclerAdapter().info = menuInfo;
menu.setHeaderTitle("Select The Action");
menu.add(0, R.id.call, 0, "Call");//groupId, itemId, order, title
menu.add(0, R.id.msg, 0, "SMS");
}
}
}
RecyclerModel.java
package program.androidheight.recyclerviewdemo;
/**
* Created by PRABHU on 22-03-2015.
*/
public class RecyclerModal {
private String name;
private String number;
private int imageUrl;
public RecyclerModal(){
}
public RecyclerModal( String name,String number, int imageUrl) {
super();
this.imageUrl = imageUrl;
this.name = name;
this.number=number;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
public int getImageUrl() {
return imageUrl;
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="program.androidheight.recyclerviewdemo" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailsActivity"
android:label="@string/title_activity_details" >
</activity>
</application>
</manifest>
OUTPUT
You can download project from Github.
0 comments:
Post a Comment