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.
Related
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 !!!
I'm using Android Studio, and I have a listView that musts display an important amount of items. showing all of these items have a huge impact on performances. SO I would like to show them 10 by 10, and with a button show the 10 next items. After some researches, I found this android How to limit list items display in ListView and a button show more and this How to limit list items display in ListView by 10 and next 10after clicking next button. But these didn't lead me to a success. The 1st link looks easier but I didn't know where to put the code samples to make it work. Thanks for help !
You dont need to show "Load More" Button always. You can use Android's Recycler View for this. It will load only the data which can be shown on screen. Rest of the data will be loaded as you scroll down. And the view which is scrolled up (Vanishing views) are recycled automatically.
Check this links
https://developer.android.com/training/material/lists-cards.html
https://guides.codepath.com/android/using-the-recyclerview
https://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156
First link question have a simple answer, If you made a custom adapter for populating listview then there will be a getCount() method which will return the number of items you want to show on listview. if you have not getCount() method then simply override it.
Link
there i(variable) have that value which number if items you want to show on listview, on refresh button click increment the value of i(variable) that will again refresh the whole listview and add the total number of items in listview which is the value of i(variable)
I'm building an android app and I'm trying to replace a list view with another list view if you click a button, like the notifications page on Instagram. On that page, if you click on the top "following" button it will show you a listview of what your followers have liked. If you click on the "you" button it will show you a listview of what people have liked your photos.
Any help would be greatly appreciated!
You can do it by following ways,
1. add Two listviews and can change the visibility as per your requirement.
2. On button click you can load the other data into same list view and can update your adapter in the same list view.
in 1 you have to load two list view at first which will consume more time if data is larger sure you can write a login in asynctask to load list views in background thread.
in 2 you have to update your adapter at the button so you will have to provide some progress bar of dialog for user while you list view is getting update.
You can use either of this whichever suites you best.
Simply , You don't need to switch Listview , you only need to switch adapters .
eg, you can switch to mFollowingAdapter when clicked on Following button and switch to mYouAdapter when you select "You" tab. that's it.
You should write a list, that has a custom adapter. This adapter will be able to display BOTH views you want to display.
If the data to be displayed is the same format (ie. both have an imageview next to a textview), you are in good shape.
When you want to switch to a different list, get the information you would like to display, replace the data in your the collection backing your list, then notify the list that the data has changed, and it should redraw.
So, this might look like:
create ArrayList() with data A
setup List, with this data and display
replace the ArrayList() with data B
call listView.notifyDataSetChanged
You can still do this if the Data A, and Data B have different views, in this case, you would need to handle this logic in your custom adapter.
I have a list view fed by BaseAdapter.
When something changes in data, I call BaseAdapter.notifyDataSetChanged. All works fine.
Now I wonder, if I change some tiny detail in some list item, is there some optimized way to update view of single item if it's displayed on screen?
I suppose that notifyDataSetChanged blindly rebuilds views of all visible items in list, which is not optimal if trivial change in item happens.
No, I guess it's not possible unless you implement your own ListView class extension.
Here there is the source code for setAdapter().
You will see, the ListView only registers itself as observer using mAdapter.registerDataSetObserver(mDataSetObserver);
And a DataSetObserver provides no means to notify about a change at a certain position.
However it might not be necessary to notify about updates of certain items, because as far as I know, a ListView only renders and updates the items currently seen on the screen so, no optimization should be necessary here.
Yes, there is way. Assuming I can identify list item, for example by assigning View.setTag to it to some value, I can iterate list items and rebind only one list item if desired, or even update only some sub-view of the item.
It's simple and relatively cheap (linear search):
for(int i = list.getChildCount(); --i>=0; ){
View v = list.getChildAt(i);
Object id = v.getTag();
if(id==myId){
updateListItem(id, v);
break;
}
}
where myId is some ID of item I want to update, and updateListItem makes desired changes on the list item. Proved, and working fine and very efficiently.
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.