2013-05-31 31 views
1

請原諒我的英文,我是法國人...Android - OnScrollListener爲交錯網格視圖

所以,我有一個問題,我的Android應用程序。 我必須將網格視圖整合爲Pinterest樣式。 我發現這個lib:交錯網格視圖(https://github.com/maurycyw/StaggeredGridView

它的工作正常,但...沒有OnScrollListener! 我不得不知道用戶什麼時候看到最後一個項目,以便加載更多。 但是,沒有OnScrollListener是不可能的!

你對我的問題有了解嗎?

非常感謝。

回答

1

UPDATE:
測試後,
調用notifyDataSetChanged()StaggeredGridView Adapter情況下滾動位置重置爲0(視圖的開始)。
而是使用PinterestLikeAdapterView


我有添加loadMoreListenerStaggeredGridView
步驟:
1-添加loadmore接口和booelan isLoading(在類的頂部)

public interface OnLoadMoreListener { 
    public boolean onLoadMore(); 
} 

private boolean mIsLoading; 
private OnLoadMoreListener mLoadMoreListener; 

2 - 找到trackMotionScroll功能,在deltaY > 0
和之後添加

if (overhang <= 80) { // 80 is how nearest to the end. ZERO mean the end of list 
    // scrolling down and almost reach the end of list 
    if (false == mIsLoading && null != mLoadMoreListener) { 
     if (false == mLoadMoreListener.onLoadMore()) { 
      mIsLoading = true; 
      Log.d(TAG, "loadMore"); 
     } 
    } 
} 

3-加setListener()loadComplated()功能

public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) { 
    this.mLoadMoreListener = onLoadMoreListener; 
} 

public void loadMoreCompleated() { 
    mIsLoading = false; 
} 

如何使用
1中定義loadMoreListenerActivity/Fragment

private StaggeredGridView.OnLoadMoreListener loadMoreListener = new StaggeredGridView.OnLoadMoreListener() { 

    @Override 
    public boolean onLoadMore() { 
     loading.setVisibility(View.VISIBLE); 
     // load more data from internet (not in the UI thread) 

     return true; // true if you have more data to load, false you dont have more data to load 
    } 
}; 

2 - 當你完成數據加載並將其添加到適配器調用

private void doneLoading() { 
    gridView.loadMoreCompleated(); 
    loading.setVisibility(View.GONE); 
} 
0

必須知道,當用戶看到的最後一個項目,以裝載更多。 但是,沒有OnScrollListener是不可能的!

爲什麼你會使用onScrollListener呢?相反,編寫一個擴展BaseAdapter的類(MyStaggeredGridAdapter,或許?)並將其應用於StaggeredGridView。然後,您可以使用適配器的getView()方法,該方法在需要呈現新視圖時將被調用。

0

要知道最後一個項目何時顯示在屏幕上,我使用Adapter的getView()方法。只需比較你的最後一個索引與「位置」參數。事情是這樣的

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ... 

     if (lastLoaded == position){ 
      loadMore(); 
     } 

     ... 
    } 

,也許,你想看到這一點:How to add data into bottom of Android StaggeredGridView and not go back top?,以避免從一開始加載列表,並從顯示的最後一項繼續。

我希望這可以幫助,不知道這是否是最好的解決辦法,但爲我工作;)

+0

你好@Hugo ...你的代碼中的最後一個加載是什麼... Plz告訴我..我也陷入了同樣的問題 –

1

試試這個,如果沒有適合你,然後換你的回收視圖滾動視圖,然後檢查滾動視圖已經觸底:

在XML文件中:

<ScrollView 
    android:id="@+id/scroll_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.RecyclerView 
       android:id="@+id/recycler_view" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scrollbars="vertical" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    </RelativeLayout> 

</ScrollView> 

在類文件:

private ScrollView mScrollView; 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    initScrollView(); 
    ... 
} 

private void initScrollView() { 
    mScrollView = (ScrollView) mView.findViewById(R.id.scroll_view); 
    mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { 
     @Override 
     public void onScrollChanged() { 
      if (mScrollView != null) { 
       if (mScrollView.getChildAt(0).getBottom() <= (mScrollView.getHeight() + mScrollView.getScrollY())) { 
        // Scroll view is at bottom 
        // If not loading then start load more 
       } else { 
        // Scroll view is not at bottom 
       } 
      } 
     } 
    }); 
} 

記住回收視圖的嵌套滾動設置爲false平滑滾動在類文件:

mRecyclerView.setNestedScrollingEnabled(false); 

如果你要開始加載到達bottomm之前下一個頁面,然後就從mScrollView減去一個整數。 getChildAt(0).getBottom()

例如1000被這裏減去:

if (mScrollView.getChildAt(0).getBottom() - 1000 <= (mScrollView.getHeight() + mScrollView.getScrollY())) { 
    // Scroll view is at bottom 
    // If not loading then start load more 
} else { 
    // Scroll view is not at bottom 
} 

使用更大的整數開始加載多之前滾動到達底部。