2016-05-03 19 views
1

Ich möchte eine endlose RecyclerView implementieren, aber bisher alle Implementierungen und Tutorials, die ich über beschreiben aus einem Server Laden von Daten gekommen sind, dann ist es das Laden oder dynamisch Hinzufügen von Elementen mit einer Schleife.Implementierung Endlose RecyclerView

In meinem Fall, ich habe bereits eine große Arraylist die Daten Ich möchte enthält im RecyclerView anzuzeigen.

Was ich brauche, ist zu sagen, die ersten 10 Elemente des Arrays anzuzeigen, und mehr auf das Laden, zeigen Sie die nächste Charge von 10

+0

Hängt vom verwendeten Datenbanksystem ab. – EpicPandaForce

+0

@EpicPandaForce Ich benutze keine Datenbank. Es ist nur eine einfache ArrayList. –

+0

Dann hängen Sie einfach die neuen Elemente in die Liste und rufen Sie dann 'notifyDatasetChanged()' (oder 'notifyItemRangeInserted (int positionStart, int ItemCount)', wenn Sie die Phantasie Animationen auch). Aber wenn Sie tatsächliche Seiten mit jeweils nur 10 Elementen wünschen, dann ist es ein bisschen anders. – EpicPandaForce

Antwort

3

Dies könnte Ihr Ziel erreichen.

public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { 
    public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName(); 

private int previousTotal = 0; // The total number of items in the dataset after the last load 
private boolean loading = true; // True if we are still waiting for the last set of data to load. 
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more. 
int firstVisibleItem, visibleItemCount, totalItemCount; 

private int current_page = 1; 

private LinearLayoutManager mLinearLayoutManager; 

public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) { 
    this.mLinearLayoutManager = linearLayoutManager; 
} 

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

    visibleItemCount = recyclerView.getChildCount(); 
    totalItemCount = mLinearLayoutManager.getItemCount(); 
    firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition(); 

    if (loading) { 
     if (totalItemCount > previousTotal) { 
      loading = false; 
      previousTotal = totalItemCount; 
     } 
    } 
    if (!loading && (totalItemCount - visibleItemCount) 
      <= (firstVisibleItem + visibleThreshold)) { 
     // End has been reached 

     // Do something 
     current_page++; 

     onLoadMore(current_page); 

     loading = true; 
    } 
} 

public abstract void onLoadMore(int current_page); 
} 

Und Probenaktivität

public class SampleActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_sample); 

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list); 
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(linearLayoutManager); 
    recyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(linearLayoutManager) { 
     @Override 
     public void onLoadMore(int current_page) { 
      // do something... 
     } 
    }); 
} 
} 

Edit: See here: Endless Scrolling with AdapterViews

+0

Ich habe tatsächlich diesen Code, aber das Problem ist, was in der OnLoadMore-Methode zu tun ist. Ich habe versucht, 'subList()' auf der Hauptliste zu machen, aber die Daten werden immer noch nicht angezeigt. –

+0

@AlexKombo Im Grunde möchten Sie dem Adapter mitteilen, dass sich die Elemente und die items.size() geändert haben. –

+0

@AlexKombo [hier ist ein Beispiel] (https://gist.github.com/rogerhu/17aca6ad4dbdb3fa5892) –

0

Versuchen Sie es mit:

recyclerView.addOnScrollListener (recyclerViewOnScrollListener); 

private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() { 

    private final String LOG_TAG = "PAGINATION_"; 
    boolean    isIdle; 

    @Override 
    public void onScrollStateChanged (RecyclerView recyclerView, int newState) { 

     try { 
      super.onScrollStateChanged (recyclerView, newState); 
      isIdle = (newState == RecyclerView.SCROLL_STATE_IDLE); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onScrolled (RecyclerView recyclerView, int dx, int dy) { 

     firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition(); 

        super.onScrolled (recyclerView, dx, dy); 

        if (!isIdle dy > 0) { 

         int totalItems = yourList.size()(); 

         totalItemCount = layoutManager.getItemCount(); 
         visibleItemCount = layoutManager.getChildCount(); 

         Log.i (LOG_TAG, "Max: " + totalItems + ", Total: " + totalItemCount + ", Previous Total: " + previousTotal + ", Visible: " + visibleItemCount + ", First: " + firstVisibleItemPosition); 

         if (isLoading) { 
          if (totalItemCount != previousTotal) { 
           isLoading = false; 
           previousTotal = totalItemCount; 
          } 
         } 

         isLastPage = (totalItemCount >= totalItems); 

         Log.i (LOG_TAG, "DETAILS: currentPage: " + currentPage + ", isLoading: " + isLoading + ", isLast: " + isLastPage); 
         if (!isLoading && !isLastPage) { 
          if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0 && totalItems >= (currentPage * PAGE_LIMIT) && totalItems > totalItemCount) { 
           if (totalItems > (currentPage * PAGE_LIMIT)) { 
            currentPage = (totalItemCount/PAGE_LIMIT) + 1; 
            isLoading = true; 
            //load more data using currentPage and PAGE_LIMIT 
           } 
          } 
         } 
        } 
        previousTotal = totalItemCount; 
       } 
       catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 

     } 
    } 

}; 
1

