Automatically load images of next page on recyclerview scrolling - android

I am using pixabay's api to show images in my recyclerview in grid layout.
Everything is working fine,i want to show images of next page in the same activity when user reaches end of images in recyclerview of first page i.e. when user reaches end of recyclerview make another request and increase page by 1 and show images of next page at the same time i want to keep images of first page as it is,if the user scrolls up again it should see images of first page.Here is my request.
https://pixabay.com/api/?q=yellow&key=MY_API_KEY&image_type=photo&page=1
How to automatically increase page no when user reaches end of recyclerview. I don't want to show buttons at the end of recyclerview for next page images

I think you'll need to implement addOnSrollListener() in your recyclerview.
You should implement something like that:
listRepositories.addOnScrollListener(new
RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int pastVisibleItems, visibleItemsCount, totalItemCount;
if (dy > 0) {
visibleItemsCount = layoutManager.getChildCount();
pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
totalItemCount = layoutManager.getItemCount();
if ((visibleItemsCount + pastVisibleItems) >= totalItemCount) {
// method where you get your images
load(currentPage++);
}
}
}
});

Related

Android horizontal RecycleView with never-ending scroll animation

I'm trying to implement an horizontal RecycleView in my Android app that displays content with never-ending scroll animation. See example of required UI here.
My question is, what would be the best practice on how to achieve this goal? (RecycleView should support scroll gestures from the user).
If I'm correct you're looking for the endless scrolling or infinite scrolling (Not Official names for that)
If it is correct then it shows like say you have 5 images in your recyclerview now when the user reaches the end item then the recycler view starts again from the first item like
item1, item2, item3, item4, item5 and again item5, item1, item2.....
To achieve this
Go to your adapter class and do the following code.
#Override
public int getItemCount() {
return items == null ? 0 : items.size() * 2; //Here instead of items.size(); you have to change like this
}
Now in your OnBindViewHolder method
MODEL_CLASS item = items.get(position%items.size());
Now head over to the activity where you set the adapter to recyclerview and add the following code
YOUR_ADAPTER_NAME.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstItemVisible = linearLayoutManager.findFirstVisibleItemPosition();
if (firstItemVisible != 0 && firstItemVisible % YOUR_LIST_ITEM.size() == 0) {
recyclerView.getLayoutManager().scrollToPosition(0);
}
}
});
By this you can see the items when scrolled to right it continues loops in your list.

Very large data makes my application very slow while using ScrollView

I am writing an android application which the whole page is scrollable.
So I've tried to use NestedScrollView but the views of recycle view are created too slow since the data is very large (e.g. more than 1000 records and can be up to 10000 records) then It make my application crashed.
So I am using the code below to fix above problem.
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!recyclerView.canScrollVertically(1))
onScrolledToBottom();
}
});
It can help to reduce the time but the problem changes to be the screen cannot scroll for the whole page.
How can deal with this issue?
ID: < input box >
item1
item2
item3
item4
item5
item6
...
item1000
Pagination is build specifically for this.
You can have a repository which would serve you data .
class YourRepository{
/**
* Offset would indicate the starting index of data that you need
* Count indicates the amount of data you need
**/
List<Data> getData(int count, int offset){}
}
With this api in place, you can use Jetpack Paging library pretty effectively.
https://developer.android.com/topic/libraries/architecture/paging

Implement Load More in RecyclerView with PairList using MVP pattern

I'm looking for best practices. I want to implement load more [progressbar] after every 10 items (Using RecyclerView).
In past, I did this by simply creating Loader class item and just added into the specific position in my List. Well, this time, I'm using PairList and it's impossible to add Loader class item (even tho I personally think, that's a pretty clean way to do it).
I found a few solutions on SO, which are almost the same: How to implement load more recyclerview in android
Is there any other way I could implement this load more progressbar after every 10th element (First load 10 items from API, then, when user reaches 10th item, we show progress bar [meanwhile we fetch the next 10 items], remove progress bar, add 10 items and so on).
Use this InfiniteScrollListener in your recycler scrollListener:
public abstract class InfiniteScrollListener extends RecyclerView.OnScrollListener {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager)recyclerView.getLayoutManager();
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
int pastVisiblesItems = linearLayoutManager.findFirstVisibleItemPosition();
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
loadMore();
}
}
}
protected abstract void loadMore();}
recyclerView Listener:
recyclerView.addOnScrollListener(new InfiniteScrollListener() {
#Override
protected void loadMore() {
boolean willLoad = //get your last api call data. if empty set false
int offset = recyclerView.getAdapter().getItemCount();
if(willLoad){
willLoad=false;
onCallApi(offset,10);
}
}
});
your api:
private void onCallApi(int offset,int limit){
//your api
}

