I am using RecyclerView inside fragment in my activity, the ConstraintLayout below is where the fragment is called, and when i open the app it calls all the data for the RecyclerView which is not open by user.
So i want to call data when user is scrolling or user is viewing the items of RecyclerView
So is there any way that only first few or 5 post are called and rest are called while scrolling.
Activity XML-
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_feed"
android:text="Feed"
android:layout_marginLeft="20dp"
android:layout_below="@+id/rcv_news"
android:layout_marginBottom="10dp"
android:textColor="#000000"
android:paddingLeft="10dp"
android:textSize="30dp"
android:fontFamily="@font/benchnine_bold" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tv_feed">
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
Fragment XML-
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_bg"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/post_ImagesRecyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:background="@color/grey_bg"
android:nestedScrollingEnabled="false"/>
<com.wang.avi.AVLoadingIndicatorView.......
Java -
postAdapter = new PostAdapter(context, false, false, postList, this, 1, loadType);
LinearLayoutManager postListManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
post.setLayoutManager(postListManager);
post.setAdapter(postAdapter);
postPayload = new PostPayload(CommonData.getCampusId(), start, LIMIT, loadType, CommonData.getCreatorInfo());
postPayload.setPostId(postId);
if (postPayload.getLoadType() == Constants.LOAD_MY_POSTS)
postPayload.setProfileCreatedBy(CommonData.getLoggedInUserId());
postPayload.setSearchKey(searchKey);
getPosts();
}
@Override
public void getMorePost() {
start = postList.size();
postPayload.setStart(start);
getPosts();
}
First of all, even though all data's called, recyclerview not create all view. It create views only when it is needed. So you dont need to worry about lazyloading.
But if you want to load your data with scroll. You can add a scroll change listener to your recyclerview in your fragment java class.I think it will work. Hope it helps
//load your desired numbers data to postList your (5 posts for this),
boolean isAlreadyLoading = false
post.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (postListManager != null) {
index = postManager.findFirstVisibleItemPosition();
if(index > postList.size()-5 && !isAlreadyLoading)
{
isAlreadyLoading=true;
postAdapter.notifyDataSetChanged;
}}}
loadNewData()
{
loadNewData(); //add new data to the end of the postList
isAlreadyLoading=false;
}