können Sie diese verwenden Endless Adapter

  • Es ist generisch, Sie geben es (Sie Modell, Recycler.ViewHolder)
  • Sie können den Adapter entweder zeigen laden oder nicht einstellen.
  • Lassen Sie die Empfänger prüfen, wann das Laden beendet ist und wann das Laden beginnt.

können Sie überprüfen auch die Probe für weitere Informationen

0

versuchen diese Klasse verwenden es mir helfen, eine Menge:

public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener { 
    // The minimum amount of items to have below your current scroll position 
    // before loading more. 
    private int visibleThreshold = 5; 
    // The current offset index of data you have loaded 
    private int currentPage = 0; 
    // The total number of items in the dataset after the last load 
    private int previousTotalItemCount = 0; 
    // True if we are still waiting for the last set of data to load. 
    private boolean loading = true; 
    // Sets the starting page index 
    private int startingPageIndex = 0; 

    RecyclerView.LayoutManager mLayoutManager; 

    public EndlessRecyclerViewScrollListener(LinearLayoutManager layoutManager) { 
     this.mLayoutManager = layoutManager; 
    } 

    public EndlessRecyclerViewScrollListener(GridLayoutManager layoutManager) { 
     this.mLayoutManager = layoutManager; 
     visibleThreshold = visibleThreshold * layoutManager.getSpanCount(); 
    } 

    public EndlessRecyclerViewScrollListener(StaggeredGridLayoutManager layoutManager) { 
     this.mLayoutManager = layoutManager; 
     visibleThreshold = visibleThreshold * layoutManager.getSpanCount(); 
    } 

    public int getLastVisibleItem(int[] lastVisibleItemPositions) { 
     int maxSize = 0; 
     for (int i = 0; i < lastVisibleItemPositions.length; i++) { 
      if (i == 0) { 
       maxSize = lastVisibleItemPositions[i]; 
      } 
      else if (lastVisibleItemPositions[i] > maxSize) { 
       maxSize = lastVisibleItemPositions[i]; 
      } 
     } 
     return maxSize; 
    } 

    // This happens many times a second during a scroll, so be wary of the code you place here. 
    // We are given a few useful parameters to help us work out if we need to load some more data, 
    // but first we check if we are waiting for the previous load to finish. 
    @Override 
    public void onScrolled(RecyclerView view, int dx, int dy) { 
     int lastVisibleItemPosition = 0; 
     int totalItemCount = mLayoutManager.getItemCount(); 

     if (mLayoutManager instanceof StaggeredGridLayoutManager) { 
      int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) mLayoutManager).findLastVisibleItemPositions(null); 
      // get maximum element within the list 
      lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions); 
     } else if (mLayoutManager instanceof GridLayoutManager) { 
      lastVisibleItemPosition = ((GridLayoutManager) mLayoutManager).findLastVisibleItemPosition(); 
     } else if (mLayoutManager instanceof LinearLayoutManager) { 
      lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition(); 
     } 

     // If the total item count is zero and the previous isn't, assume the 
     // list is invalidated and should be reset back to initial state 
     if (totalItemCount < previousTotalItemCount) { 
      this.currentPage = this.startingPageIndex; 
      this.previousTotalItemCount = totalItemCount; 
      if (totalItemCount == 0) { 
       this.loading = true; 
      } 
     } 
     // If it’s still loading, we check to see if the dataset count has 
     // changed, if so we conclude it has finished loading and update the current page 
     // number and total item count. 
     if (loading && (totalItemCount > previousTotalItemCount)) { 
      loading = false; 
      previousTotalItemCount = totalItemCount; 
     } 

     // If it isn’t currently loading, we check to see if we have breached 
     // the visibleThreshold and need to reload more data. 
     // If we do need to reload some more data, we execute onLoadMore to fetch the data. 
     // threshold should reflect how many total columns there are too 
     if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) { 
      currentPage++; 
      onLoadMore(currentPage, totalItemCount, view); 
      loading = true; 
     } 
    } 

    // Call this method whenever performing new searches 
    public void resetState() { 
     this.currentPage = this.startingPageIndex; 
     this.previousTotalItemCount = 0; 
     this.loading = true; 
    } 

    // Defines the process for actually loading more data based on page 
    public abstract void onLoadMore(int page, int totalItemsCount, RecyclerView view); 

} 

Referenz: