2009-08-27 9 views

Respuesta

5

Teniendo en cuenta los requisitos, es probable que extienda BaseAdapter (en comparación con CursorAdapter que utiliza un mecanismo diferente).

He aquí un fragmento de eso:

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

Por qué se acepta esta respuesta, la pregunta es acerca de una vista de desplazamiento, no una vista de lista, la vista de desplazamiento no tiene adaptador. – Ixx

2

que tenían el mismo problema y lo resolvió mediante el uso de la ListView (añade automáticamente ScrollView si es necesario) y el establecimiento de OnScrollListener.

Aquí es un ejemplo de código:

 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

Por favor, lea este artículo: Código Android: Understanding When Scrollview Has Reached The Bottom

Fuente:

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

Si se desea se puede añadir hacer un widget personalizado.

Ejemplo:

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

También puede anular onOverScrolled (int scrollX, int scrollY , boolean clampedX, Boolean clampedY) y compruebe si clampedY es verdadero – Julian

+0

Gracias por sus comentarios. –

+0

Estoy confundido. Cuando escribió: int diff = (view.getBottom() - (recyclerView.getHeight() + recyclerView.getScrollY())); ¿Por qué necesitamos agregar recyclerView.getScrollY()? El cálculo de getBottom() siempre es w.r.t el padre. ¿Por qué tendríamos que agregar el desplazamiento? Podrías explicar por favor? –

-1

Aquí es una solución mucho más simple sin tener que subclase vista de desplazamiento. Suponiendo que tiene una variable RecyclerView recyclerView,

recyclerView.computeVerticalScrollRange() le proporciona el rango de desplazamiento completo, o en otras palabras, la altura del contenido.

(recyclerView.computeVerticalScrollOffset() proporciona el desplazamiento de la parte superior mientras se desplaza Este valor se continuará hasta la altura del contenido -.. De desplazamiento de altura vista

Así,

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

Manipulando el rollo La vista secundaria de vista parece tan hacky

Cuestiones relacionadas