Skip to content Skip to sidebar Skip to footer

Animate/hide Button When Close To End Of Listview

I have view like this: ListView is on the whole screen and the button is always visible (on FrameLayout). I want to hide the button outside the screen (below it) with animation wh

Solution 1:

I rarely use ListView over RecyclerView, but with RecyclerView I achieve this by implementing OnInterceptTouchEvent. ListView also has the same method that you can Override. Once you've done this, you can detect scroll events by listening for the distance between the finger down event, and the updated position once it has moved:

privatestaticfinalintTOUCH_SLOP=200; //Set this to your preference.int originalFingerPosition;
//...@OverridepublicbooleanonInterceptTouchEvent(MotionEvent e) {
        switch (e.getAction()){
            case MotionEvent.ACTION_DOWN:
                originalFingerPosition = e.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (e.getY() - originalFingerPos > TOUCH_SLOP){
                    //This counts as a down swipe. Do something.
                } elseif (e.getY() + TOUCH_SLOP < originalFingerPosition ){
                    //This is an upswipe. Do something else.
                }
            }
        returnfalse;
        }

Be sure to return false once you're done so you don't consume the event and prevent other views from behaving as they should with a scroll.

After that, it's just a case of launching a suitable animation, like a TranslateAnimation, to scroll the button off screen.

EDIT: If you only want the animation to occur when you reach the last item of your list, then I would instead take a look at this answer and include the code to animate the Button off screen there:

Adapted from the example given in this post

listView.setOnScrollListener(OnScrollListener);
//...@OverridepublicvoidonScroll(AbsListView lw, finalint firstVisibleItem,
             finalint visibleItemCount, finalint totalItemCount) {

switch(lw.getId()) {
    case android.R.id.list:
         finalintlastItem= firstVisibleItem + visibleItemCount;
         if((lastItem == totalItemCount) && (button.getVisibility() == View.VISIBLE)) {
          Animationtranslate=newTranslateAnimation(
                    originalXPosition, originalXPosition,  originalYPosition, listView.getHeight());
            translate.setDuration(250);
            translate.setInterpolator(newLinearInterpolator());
            button.startAnimation(translate);
            button.setEnabled(false);
            button.setVisibility(View.GONE);
          } else {
          //The Opposite...
          }
     }
}

Post a Comment for "Animate/hide Button When Close To End Of Listview"