2011-02-10 29 views
35

Duplicar posible:
How to trigger an event when scrollView reach the bottom with Android?Android: Detección Cuando ScrollView toca fondo

he estado intentando durante una hora o así para detectar cuando mi ScrollView llega a la parte inferior de la pantalla. Debido a los distintos tamaños de pantalla y lo que no está relacionado con Android, no puedo decir simplemente que está en la parte inferior cuando su posición Y alcanza cierto valor, pero no hay un método que detecte cuándo está en la parte inferior que encontré.

Estoy seguro de que hay una solución simple para esto, restando el alto de la vista por alguna otra variable o algo, pero por alguna razón, simplemente no está haciendo clic para mí. Cualquier idea sería apreciada, gracias.

Respuesta

80

Prueba esto:

@Override 
protected void onScrollChanged(int l, int t, int oldl, int oldt) 
{ 
    // Grab the last child placed in the ScrollView, we need it to determinate the bottom position. 
    View view = (View) getChildAt(getChildCount()-1); 

    // Calculate the scrolldiff 
    int diff = (view.getBottom()-(getHeight()+getScrollY())); 

    // if diff is zero, then the bottom has been reached 
    if(diff == 0) 
    { 
     // notify that we have reached the bottom 
     Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached"); 
    } 

    super.onScrollChanged(l, t, oldl, oldt); 
} 

Para implementar esto, se extienden ScrollView y luego anular la onScrollChanged método (heredado de View).

Referencia: Android: Understanding when ScrollView has reached the bottom

+27

En mi experiencia, usted debe comprobar que si (esta <= 0) – littleK

+1

tiene sentido si se pasa a devolver -1. – BlackHatSamurai

+10

solo para dar crédito, se copió la respuesta de http://www.marteinn.se/blog/?p=485 – Gabor

Cuestiones relacionadas