LOAD MORE ITEM ON SCROLL
Objective
The main objective of this post is to help you understand how to load more item on scroll using endless recyclerview in your android Application.You will get Final Output:
- In This activity you will Crate XML file.
- First implement below Card view library in app:Build.gradle
compile 'com.android.support:recyclerview-v7:26.+'
compile 'com.android.support:cardview-v7:26.+'
- activity_main.xml
<RelativeLayout 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="wrap_content"
android:orientation="vertical"
tools:context="com.example.abc.endlessrecyclerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<ProgressBar
android:id="@+id/progBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:visibility="gone" />
</RelativeLayout>
- list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:card_view="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" app:cardElevation="10dp" app:cardPreventCornerOverlap="true" card_view:contentPadding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_Rollno" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="@color/colorPrimaryDark" android:textSize="30dp" android:textStyle="bold" /> <TextView android:id="@+id/tv_Name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:textColor="@color/DarkRed" android:textSize="25dp" android:textStyle="bold" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout>
- The Above Code was use to create XML file..
- After Create main Java class and POJO class
- MainActivity.java
- The Above mainactivity class i have a used the ArrayList.
- I have used onScrollStateChanged and onScrolled method.
- I have used the Adapter class....
- public class MainActivity extends AppCompatActivity {
private ArrayList<StudentInfo> studentInfoArrayList;
private Adapter adapter;
ProgressBar progressBar;
RecyclerView recyclerView;
LinearLayoutManager linearLayoutManager;
private boolean userScrolled = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
progressBar = (ProgressBar) findViewById(R.id.progBar);
linearLayoutManager = new LinearLayoutManager(MainActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
studentInfoArrayList = new ArrayList<StudentInfo>();
StudentInfo studentInfo = new StudentInfo();
studentInfo.setName("Mahesh");
studentInfo.setRollNo("Rollno = 1");
studentInfoArrayList.add(studentInfo);
studentInfo = new StudentInfo();
studentInfo.setName("Axay");
studentInfo.setRollNo("Rollno = 2");
studentInfoArrayList.add(studentInfo);
studentInfo = new StudentInfo();
studentInfo.setName("Vijay");
studentInfo.setRollNo("Rollno = 3");
studentInfoArrayList.add(studentInfo);
studentInfo = new StudentInfo();
studentInfo.setName("Druv");
studentInfo.setRollNo("Rollno = 4");
studentInfoArrayList.add(studentInfo);
studentInfo = new StudentInfo();
studentInfo.setName("Prakash ");
studentInfo.setRollNo("Rollno = 5");
studentInfoArrayList.add(studentInfo);
studentInfo = new StudentInfo();
studentInfo.setName("Vishal");
studentInfo.setRollNo("Rollno = 6");
studentInfoArrayList.add(studentInfo);
adapter = new Adapter(studentInfoArrayList, getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
implementScrollListener();
}
private void implementScrollListener() {
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView,
int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
userScrolled = true;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx,
int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = linearLayoutManager.getChildCount();
totalItemCount = linearLayoutManager.getItemCount();
pastVisiblesItems = linearLayoutManager
.findFirstVisibleItemPosition();
if (userScrolled
&& (visibleItemCount + pastVisiblesItems) == totalItemCount) {
userScrolled = false;
addDataToList();
}
}
});
}
private void addDataToList() {
progressBar.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 5; i++) {
studentInfoArrayList.add(studentInfoArrayList.get(i));
}
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Data Updated.",
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}, 4000);
}
}
- Adapter.java
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
ArrayList<StudentInfo> mStudentInfoArrayList;
Context mContext;
public Adapter(ArrayList<StudentInfo> studentInfoArrayList, Context applicationContext) {
mStudentInfoArrayList = studentInfoArrayList;
mContext = applicationContext;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final StudentInfo studentInfo = mStudentInfoArrayList.get(position);
holder.tv_Name.setText(studentInfo.getName());
holder.tv_RollNo.setText(studentInfo.getRollNo());
}
@Override
public int getItemCount() {
return mStudentInfoArrayList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_Name,tv_RollNo;
public MyViewHolder(View itemView) {
super(itemView);
tv_Name = (TextView) itemView.findViewById(R.id.tv_Name);
tv_RollNo = (TextView) itemView.findViewById(R.id.tv_Rollno);
}
}
}
- StudentInfo.java
class StudentInfo { String Name; String RollNo; public StudentInfo() { } public String getName() { return Name; } public void setName(String name) { this.Name = name; } public String getRollNo() { return RollNo; } public void setRollNo(String rollNo) { RollNo = rollNo; } }