Implementaion of Fast Scroll RecyclerView will be quite similar to the Simple RecyclerView or CardView with RecyclerView. You can implement Fast Scroll RecyclerView for CardView also.
if you want Fast Scroll RecyclerView for CardView then you need to add dependency also for CardView.
In this example we need to create:
single_user_view.xml
FastScrollAdapter.java
AllUsersActivity.java
Dependencies
To use CardView and RecyclerView in Android you need to add following dependencies:dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.simplecityapps:recyclerview-fastscroll:1.0.5' }
if you want Fast Scroll RecyclerView for CardView then you need to add dependency also for CardView.
In this example we need to create:
Fast Scroll RecyclerView in Android |
Activity
AllUsers.java: contains java file and xml layout file
AllUsersActivity.java
activity_all_users.xml
AllUsersActivity.java
activity_all_users.xml
Adapter
FastScrollAdapter.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 FastScrollRecyclerView.SectionedAdapter and override three methods:
onCreateViewHolder(…)
onBindViewHolder (…)
getItemCount(…)
Create a constructor to receive List of Users passed by the Activity Class
Create a class UserViewHolder.java that will extends RecyclerView.ViewHolder
Implement FastScrollRecyclerView.SectionedAdapter and override three methods:
onCreateViewHolder(…)
onBindViewHolder (…)
getItemCount(…)
getSectionName(...)
Layout
single_cardview_layout.xml
for single User CardView inside RecyclerView
for single User CardView inside RecyclerView
Source Code:
activity_all_users.xml<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".activity.AllUsersActivity"> <com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" app:fastScrollThumbColor="@color/colorAccent" app:fastScrollPopupBgColor="@color/colorAccent" app:fastScrollPopupTextColor="@android:color/primary_text_dark"/> </FrameLayout>
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:layout_gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Sushil" 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="sushil@gmail.com" /> </LinearLayout> </LinearLayout>
FastScrollAdapter.java
package com.sushil.tech.recyclerviewfastscroll; import android.content.Context; import android.support.annotation.NonNull; 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.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView; import java.util.ArrayList; public class FastScrollAdapter extends RecyclerView.Adapter<FastScrollAdapter.UserViewHolder> implements FastScrollRecyclerView.SectionedAdapter { private Context mContext; ArrayList<AllUsersActivity.User> userList; public FastScrollAdapter(Context mContext, ArrayList<AllUsersActivity.User> userList) { this.mContext = mContext; this.userList = userList; } @Override public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.item, null); UserViewHolder viewHolder = new UserViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(UserViewHolder holder, int position) { AllUsersActivity.User user = userList.get(position); holder.tvName.setText(user.userName); holder.tvEmail.setText(user.userMobile); holder.tvMobile.setText(user.userEmail); } @Override public int getItemCount() { return userList.size(); } @NonNull @Override public String getSectionName(int position) { return String.valueOf(userList.get(position).userName.charAt(0)); } class UserViewHolder extends RecyclerView.ViewHolder { ImageView ivProfile; TextView tvName; TextView tvMobile; TextView tvEmail; 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); } } }
AllUsersActivity.java
package com.sushil.tech.recyclerviewfastscroll; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView; import java.util.ArrayList; public class AllUsersActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_all_users); ArrayList<User> UserList = new ArrayList<>(); UserList.add(new User(R.mipmap.ic_launcher, "Aarti", "9988776655", "aarti@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Akash", "9988776655", "akash@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Ajay", "9988776655", "ajay@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Amit", "9988776655", "amit@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Anand", "9988776655", "anand@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Anju", "9988776655", "anju@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "John", "9988776655", "john@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Jyoti", "9988776655", "jyoti@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Rakesh", "9988776655", "rakesh@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Manu", "9988776655", "manu@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Prakash", "9988776655", "prak@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Puja", "9988776655", "puja@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Suman", "9988776655", "suman@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Tanu", "9988776655", "tanu@sonevalley.com")); UserList.add(new User(R.mipmap.ic_launcher, "Vijay", "9988776655", "Vijay@sonevalley.com")); FastScrollRecyclerView recyclerView = (FastScrollRecyclerView) findViewById(R.id.recycler); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(new FastScrollAdapter(this, UserList)); } public class User { int imageResourceId; String userName; String userMobile; String userEmail; public User(int imageResourceId, String userName, String userMobile, String userEmail) { this.imageResourceId = imageResourceId; this.userName = userName; this.userMobile = userMobile; this.userEmail = userEmail; } } }
Screen Preview
Fast Scroll RecyclerView in Android |
0 comments:
Post a Comment