I want to create an infinite list , when i scroll to the end of list , my list will show first item, second item ... at the end() . Such as :
item[0]
........
item[last]
item[0]
item[1]
The first thing comes to my mind is that I will double the list so it can show like I wished.
Unfortunately , it's a bad idea because size of my list will grow more and more , and loading a big list is not good for performance(obviously).
May you guys show me other solutions to handle this case ? Thank you very much .
Using RecyclerView, you can achieve what you want in few steps. The idea is to set adapter's item count to infinity, and place your first item to the middle of infinity.
In your Adapter override getItemCount() method like this.
#Override
public int getItemCount() {
return Integer.MAX_VALUE;// Not the infinity, but I'm not sure someone will scroll about 2 million items and find out that's a cheat
}
Then in the class where you use the RecyclerView, get LayoutManager and set starting position to the middle of adapter itemCount.
LinearLayoutManager layoutManager = getLayoutManager();
int pos = (Integer.MAX_VALUE / 2); //Voila, you are in the middle of infinity
layoutManager.scrollToPosition(pos);
yourAdapter.setLayoutManager(layoutManager);
By doing this you are able to scroll your list in any direction up to about 2 000 000 scrolls, without container size increasing and any performance issues.
You need to update your list when user reach bottom of the page.
You can keep track end of the page in on-scroll Listener.
Once user reach bottom of the or about to reach bottom of the page just double your list or extend it as per your requirement.
Extend or double your list is required because we need to store or display the data of each item in each row.
And Same thing happen in pagination concept.
Clearly understood we are not extending list unnecessary, we are extending when we really need.
You can visit this link for Endless Scrolling with AdapterViews and RecyclerView.
use onScrollListener() and when got to the end , use LIST.size()+VisibleItems (items from end of list that are visible) and put them first on array and renew your adapter data
sorry for very briefing Explanation !!!
Related
I want to implement a RecyclerView that never finishes. Before the first item there is the last item and after the last item is scrolled, the first item is shown.
How can I do that?
What Im trying to achieve is a music player that shows the current song, the song before and after.
Like this:
(ps. is there a better way than using a recyclerview?)
The way I have seen this done is to define the number of items that you have to display as Integer.MAX_VALUE. In onBindViewHolder() of the adapter you will have something like:
#Override
public int getItemCount() {
return (mItems == null) ? 0 : Integer.MAX_VALUE;
}
Now you can't really have Integer.MAX_VALUE items to display, so this is a small lie to the RecyclerView. In your code, map the index value from 0...Integer.MAX_VALUE-1 to 0...your_item_count-1 like this in onBindViewHolder():
int realPos = position % mItems.size();
Use realPos instead of the position that the RecyclerView passes in.
All that is left is to scroll the RecyclerView to the middle of the range:
mRecycler.scrollToPosition(Integer.MAX_VALUE / 2);
This works OK, but is it perfect? No, but close enough; I don't think anyone will scroll enough to see the flaw. (It will stop wrapping at the extremes.)
I want to achieve the following layout -
In this layout, I want to show all the available items to the user. Pressing right arrow will show the next 6 available items. This should happen via horizontal scrolling type motion.
Since items can be many, I want to use GridView for this because of its recycling optimisation. Optimisation is necessary because every item is a Linear Layout in itself.
Now, I can't use GridView because I want to show next 6 items using the arrows only. Also, it will be good if this happens in smooth scrolling type motion.
Can I make any tweak to Gridview so that I can use arrows to scroll GridView to show next 6 items or Is there any other ViewGroup available to achieve this along with the recycling optimisation.
This example is for listview you can refer for gridview also.
For a SmoothScroll with Scroll duration:
getListView().smoothScrollToPositionFromTop(position,offset,duration);
Parameters
position -> Position to scroll to
offset ---->Desired distance in pixels of position from the top of the view when scrolling is finished
duration-> Number of milliseconds to use for the scroll
Note: From API 11.
listview is quite lengthy and also with alphabet scroller. Then I found that the same function can take other parameters as well :)
To position the current selection:
int h1 = mListView.getHeight();
int h2 = v.getHeight();
mListView.smoothScrollToPositionFromTop(position, h1/2 - h2/2, duration);
Or
You can use RecyclerView :
You have to make use of LayoutManager for that. Follow the below steps.
1). First of all, declare LayoutManager in your Activity/Fragment. For example, I have taken LinearLayoutManager
private LinearLayoutManager mLinearLayoutManager;
2). Initialise the LinearLayoutManager and set that to your RecyclerView
mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);
3). On your Button onClick, do this to scroll to the bottom of your RecyclerView.
mLinearLayoutManager.scrollToPosition(yourList.size() - 1); // yourList is the ArrayList that you are passing to your RecyclerView Adapter.
Hope this will help..!!
You could try giving corresponding list input to adapter at each click. And then call the notifydatasetchanged to reflect the changes on the View
If the recyclerview has a list of items and I want to go to a specific row immediately when user enters the UI, so I don't want the user to see it scroll to reach that row. Is it achievable?
Thanks.
You can use method scrollToPositionWithOffset (int position, int offset)
Scroll to the specified adapter position with the given offset from
resolved layout start.
See documentation
Example:
//Scroll to item position 2 with offset 0
RECYCLERVIEW_LAYOUT_MANAGER.scrollToPositionWithOffset(2, 0);
Hope this will help~
I would say it is impossible.
RecyclerView#scrollToPosition causes sort of animation because it does not know the height of all rows initially. Not all items are loaded immediatly but one by one as you scroll down the list.
I use ListView and I solved the issue by calling setSelection(position)
I want to implement a List View in Fragment. Size of the list can be more than 1000 which includes images in left and name and type will be right. I am able to implement CustomArrayAdapter and List View but i don't know how to load only specific number of items or only those which are visible. I want that if user scroll down then only hit the server and load the images and data.
How can i implement this kind of List View in Fragment?
I don't know much about andorid. Please help me out to solve this problem.
Thank in advance
The listview class has a couple of useful methods to deal with that:
int lastItem = listView.getLastVisiblePosition();
int firstItem = listView.getFirstVisiblePosition();
What you could do is load an appropriate batch of data from the server (say 100 items), then override the method onScoll() of the listview to keep track on what are those values over time, when the user arrives to the end of the batch (say, the position 100), you can load the next batch and notify the adapter that the list has changed.
I want to set a simple listener in a ListView so when the user passes through the 50º item of the list, I call a thread that will append 100 more items into that list.
I am looking for an event to do that for over an hour now and I don't seem to be able to find one. There are tons of events for when a list item will be clicked or something like that, but I can't find one for when the user simples passes through the item in the scroll processing.
Thank you so much in advance!
Put the ScrollListener on list view, when it reaches end of the list u will get the last item, then add the progressbar at the footer and then start thread where u can load the other items, and notifyDataSetChanged() for adapter , and you will get the list with added items..!!
And you can check whether the item number 50 is reached, in onScroll().
I think there is no need to do this, because the ListView recycles the item views. So you dont occupy memory for the listview items (per each value), you just use memory for the ArrayList(or any data type you use for feeding).
Also you can do this whenever user reaches the end of the listview (last listview item) then refeed the adapter with the new value and then call .notifyDataSetChanged().
About reaching 50% of listview visible items can make things scrambled because you'll always have the 50% reached, otherwise you can improvise (reaching 50% on the ArrayList instead ListView) by checking if more than 50% of the existing ArrayList is displayed, let's say you 10 items and the 6th item is on the listview count / 2, you check if the on that half listview item is more than 50% of data feed for the listview you feed with new data. (I might be unclear)
I hope the idea will help you.