2011-02-10 31 views
35

संभव डुप्लिकेट हिट्स जब:
How to trigger an event when scrollView reach the bottom with Android?एंड्रॉयड: का पता लगा रहा scrollview नीचे

मैं जब मेरे scrollview स्क्रीन के नीचे करने के लिए हो जाता है पता लगाने के लिए एक घंटे के लिए कोशिश कर रहे हैं या तो। विभिन्न स्क्रीन आकारों और एंड्रॉइड के साथ क्या शामिल नहीं है, मैं बस यह नहीं कह सकता कि यह नीचे की स्थिति में है जब इसकी वाई स्थिति एक निश्चित मूल्य तक पहुंच जाती है, लेकिन ऐसा कोई तरीका नहीं है जो मुझे पता चलने पर पता चलता है।

मुझे यकीन है कि इसके लिए एक आसान समाधान है, किसी अन्य चर या कुछ द्वारा दृश्य की ऊंचाई घटाकर, लेकिन किसी कारण से यह मेरे लिए क्लिक नहीं कर रहा है। किसी भी विचार की सराहना की जाएगी, धन्यवाद।

उत्तर

80

इस प्रयास करें:

@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); 
} 

यह लागू, ScrollView का विस्तार और फिर onScrollChanged विधि (View से विरासत में मिली) ओवरराइड करने के लिए।

संदर्भ: Android: Understanding when ScrollView has reached the bottom

+27

मेरे अनुभव में, आप अगर (diff <= 0) – littleK

+1

तभी सार्थक होता है यह वापस जाने के लिए -1 होता है के लिए जाँच करनी चाहिए। – BlackHatSamurai

+10

सिर्फ क्रेडिट देने के लिए, उत्तर http://www.marteinn.se/blog/?p=485 – Gabor

संबंधित मुद्दे