2009-08-27 18 views
11

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

ई.g मेरी वस्तुओं की सूची स्वचालित रूप से अधिक वस्तुओं के साथ बढ़ा दी जानी चाहिए।

कोई विचार यह कैसे किया जा सकता है?

मैं किसी भी टिप के लिए धन्यवादपूर्ण हूं।

उत्तर

5

आवश्यकताओं को देखते हुए, आप बेसएडाप्टर (कर्सर एडाप्टर के विपरीत जो एक अलग तंत्र का उपयोग करते हैं) का विस्तार कर रहे हैं।

यहाँ उस के लिए एक टुकड़ा है:

public View getView(int position, View convertView, ViewGroup parent) { 
    if (position == backingLinkedList.size()) { 
     //get more items and add them to the backingLinkedList in a background thread 
     notifyDataSetChanged(); 
    } 
} 
+13

यह उत्तर क्यों स्वीकार किया जाता है, सवाल एक स्क्रॉल व्यू के बारे में है, सूची दृश्य नहीं, स्क्रॉल दृश्य में एडाप्टर नहीं है। – Ixx

2

मैं एक ही समस्या थी और मैं ListView (स्वतः scrollview कहते हैं यदि आवश्यक हो) का उपयोग करते हुए और OnScrollListener निर्धारित करके इसे हल। Android: Understanding When Scrollview Has Reached The Bottom

स्रोत कोड:

 tableListView.setOnScrollListener(new OnScrollListener() { 

     public void onScrollStateChanged(AbsListView view, int scrollState) { 

     } 

     public void onScroll(AbsListView view, int firstVisibleItem, 
       int visibleItemCount, int totalItemCount) { 
      if (visibleItemCount == totalItemCount) 
      // too little items (ScrollView not needed) 
      { 
       java.lang.System.out.println("too little items to use a ScrollView!"); 
      } 
      else { 
       if ((firstVisibleItem + visibleItemCount) == totalItemCount) { 
        // put your stuff here 
        java.lang.System.out.println("end of the line reached!"); 
       } 
      } 

     } 
    }); 
8

कृपया यह लेख पढ़ें:

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

आप आप एक कस्टम विजेट बनाने में जोड़ सकते हैं चाहते हैं

यहाँ एक कोड नमूना है।

उदाहरण:

package se.marteinn.ui; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.ScrollView; 


/** 
* Triggers a event when scrolling reaches bottom. 
* 
* Created by martinsandstrom on 2010-05-12. 
* Updated by martinsandstrom on 2014-07-22. 
* 
* Usage: 
* 
* scrollView.setOnBottomReachedListener(
*  new InteractiveScrollView.OnBottomReachedListener() { 
*   @Override 
*   public void onBottomReached() { 
*    // do something 
*   } 
*  } 
* ); 
* 
* 
* Include in layout: 
* 
* <se.marteinn.ui.InteractiveScrollView 
*  android:layout_width="match_parent" 
*  android:layout_height="match_parent" /> 
* 
*/ 
public class InteractiveScrollView extends ScrollView { 
    OnBottomReachedListener mListener; 

    public InteractiveScrollView(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public InteractiveScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public InteractiveScrollView(Context context) { 
     super(context); 
    } 

    @Override 
    protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
     View view = (View) getChildAt(getChildCount()-1); 
     int diff = (view.getBottom()-(getHeight()+getScrollY())); 

     if (diff == 0 && mListener != null) { 
      mListener.onBottomReached(); 
     } 

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


    // Getters & Setters 

    public OnBottomReachedListener getOnBottomReachedListener() { 
     return mListener; 
    } 

    public void setOnBottomReachedListener(
      OnBottomReachedListener onBottomReachedListener) { 
     mListener = onBottomReachedListener; 
    } 


    /** 
    * Event listener. 
    */ 
    public interface OnBottomReachedListener{ 
     public void onBottomReached(); 
    } 

} 
+0

आप ओवरस्क्रोल (int scrollX, int scrollY) पर ओवरराइड भी कर सकते हैं , बूलियन clampedX, बूलियन clampedY) और जांचें कि क्या clampedY सच है – Julian

+0

प्रतिक्रिया के लिए धन्यवाद। –

+0

मैं उलझन में हूँ। जब आपने लिखा: int diff = (view.getBottom() - (recyclerView.getHeight() + recyclerView.getScrollY()); हमें recyclerView.getScrollY() जोड़ने की आवश्यकता क्यों है? getBottom() गणना हमेशा माता-पिता w.r.t है। हमें ऑफ़सेट जोड़ने की आवश्यकता क्यों होगी? क्या आप कृपया समझा सकते हैं? –

-1

यहाँ स्क्रॉल दृश्य उपवर्ग के बिना एक बहुत सरल समाधान है। मान लीजिए कि आपके पास रीसाइक्लर व्यू रीसाइक्लर व्यू वैरिएबल है,

रीसाइक्लिंगव्यू.कॉमपूटवर्टिकलस्क्रोलरेंज() आपको पूर्ण स्क्रॉल रेंज, या दूसरे शब्दों में, सामग्री की ऊंचाई देता है। पुस्तक से छेड़छाड़ स्क्रॉल दृश्य ऊंचाई

तो,

if((recyclerView.computeVerticalScrollOffset() == 
(recyclerView.computeVerticalScrollRange() - recyclerView.getHeight())) { 
    // You have hit rock bottom. 
    } 

-।

(recyclerView.computeVerticalScrollOffset() ऊपर से ऑफसेट आपके स्क्रॉल यह मान सामग्री ऊंचाई तक पर जाना होगा देता है। देखें का बच्चा दृश्य बस इतना हैकी लगता है।

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