Android SDK doesn’t includes the RecyclerView class, hence for using RecyclerView in your project you first need to add the RecyclerView support library to your project. To do so in Android Studio, go to build.gradle and add the following dependencies:
RecyclerView in Andriod
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:+'
}
In this example we need to create:
Activity
AllUsers.java: contains java file and xml layout file
AllUsersActivity.java
activity_all_users.xml
Adapter
AllUsersAdapter.java
Create a constructor to receive List of Users passed by the Activity Class
Create a class UserViewHolder.java that will extends RecyclerView.ViewHolder
Implement RecyclerView.Adapter and override three methods:
onCreateViewHolder(…)
onBindViewHolder (…)
getItemCount(…)
Layout
single_user_view.xml
for single User View inside RecyclerView
activity_all_users.xml
single_user_view.xml
AllUsersAdapter.java
AllUsers.java
Preview Screen
RecyclerView in Andriod
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:+'
}
In this example we need to create:
Activity
AllUsers.java: contains java file and xml layout file
AllUsersActivity.java
activity_all_users.xml
Adapter
AllUsersAdapter.java
Create a constructor to receive List of Users passed by the Activity Class
Create a class UserViewHolder.java that will extends RecyclerView.ViewHolder
Implement RecyclerView.Adapter and override three methods:
onCreateViewHolder(…)
onBindViewHolder (…)
getItemCount(…)
Layout
single_user_view.xml
for single User View inside RecyclerView
Source Code:
activity_all_users.xml
<?xml version="1.0" encoding="utf-8"?> <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.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rvAllUsers" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
single_user_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp"> <ImageView android:id="@+id/ivProfile" android:layout_width="70dp" android:layout_height="70dp" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SoneValley" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/tvMobile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8899772233" /> <TextView android:id="@+id/tvEmail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sonevalleyblog@gmail.com" /> <TextView android:id="@+id/tvCreatedDate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2015-07-02 20:33:12" /> </LinearLayout> </LinearLayout>
AllUsersAdapter.java
package com.sonevalley.tech.recyclerView; 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.ImageView; import android.widget.TextView; import com.sonevalley.tech.recyclerView.AllUsers.User; import java.util.ArrayList; public class AllUsersAdapter extends RecyclerView.Adapter { private Context mContext; ArrayList<User> userList; public AllUsersAdapter(Context mContext, ArrayList<User> userList) { this.mContext = mContext; this.userList = userList; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(mContext).inflate(R.layout.single_user_view, null); UserViewHolder viewHolder = new UserViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) { UserViewHolder holder = (UserViewHolder) viewHolder; holder.ivProfile.setImageResource(userList.get(i).imageResourceId); holder.tvName.setText(userList.get(i).userName); holder.tvMobile.setText(userList.get(i).userMobile); holder.tvEmail.setText(userList.get(i).userEmail); holder.tvCreatedDate.setText(userList.get(i).userCreatedDate); } @Override public int getItemCount() { return userList.size(); } public static class UserViewHolder extends RecyclerView.ViewHolder { private ImageView ivProfile; private TextView tvName; private TextView tvMobile; private TextView tvEmail; private TextView tvCreatedDate; public UserViewHolder(View itemView) { super(itemView); ivProfile = (ImageView) itemView.findViewById(R.id.ivProfile); tvName = (TextView) itemView.findViewById(R.id.tvName); tvMobile = (TextView) itemView.findViewById(R.id.tvMobile); tvEmail = (TextView) itemView.findViewById(R.id.tvEmail); tvCreatedDate = (TextView) itemView.findViewById(R.id.tvCreatedDate); } } }
AllUsers.java
package com.sonevalley.tech.recyclerView; 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 java.util.ArrayList; public class AllUsers extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_all_users); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvAllUsers); recyclerView.setLayoutManager(new LinearLayoutManager(this)); ArrayList<User> UserList = new ArrayList<>(); UserList.add(new User(R.mipmap.ic_launcher, "Amit", "9988776655", "amit@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "John", "9988776655", "john@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "rakesh", "9988776655", "rakesh@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "akash", "9988776655", "akash@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "Tanu", "9988776655", "tanu@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "Manu", "9988776655", "manu@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "Prakash", "9988776655", "prak@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "Jyoti", "9988776655", "jyoti@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "Puja", "9988776655", "puja@sonevalley.com", "2015-07-02 20:33:12")); UserList.add(new User(R.mipmap.ic_launcher, "Aarti", "9988776655", "aarti@sonevalley.com", "2015-07-02 20:33:12")); AllUsersAdapter allUserAdapter = new AllUsersAdapter(this, UserList); recyclerView.setAdapter(allUserAdapter); recyclerView.setItemAnimator(new DefaultItemAnimator()); } public class User { int imageResourceId; String userName; String userMobile; String userEmail; String userCreatedDate; public User(int imageResourceId, String userName, String userMobile, String userEmail, String userCreatedDate) { this.imageResourceId = imageResourceId; this.userName = userName; this.userMobile = userMobile; this.userEmail = userEmail; this.userCreatedDate = userCreatedDate; } } }
Preview Screen
Android RecyclerView Example |
i did almost same as yours, but its not working..it gets crashed
ReplyDelete