I know that ListView creates only the needed view items and others are dynamically created as we scroll and the functions handling this are newView and bindView.but I can't find the function that disposes the items that go out of the view.
I use a custom CursorAdapter for my ListView.
I need that to create an ArrayList to keep the items that are currently on the view. How can I do this or does the ListView have such functionality?
If you want to know which items are currently visible in your ListView you can set an onScrollListener to the ListView.
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
In onScroll you can get the first and last visible position (firstVisibleItem + visibleItemCount) and thus get the items from your underlying ArrayList that are currently visible.
Related
I am using on scroll listener on list view and I am loading data at the end of list,it is getting loaded but after every scroll when request goes it takes me to first page and i want list should be stable when i scroll and doesn't move to first and loads data.
searchListview.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if ((firstVisibleItem + visibleItemCount) >= totalItemCount ) {
// I am loading data here
}
}
});
This is occurring because you are setting new yourAdapter on listview everytime your request is called. You need to check do that if and only if your adapter is null. If its not null then you can call yourAdapter.notifyDataSetChanged();
Note: Do not clear your data on subsequent calls.
I have a custom cursor adapter to populate a list view in my fragment.
I want to set the visibility of certain elements within the list view items, depending on whether the item is the first visible one in the list or not. Is it possible to get that info in the bindView() method of the Cursor adapter?
Adapter's purpose plan:
Create views
Attaching data to them
Return the view for the ListView to use.
Conclusion: Adapter doesn't know where the view it's creating will be shown.
However, the ListView does know about this and it's probably the only way you can get this working.
Example code to get you started:
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
int previousFirst = -1;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (previousFirst != firstVisibleItem) {
previousFirst = firstVisibleItem;
TextView textView = (TextView) view.findViewById(R.id.title);
if (textView != null) textView.setText("First: " + firstVisibleItem);
}
}
});
Problems with this code:
Once the first item changes, you need to set it's text back to the previous one.
Same goes with the view hierarchy. If you change how this view looks, after it's not the first one anymore you need to change it back.
ListView doesn't scroll upon creation. So the first item will not have the text changed until you scroll manually.
ListView doesn't include the options to customize the first visible item internally, that's why you have to use all these dirty hacks. However, it is possible :). I leave you to overcome these problems yourself.
Good luck!
This is for example as I haven't coded it yet, since I don't know how to start.
Let's say in Android I have String array with 100 values:
String[] myString = new String[100];
for (int number = 0; number < 100; number++) {
myString[number] = "image " + number;
}
Which way should I display for example 5 values per page (which view to use (table, grid) and should I use Fragment replace for each page).
I would like to achieve something like this, but in Android. I just need some guidelines to start.
In general if you have noticed, explicit pagination is not so often done in android, instead, infinite scrolling is used, for example your news feed in Facebook is a list, when you reach the bottom, it loads more and you can scroll more, at the bottom of that it loads again.
To implement this, it is quite easy, just set an onScrollListener and override onScrollStateChanged() method.
Set onScrollListener after initializing your ListView:
//In onCreate()
ListView listView = (ListView)findViewById(R.id.list_view);
listView.setOnScrollListener(new ListScrollListener());
Make your listener class:
private class ListScrollListener implements OnScrollListener{
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == SCROLL_STATE_IDLE) {
if (listView.getLastVisiblePosition() >= listView.getCount()-1) {
page_num++;
//TODO Load more list items.
//notify the adapter of the listview that data has changed
mAdapter.notifyDataSetChanged();
}
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
//This can be left blank
}
}
If you want you can add a footer with a ProgressBar to your ListView to display when you are loading more items with listView.addFooterView(View v);
You can find a nice tutorial about a custom ListView at Vogella tutorials.
I have my own Adapter extended from a BaseAdapter. I need to know when the latest element from the Adapter is shown on user's screen when user scrolls to it. Please advice.
You could override the onDraw() method in the element's view. When onDraw() is called, you know that the element is on the screen. Design wise, you could have an observer in each element's view. The adapter will register itself (or a different client) as an observer to the last/latest element's view. When the view is drawn, it will notify the observer.
Try this...
adapter.notifyDataSetChanged();
It will Refresh your adapter, notifies if any change..
Check the code below:
listView.setOnScrollListener(new OnScrollListener() {
// ...
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
boolean isLastRowShown = view.getLastVisiblePosition() == (view.getAdapter().getCount() - 1);
}
});
I have a requirement that I need to display a custom ListView with 'N' elements(Size of ListView may vary). I need to display how many elements user has viewed/ scrolled from my Listview...
My requirement is to I nned to display user that how many List items user not viewed yet..
Please let me know how get the count of elemnts...
You can use listView.getLastVisiblePosition(), which will tell you the position of the bottom-most visible item in your ListView.
// class members
private TextView textView;
private in maxViewed = 0;
// in your onCreate method
// find pointer to where you are displaying to user how many items he's viewed
textView = (TextView)findViewById(R.id.textView); // make sure to make this item in your layout
// make the scroll listener for the listview
listView.setOnScrollListener(new OnScrollListener(){
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
// keep track of how many items we've viewed
maxViewed = Math.max(maxViewed, listView.getLastVisiblePosition());
textView.setText(String.valueOf(maxViewed));
}
});