2016-03-21 21 views
14

Ho questo codice per un RecyclerView.Rileva quando RecyclerView raggiunge la posizione più bassa durante lo scorrimento

recyclerView = (RecyclerView)rootview.findViewById(R.id.fabric_recyclerView); 
    recyclerView.setLayoutManager(layoutManager); 
    recyclerView.addItemDecoration(new RV_Item_Spacing(5)); 
    FabricAdapter fabricAdapter=new FabricAdapter(ViewAdsCollection.getFabricAdsDetailsAsArray()); 
    recyclerView.setAdapter(fabricAdapter); 

Ho bisogno di sapere quando RecyclerView raggiunge la posizione più bassa durante lo scorrimento. È possibile ? Se sì, come?

+0

Prova http://stackoverflow.com/questions/27841740/how-to-know-whether-a-recyclerview-linearlayoutmanager-is-scrolled-to-top-or- b – Raghavendra

+0

http: // sta ckoverflow.com/questions/26543131/how-to-implement-endless-list-with-recyclerview –

+1

recyclerView.canScrollVertically (int direction); Quale dovrebbe essere il parametro come quello che dobbiamo passare qui? – Adrian

risposta

25

Basta implementare un addOnScrollListener() sul tuo recyclerview. Quindi all'interno del listener di scroll implementare il codice sottostante.

RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() { 
     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      if (mIsLoading) 
       return; 
      int visibleItemCount = mLayoutManager.getChildCount(); 
      int totalItemCount = mLayoutManager.getItemCount(); 
      int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition(); 
      if (pastVisibleItems + visibleItemCount >= totalItemCount) { 
       //End of list 
      } 
     } 
    }; 
+1

Puoi spiegare un po 'di mIsLoading per favore? dove stai impostando o modificando il valore. – MiguelHincapieC

+0

mLoading è solo una variabile booleana che indica se la vista è impegnata o meno. Per esempio; se l'app sta popolando il recyclerview, mLoading sarà vero e diventa falso una volta che l'elenco sarà popolato. –

+0

ohh ok, carri armati per la risposta. Ho pensato che avesse un ruolo più importante xD. – MiguelHincapieC

3

C'è la mia implementazione, è molto utile per StaggeredGridLayout.

Usage:

private EndlessScrollListener scrollListener = 
     new EndlessScrollListener(new EndlessScrollListener.RefreshList() { 
      @Override public void onRefresh(int pageNumber) { 
       //end of the list 
      } 
     }); 

rvMain.addOnScrollListener(scrollListener); 

Listener implementazione:

class EndlessScrollListener extends RecyclerView.OnScrollListener { 
private boolean isLoading; 
private boolean hasMorePages; 
private int pageNumber = 0; 
private RefreshList refreshList; 
private boolean isRefreshing; 
private int pastVisibleItems; 

EndlessScrollListener(RefreshList refreshList) { 
    this.isLoading = false; 
    this.hasMorePages = true; 
    this.refreshList = refreshList; 
} 

@Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
    super.onScrolled(recyclerView, dx, dy); 
    StaggeredGridLayoutManager manager = 
      (StaggeredGridLayoutManager) recyclerView.getLayoutManager(); 

    int visibleItemCount = manager.getChildCount(); 
    int totalItemCount = manager.getItemCount(); 
    int[] firstVisibleItems = manager.findFirstVisibleItemPositions(null); 
    if (firstVisibleItems != null && firstVisibleItems.length > 0) { 
     pastVisibleItems = firstVisibleItems[0]; 
    } 

    if (visibleItemCount + pastVisibleItems >= totalItemCount && !isLoading) { 
     isLoading = true; 
     if (hasMorePages && !isRefreshing) { 
      isRefreshing = true; 
      new Handler().postDelayed(new Runnable() { 
       @Override public void run() { 
        refreshList.onRefresh(pageNumber); 
       } 
      }, 200); 
     } 
    } else { 
     isLoading = false; 
    } 
} 

public void noMorePages() { 
    this.hasMorePages = false; 
} 

void notifyMorePages() { 
    isRefreshing = false; 
    pageNumber = pageNumber + 1; 
} 

interface RefreshList { 
    void onRefresh(int pageNumber); 
} } 
+0

Perché aggiungere un ritardo di 200 millisecondi alla richiamata? – Mani

