Hello Friends, today we are going to see and example of recyclerview load on scroll in Android. Basically we are going to implement infinite scroll in recyclerview here and the basic function of this code is to load more data on scroll when we reaches to the end while scrolling the recyclerview. In this example we are going to generate data using for loop but we can also load data through webservice by just modifying loadMoreData method accordingly.
So, Here is the code
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
package com.zatackcoder.recyclerviewloadonscroll; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.ProgressBar; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private ArrayList items = new ArrayList<String>(); private ProgressBar mProgressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initControls(); loadData(); } private void initControls() { mRecyclerView = findViewById(R.id.recycler_view); mProgressBar = findViewById(R.id.progress_bar); mRecyclerView.setHasFixedSize(true); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager(); if (!mProgressBar.isShown()) { if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == items.size() - 1) { mProgressBar.setVisibility(View.VISIBLE); loadMoreData(); } } } }); mAdapter = new MyAdapter(getApplicationContext(), items); mRecyclerView.setAdapter(mAdapter); } private void loadData() { for(int i = 1; i <= 15; i++) { items.add("Item " + i); } mAdapter.notifyDataSetChanged(); } private void loadMoreData() { int count = items.size(); for(int i = 1; i < 15; i++) { items.add("Item " + (count + i)); } mAdapter.notifyDataSetChanged(); new Handler().postDelayed(new Runnable() { @Override public void run() { mProgressBar.setVisibility(View.GONE); } }, 1000); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintVertical_weight="1" tools:layout_editor_absoluteX="1dp" tools:layout_editor_absoluteY="1dp" /> <ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#fff" android:visibility="gone" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
MyAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.zatackcoder.recyclerviewloadonscroll; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { private Context context; private ArrayList<String> items; public MyAdapter(Context context, ArrayList<String> items) { this.context = context; this.items = items; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false); MyViewHolder mvh = new MyViewHolder(view); return mvh; } @Override public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int position) { myViewHolder.container.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String message = "You clicked on list_item which has index " + position + " and title " + items.get(position); Toast.makeText(context, message, Toast.LENGTH_LONG).show(); } }); myViewHolder.title.setText(items.get(position)); } @Override public int getItemCount() { return items.size(); } public static class MyViewHolder extends RecyclerView.ViewHolder { // each data list_item is just a string in this case View container; TextView title; public MyViewHolder(View view) { super(view); container = view; title = view.findViewById(R.id.title); } } } |
list_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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:layout_margin="5dp" android:background="#fff" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:padding="5dp" android:text="Title" android:textSize="25dp" /> </LinearLayout> |
Video
AndroidStudio Project Download
RecyclerViewLoadOnScroll AndroidStudio Project
1 file(s) 10.38 MB
Thanks for Stoping by
If you find this helpful then please do share
Comments