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
Related
I am currently experiencing an issue with scrolling my news ticker, similar to those found on news channels. To resolve this issue, I am using the following function to move the news ticker:
rv.smoothScrollBy(0, 0, new LinearInterpolator(), 10000);
-- The first parameter: I am currently unable to determine the correct value for this parameter.
-- The second parameter: As my RecyclerView orientation is horizontal (LinearLayoutManager), I am keeping it set to 0.
-- The third parameter: I do not require any additional effects, so I am using the LinearInterpolator.
-- The fourth parameter: I wish to move from the first scroll position to the last scroll position within the RecyclerView in 10 seconds, regardless of the text length.
Here is what I did to fetch dx
rv1.setAdapter(new CustomAdapter(MainActivity.this, strings));
rv1.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (!recyclerView.canScrollHorizontally(1))
Log.w("ABC", "dx is " + dx);
}
});
rv1.smoothScrollBy(100000000, 0);
Is there a way to determine the value for the first parameter without resorting to the workaround I have currently implemented? Specifically, can I fetch the value directly without having to move the scroll to the last position by adding a large number and then determine the dx value?
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.
How do I get the position of a list item that's on focus after scrolling the RecyclerView? Will this work?
recyclerview1.addOnScrollListener(new RecyclerView.OnScrollListener())
If yes, then how?
Yes there are two ways of doing this.
One using onBindViewHolder , in onBindViewHolder add
Log.d("onBindViewHolder", "position =>"+position);
count++; //for the position
but remember in this way count call several time for each news and you must count only in first call.
Other way around is using layoutManagers i.e LinearLayoutManager or GridLayoutManager by doing this
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
int lastVisiblePosition = layoutManager.findLastVisibleItemPosition();
OR for scrollListener you can do this
recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//you can use other methods as per your requirements
Toast.makeText(mContext, "Scrolled to"+layoutManager.findLastCompletelyVisibleItemPosition(), Toast.LENGTH_SHORT).show();
})
Hope this helps you.
I just faced this problem, and i got I put the recyclerView in scrollView So please never put the recyclerview inside the scrollView that may also cause mLayoutManager.findFirstVisibleItemPosition() that always return a fixed value. and also check recyclerview.setNestedScrollingEnabled(false);
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++);
}
}
}
});
I create parallax effect moving the image background depends on location of first element of recycler view only.
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if ((holder = recyclerView.findViewHolderForAdapterPosition(0)) != null) {
int offset = recyclerView.findViewHolderForAdapterPosition(0).itemView.getTop() / 10;
backgroundPhoto.setTop(offset);
}
The problem is : when the first item of recycler(header) scroll off the screen, background picture somehow jump ot initial position.
Once the view is scrolled off the screen the RecyclerView might still be able to access it but it's getTop() value will have some random value or 0 which will cause your parallax effect to jump.
You could keep a field in your class which saves the currently "scrolled distance" and add dx to it in the onScrolled(...) callback and use this value to calculate the parallax offset.