ListView scroll up for refresh, scroll down to add new items to the list

Right now I have a Listview that stores 25 items, which are retrieved from an API. What I want is that the ListView refreshes on scroll up (retreive data from api call again). And add another 25 items on scroll down.
What could I use for this?
I found SwipeRefreshLayout, However, I can't find a way to distinguish scroll up and scroll down.
Use RecyclerView instead of ListVIew. Initialize the RecyclerView as follows :
RecyclerView Initialization :
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
videosAdapter = new VideosAdapter();
recyclerView.setItemAnimator(new SlideInUpAnimator());
recyclerView.setAdapter(videosAdapter);
// Pagination
recyclerView.addOnScrollListener(recyclerViewOnScrollListener);
Now setup OnScrollListener :
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new
RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading && !isLastPage) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= PAGE_SIZE) {
loadMoreItems();
}
}
}
};
Once you get the response you need to add the received data to the list again.
I think your question is little bit misleading.If i am not wrong, here can be to thing.
Reload List on Over Scroll Down :- This functionality can be achieve by using SwipeRefreshLayout. Follow the android tutorial for Adding Swipe-to-Refresh To Your App.
Load more items on Over Scroll Up:- This is the part of Lay loading(Pagination) . If you are using ListViewthen you can use OnScrollListener and setFooterView() to make it done . Have a look at This thread.
Suggestion:- A simple suggestion Use RecyclerView instead of ListView to make better use of ViewHolder pattern.

Endless RecyclerView data loading logic is not consistent on all devices

I'm loading data from SQLite database in RecyclerView in batch of 25 records per load cycle. I figure out every thing but having problem with logic of calling data loading method in RecyclerView scroll listener.
Problem is list is loading nicely on scroll of recyclerView in emulator but when i tested in physical device, on Lenovo Phab 2 it worked fine but in Mi note 3, list doesn't load consistenly (sometime it loads, sometime not) more records on reaching bottom of scroll.
Below is the code I'm using in RecyclerView scrolle listener
private val RecyclerView_ScrollListener = object : RecyclerView.OnScrollListener(){
override fun onScrolled(recyclerView1: RecyclerView?, dx: Int, dy: Int) {
super.onScrolled(recyclerView1, dx, dy)
if (dy > 0) {
if (limit < rowCount_fromDatabase) {
visibleItemCount = layoutManager_MemberList.childCount
totalItemCount = layoutManager_MemberList.itemCount
firstVisibleItemPosition = layoutManager_MemberList.findFirstVisibleItemPosition()
if (visibleItemCount + firstVisibleItemPosition >= totalItemCount) {
limit += 25
load_Data()
}
}
}
}
I had the same problem. Try this
if (limit < returnedRowCount_fromDatabase) {
if((dy > 0) && (linearLayoutManager.findLastCompletelyVisibleItemPosition() == rowList.size() - 1)){
limit += 25;
load_Data();
}
}
I have written a class, that works with all types of LayoutManagers. It was tested on emulators and real devices.
It has blockers for preventing double loading of next page. When Listener.onListEndReached() returns true, it won't be called again, untill RecyclerViewOnScrollUpdater.loading will be set to false.
Also you can set visibleThreshold and enable/disable page loading via setting enabled.
What i use to scroll more is :
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!recyclerView.canScrollVertically(1)){
//Load more data here. It will only trigger when you reach the end of your list and cannot scroll further
}
}
});
Hope this helps.

Categories

Resources