+0

@Mani Non ricordo esattamente in questo momento, ma forse l'ho fatto solo per effetto liscio ... –

23

v'è anche un modo semplice per farlo

rv_convo.addOnScrollListener(new RecyclerView.OnScrollListener() { 
    @Override 
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) { 
     super.onScrollStateChanged(recyclerView, newState); 

     if (!recyclerView.canScrollVertically(1)) { 
      Toast.makeText(ChatDetailsActivity.this,"LAst",Toast.LENGTH_LONG).show(); 

     } 
    } 
}); 

interi di direzione: -1 fino 1 per giù, 0 restituirà sempre false.

+0

onScrolled() impedisce che il rilevamento si verifichi due volte. –

+0

dai uno sguardo a https://stackoverflow.com/a/48514857/6674369 –

1

Sono stato anche alla ricerca di questa domanda, ma non ho trovato la risposta che mi ha soddisfatto, così creo propria realizzazione di recyclerView:

class CustomRecyclerView: RecyclerView{ 

    abstract class TopAndBottomListener{ 
     open fun onBottomNow(onBottomNow:Boolean){} 
     open fun onTopNow(onTopNow:Boolean){} 
    } 


    constructor(c:Context):this(c, null) 
    constructor(c:Context, attr:AttributeSet?):super(c, attr, 0) 
    constructor(c:Context, attr:AttributeSet?, defStyle:Int):super(c, attr, defStyle) 


    private var linearLayoutManager:LinearLayoutManager? = null 
    private var topAndBottomListener:TopAndBottomListener? = null 
    private var onBottomNow = false 
    private var onTopNow = false 
    private var onBottomTopScrollListener:RecyclerView.OnScrollListener? = null 


    fun setTopAndBottomListener(l:TopAndBottomListener?){ 
     if (l != null){ 
      checkLayoutManager() 

      onBottomTopScrollListener = createBottomAndTopScrollListener() 
      addOnScrollListener(onBottomTopScrollListener) 
      topAndBottomListener = l 
     } else { 
      removeOnScrollListener(onBottomTopScrollListener) 
      topAndBottomListener = null 
     } 
    } 

    private fun createBottomAndTopScrollListener() = object :RecyclerView.OnScrollListener(){ 
     override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { 
      checkOnTop() 
      checkOnBottom() 
     } 
    } 

    private fun checkOnTop(){ 
     val firstVisible = linearLayoutManager!!.findFirstCompletelyVisibleItemPosition() 
     if(firstVisible == 0 || firstVisible == -1 && !canScrollToTop()){ 
      if (!onTopNow) { 
       onTopNow = true 
       topAndBottomListener?.onTopNow(true) 
      } 
     } else if (onTopNow){ 
      onTopNow = false 
      topAndBottomListener?.onTopNow(false) 
     } 
    } 

    private fun checkOnBottom(){ 
     var lastVisible = linearLayoutManager!!.findLastCompletelyVisibleItemPosition() 
     val size = linearLayoutManager!!.itemCount - 1 
     if(lastVisible == size || lastVisible == -1 && !canScrollToBottom()){ 
      if (!onBottomNow){ 
       onBottomNow = true 
       topAndBottomListener?.onBottomNow(true) 
      } 
     } else if(onBottomNow){ 
      onBottomNow = false 
      topAndBottomListener?.onBottomNow(false) 
     } 
    } 


    private fun checkLayoutManager(){ 
     if (layoutManager is LinearLayoutManager) 
      linearLayoutManager = layoutManager as LinearLayoutManager 
     else 
      throw Exception("for using this listener, please set LinearLayoutManager") 
    } 

    private fun canScrollToTop():Boolean = canScrollVertically(-1) 
    private fun canScrollToBottom():Boolean = canScrollVertically(1) 
} 

poi nella vostra attività/frammento:

override fun onCreate() { 
    customRecyclerView.layoutManager = LinearLayoutManager(context) 
} 

override fun onResume() { 
    super.onResume() 
    customRecyclerView.setTopAndBottomListener(this) 
} 

override fun onStop() { 
    super.onStop() 
    customRecyclerView.setTopAndBottomListener(null) 
} 

spero che faccia a qualcuno qualcuno ;-)

Problemi correlati