2009-08-27 16 views

Odpowiedz

5

Biorąc pod uwagę wymagania, prawdopodobnie rozszerzysz BaseAdapter (w przeciwieństwie do CursorAdapter, który wykorzystuje inny mechanizm).

Oto urywek za to:

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

Dlaczego ta odpowiedź jest akceptowana, pytanie dotyczy widoku przewijania, a nie widoku listy, widok przewijania nie ma adaptera – Ixx

2

miałem ten sam problem i rozwiązać go za pomocą ListView (automatycznie dodaje Scrollview razie potrzeby) i ustawienie OnScrollListener.

Oto przykładowy kod:

 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

Proszę przeczytać ten artykuł: kod Android: Understanding When Scrollview Has Reached The Bottom

Źródło:

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

Jeśli chcesz, możesz dodać dokonać niestandardowego widżet.

przykład:

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

Możesz także zastąpić onOverScrolled (int scrollX, int scrollY) , boolean clampedX, boolean clampedY) i sprawdź, czy clampedY jest prawdziwe – Julian

+0

Dziękujemy za informację zwrotną. –

+0

Jestem zdezorientowany. Kiedy napisałeś: int diff = (view.getBottom() - (recyclerView.getHeight() + recyclerView.getScrollY())); Dlaczego musimy dodać recyclerView.getScrollY()? Obliczenie getBottom() zawsze jest w.r.t rodzicem. Dlaczego potrzebujemy dodać przesunięcie? Czy możesz wyjaśnić, proszę? –

-1

Tutaj jest znacznie prostsze rozwiązanie bez podklasy widok przewijania. Zakładając, że masz zmienną RecyclerView recyclerView,

recyclerView.computeVerticalScrollRange() podaje pełny zakres przewijania lub innymi słowy wysokość treści.

(recyclerView.computeVerticalScrollOffset() daje przesunięcie od góry podczas przewijania Wartość ta będzie trwać aż do wysokości zawartości. -. Przewijania wysokości widok

Więc

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

Manipulowanie zwój widok widoku dziecka wydaje się być tak hackowaty

Powiązane problemy