Reputation: 9794
I try to add a relativelayout when the user begin to scroll down in a listview. I would like to show the relativelayout again when the user scroll to top and when first item of the listview is displayed.
My code works but there is a little "climb" when the user try to scroll. I think that the problem is located on listAtTop function but i don't know where.
list.setOnDetectScrollListener(new OnDetectScrollListener() {
@Override
public void onUpScrolling() {
/* do something */
Log.d("INFO", "UPPPPPPPPPPP");
if (listIsAtTop()) {
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
relative1.setVisibility(View.VISIBLE);
}
}
@Override
public void onDownScrolling() {
/* do something */
Log.d("INFO", "DOWNNNNNNNN");
if (!listIsAtTop()) {
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.relative1);
relative1.setVisibility(View.GONE);
}
}
});
private boolean listIsAtTop() {
if (list.getChildCount() == 0) {
return true;
}
return list.getFirstVisiblePosition() == 0 && (list.getChildCount() == 0 || list.getChildAt(0).getTop() == 0);
}
Upvotes: 2
Views: 3768
Reputation: 47807
Try this way
list.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
int mPosition=0;
int mOffset=0;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
int position = list.getFirstVisiblePosition();
View v = list.getChildAt(0);
int offset = (v == null) ? 0 : v.getTop();
if (mPosition < position || (mPosition == position && mOffset < offset)){
// Scrolled up
//search_layout.setVisibility(View.GONE);
} else {
// Scrolled down
//search_layout.setVisibility(View.VISIBLE);
}
}
});
In this code when you scroll down
hide layout and when you scroll up and reached to top
then Visible layout
Upvotes: 2