2015-08-21 3 views
3

Я пытаюсь реализовать бесконечную прокрутку вверх, где я бы загружал больше данных, когда пользователь приближается к верхней части списка (например, в приложении для обмена сообщениями). У меня есть прокрутка до нижнего класса, который отлично работает, но мне не удается найти библиотеку или изменить этот код, чтобы обнаружить прокрутку вверх. Может ли кто-нибудь помочь?Android: Infinite Прокрутите до TOP в listview

Вот класс:

public abstract class EndlessScrollListener implements AbsListView.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; 

    public EndlessScrollListener() { 
    } 

    public EndlessScrollListener(int visibleThreshold) { 
     this.visibleThreshold = visibleThreshold; 
    } 

    public EndlessScrollListener(int visibleThreshold, int startPage) { 
     this.visibleThreshold = visibleThreshold; 
     this.startingPageIndex = startPage; 
     this.currentPage = startPage; 
    } 

    // 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 onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
    { 
     // 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; 
      currentPage++; 
     } 

     // 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. 
     if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) { 
      onLoadMore(currentPage + 1, totalItemCount); 
      loading = true; 
     } 
    } 

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

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // Don't take any action on changed 
    } 
} 
+1

просто вы должны обнаружить верхний свиток, а затем загрузить данные после этого вы можете извещать адаптер –

+0

вы можете использовать swiperefreshlayout –

+0

@GopalSinghSirvi Ну, что это, вероятно, не имеет большого значения, но, как я начинаю на андроиди я не могу узнать, как определить верхний прокрутки, я надеюсь получить помощь –

ответ

0

Я сделал некоторые изменения в свой класс, добавив параметр, чтобы определить направление setScrollDirection по умолчанию он будет SCROLL_DIRECTION_DOWN пожалуйста, проверить его и дайте мне знать, как это работает :)

public abstract class EndlessScrollListener implements AbsListView.OnScrollListener { 

    public final static int SCROLL_DIRECTION_UP = 0; 
    public final static int SCROLL_DIRECTION_DOWN = 1; 

    // 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; 

    private int scrollDirection = SCROLL_DIRECTION_DOWN; 

    public EndlessScrollListener() { 
    } 

    public EndlessScrollListener(int visibleThreshold) { 
     this.visibleThreshold = visibleThreshold; 
    } 

    public EndlessScrollListener(int visibleThreshold, int startPage) { 
     this.visibleThreshold = visibleThreshold; 
     this.startingPageIndex = startPage; 
     this.currentPage = startPage; 
    } 

    // 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 onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
    { 
     Log.v("EndlessScroll", "firstVisibleItem: "+firstVisibleItem); 
     Log.v("EndlessScroll", "visibleItemCount: "+visibleItemCount); 
     Log.v("EndlessScroll", "totalItemCount: "+totalItemCount); 
     // 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; 
      currentPage++; 
     } 

     // 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. 
     if (!loading) 
     { 
      if(scrollDirection == SCROLL_DIRECTION_DOWN && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) { 
       onLoadMore(currentPage + 1, totalItemCount); 
       loading = true; 
      } 
      else if(scrollDirection == SCROLL_DIRECTION_UP && firstVisibleItem<=visibleThreshold) { 
       onLoadMore(currentPage + 1, totalItemCount); 
       loading = true; 
      } 
     } 
    } 

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

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // Don't take any action on changed 
    } 

    public int getScrollDirection() { 
     return scrollDirection; 
    } 

    public void setScrollDirection(int scrollDirection) { 
     if (scrollDirection == SCROLL_DIRECTION_DOWN || scrollDirection == SCROLL_DIRECTION_UP) 
     { this.scrollDirection = scrollDirection; } 
    } 

    public boolean isLoading() { 
     return loading; 
    } 

    public void finishedLoading() { 
     this.loading = false; 
    } 

} 
+0

Это именно то, что мне нужно, спасибо! –

+0

KaliAney @Cesar, где и когда вызывать метод setScrollDirection? – androidevil

Смежные вопросы