Reputation: 2442
How can I detect listView
item to change or make some actions when it was scrolled above the center of screen
?
UPD Solution
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int half = visibleItemCount/2;
int to = firstVisibleItem+half;
for (int i=firstVisibleItem; i<to; i++) {
// actions for item above center
Item item = (Item)adapter.getItem(i);
item.read = true;
}
adapter.notifyDataSetChanged();
}
Upvotes: 3
Views: 91
Reputation: 3086
You can achieve this using a combination of methods. If your ListView
stretches the whole screen (i.e., height is set to MATCH_PARENT
), you can determine the center of your screen as the total number of children / 2. Use the getChildCount() method for this job.
As Ayoub said, you can also use a ListView.OnScrollListener to listen for scroll changes in your ListView.
In case your ListView's height does not match the available window screen, you can use this to find the available window height:
DisplayMetrics metrics = getResources().getDisplayMetrics();
int windowWidth = metrics.widthPixels;
int windowHeight = metrics.heightPixels
Upvotes: 2
Reputation: 341
You can set a ListView.OnScrollListener, and check if the item you want scrolls above the center. The onScroll method will supply you with the number of items visible, the index of the first one and the total items of your list, knowing this you should be able to determinate when an element scrolls over a position.
Upvotes: 1