I have a ListView, first its scrolled down, now when we scroll up,it reach top most point. I want to detect that .Is there any way?I am developing application with api level
You will need to check what is the first visible position then applying Graeme's solution to see if the first visible listview item is at the top position.
Something like lv.getFirstVisiblePosition() == 0 && (lv.getChildCount() == 0 || lv.getChildAt(0).getTop() == 0)
edit
See comments below as to why, especially on different API versions (esp later ones), this isn't a foolproof way to see if you're list is at the top (padding etc). However, it does give you a start for a solution on devices below API 14:
private boolean listIsAtTop() {
if(listView.getChildCount() == 0) return true;
return listView.getChildAt(0).getTop() == 0;
}
As far as my implementation years ago - this worked perfectly at the time.
You can use an OnScrollListener to be notified the position 0 is now visible. Use the onScroll
method.
My friends, combining Graeme's answer with the onScroll method...
listView.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 == 0 && listIsAtTop()){
swipeRefreshLayout.setEnabled(true);
}else{
swipeRefreshLayout.setEnabled(false);
}
}
});
private boolean listIsAtTop() {
if(listView.getChildCount() == 0) return true;
return listView.getChildAt(0).getTop() == 0;
}
Graeme's answer is close but is missing something that user2036198 added, a check for getFirstVisiblePosition(). getChildAt(0) doesn't return the very first view for the very first item in the list. AbsListView implementations don't make a single view for every position and keep them all in memory. Instead, view recycling takes effect to limit the number of views instantiated at any one time.
The fix is pretty simple:
public boolean canScrollVertically(AbsListView view) {
boolean canScroll = false;
if (view != null && view.getChildCount() > 0) {
// First item can be partially visible, top must be 0 for the item
canScroll = view.getFirstVisiblePosition() != 0 || view.getChildAt(0).getTop() != 0;
}
return canScroll;
}
For best results on ICS or higher, always use ViewCompat from the v4 support library or View.canScrollVertically(). Use the above method on lower API levels as ViewCompat always returns false for canScrollVertically() and canScrollHorizontally() below ICS.
This question is old but I have a solution that works perfectly and it is possible that works for someone looking for a solution.
int limitRowsBDshow = 10; //size limit your list
listViewMessages.setOnScrollListener(new AbsListView.OnScrollListener() {
int counter = 1;
int currentScrollState;
int currentFirstVisibleItem;
int currentVisibleItemCount;
int currentTotalItemCount;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.currentScrollState = scrollState;
this.isScrollCompleted();
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.currentTotalItemCount = totalItemCount;
}
private void isScrollCompleted() {
if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
/*** detect if there's been a scroll which has completed ***/
counter++;
if (currentFirstVisibleItem == 0 && currentTotalItemCount > limitRowsBDshow - 1) {
linearLay20msgMas.setVisibility(View.VISIBLE);
}
}
}
});
This code is found a time ago (here StackOverflow). But I can not find this to mention