In android, I have a button that appears when a user scrolls up, and disappears otherwise. However, also, when it appears, I am trying to make it disappear again after 3 seconds, if the user stopped scrolling (after 3 seconds of being idle). I tried the following code,
//detecting whether the user is scrolling up or down, to make the refresh button visible/invisible
listView.setOnTouchListener(new View.OnTouchListener() {
int scrollEventListSize = 5;
float lastY;
// Used to correct for occasions when user scrolls down(/up) but the onTouchListener detects it incorrectly. We will store detected up-/down-scrolls with -1/1 in this list and evaluate later which occured more often
List<Integer> downScrolledEventsHappened;
@Override
public boolean onTouch(View v, MotionEvent event) {
float diff = 0;
if(event.getAction() == event.ACTION_DOWN){
lastY = event.getY();
downScrolledEventsHappened = new LinkedList<Integer>();
}
else if(event.getAction() == event.ACTION_MOVE){
diff = event.getY() - lastY;
lastY = event.getY();
if(diff>0)
downScrolledEventsHappened.add(1);
else
downScrolledEventsHappened.add(-1);
//List needs to be filled with some events, will happen very quickly
if(downScrolledEventsHappened.size() == scrollEventListSize+1){
downScrolledEventsHappened.remove(0);
int res=0;
for(int i=0; i<downScrolledEventsHappened.size(); i++){
res+=downScrolledEventsHappened.get(i);
}
if (res > 0) {
Log.i("INFO", "Scrolled up");
refreshButton.setVisibility(View.VISIBLE);
}
else {
Log.i("INFO", "Scrolled down");
refreshButton.setVisibility(View.INVISIBLE);
}
}
}
return false; // don't interrupt the event-chain
}
});
//Removing refresh button when user stops scrolling up or already at the top of the list.
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
refreshButton.setVisibility(View.INVISIBLE);
}
}, 3000);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem == 0 && listIsAtTop()){
System.out.println("List at Top....Refresh Button is disappearing...");
refreshButton.setVisibility(View.INVISIBLE);
}
}
});
This code however, is not exactly giving me what I want. It is true that the button disappears after 3 seconds of the user being idle, but it also does so, while the user is scrolling up again! I mean, I need a mechanism in which the after-3-seconds disappearing idea to be canceled when the user scrolls up again. how can I do that? Thank You.
when use is scrolling up cancel the handlers pending events and start handler again
something like this
start handler in this way