2016-03-07 9 views
8

私の画像ギャラリーにStaggeredGridLayoutManagerを使用しています。私はsetReverseLayout(true)を設定しました。画像が下から積み重なるようにしました。Android:StaggeredGridLayoutManager初期化中にスクロールする

私が直面している問題は、アプリケーションの初期化時に、スクロールがギャラリーの下部にあることです。ユーザーが最初にアプリを起動したときにスクロールがギャラリーの上部に表示されるようにしたいと考えています。

scrollToPosition(次のコードスニペット)を使用してみましたが、スクロールはギャラリーの途中で終了します。scrollToPositionの呼び出し時に画像が正しく読み込まれなかったためです。

mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); 
    mLayoutManager.setReverseLayout(true); 

mRecyclerView.setLayoutManager(mLayoutManager); 
mImageList = ((MainActivity)getActivity()).getImages(); 
adapter = new ImageAdapter(getActivity(),mImageList); 
mRecyclerView.setAdapter(adapter); 
mRecyclerView.scrollToPosition(mImageList.size()-1); 

スクロールを下に向けるのではなく上を向ける適切な方法はありますか?

答えて

2

私は前に同様の問題を解決し、ここでの方法である:

mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
       new ViewTreeObserver.OnGlobalLayoutListener() { 
        @SuppressWarnings("deprecation") 
        @Override 
        public void onGlobalLayout() { 
         ViewTreeObserver observer = mRecyclerView.getViewTreeObserver(); 
         scrollRecyclerViewToTop(); 
         if (!observer.isAlive()) { 
          return; 
         } 
         if (mScrolledByUser) { 
          if (hasJellyBeanApi()) { 
           observer.removeOnGlobalLayoutListener(this); 
          } else { 
           observer.removeGlobalOnLayoutListener(this); 
          } 
         } 
        } 
       }); 

そして:

そして:

private void scrollRecyclerViewToTop() { 
    mRecyclerView.scrollToPosition(0); 
    mRecyclerView.scrollBy(0, Integer.MIN_VALUE); 
} 

を意味は簡単です:聞かせてRecyclerView常にuまで上にスクロールそれに触れる。これがあなたを助けることを願っています。

+0

画面に触れると機能しますが、触れないと何も表示されません。私はデフォルトでそれをスクロールしたい、それはユーザーが触れる必要はありません。これは私があなたのために賞金を授与するので最も近い答えです。 btw私の答えは私が望むように動作します。 – uguboz

1

以前の方法(複数可)から得られた位置を使用してリサイクル業者ビューscrollToPositionを呼び出す findFirstCompletelyVisibleItemPositions

またはfindFirstVisibleItemPositions

を呼び出すことにより、最初に目に見える位置を見つける試み

+0

問題を完全に解決することはできません。このシステムを使用して、我々は にOnScrollListenerを拡張する当社独自のクラスを作成 ことで、ほとんどのユースケースをサポートしている基本的なEndlessScrollListenerを定義することができます。最初に表示される項目は、必ずしも最後の項目ではありません。だから私は別のランで異なる位置にスクロールすることに終わるかもしれません。 – Neo

0

はのscrollToPositionWithOffset()機能を使用してみてください代わりにStaggeredGridLayoutManagerscrollToPositionよりも信頼性が高いことがわかりました。また、Handler().post()の中でこの機能を実行してください。これにより、Runnableがメッセージキューに追加されます。 UIスレッドが解放されると実行されます。

new Handler().post(new Runnable() { 
      @Override 
      public void run() { 
       staggeredGridLayoutManager.scrollToPositionWithOffset(mImageList.size() - 1, 0); 
      } 
     }); 
+0

残念ながら、それは私のために働いていませんでした。 Thanskの答えは – uguboz

1

私はdataobserver内scrolltopositionを使用し、今では正常に動作します。..

recyclerview負荷、上部の項目は、以下のコードで表示されます:

rcAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { 
      @Override 
      public void onItemRangeInserted(int positionStart, int itemCount) { 
       super.onItemRangeInserted(positionStart, itemCount); 
       int count = rcAdapter.getItemCount(); 
       mRecyclerView.scrollToPosition(itemCount-1); 

      } 
     }); 

一番下までスクロールしますチャットビューのように新しく追加されたアイテムについては、以下のコードを使用してください。

 rcAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() { 
        @Override 
        public void onItemRangeInserted(int positionStart, int itemCount) { 
         super.onItemRangeInserted(positionStart, itemCount); 
         int count = rcAdapter.getItemCount(); 
         int lastVisiblePositions[] = new int[2]; //for 2 columns 
         int lastVisiblePosition = staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(lastVisiblePositions)[0]; 
         // If the recycler view is initially being loaded or the user is at the bottom of the list, scroll 
         // to the bottom of the list to show the newly added message. 
         if (lastVisiblePosition == -1 || 
          (positionStart >= (count- 1) && lastVisiblePosition == (positionStart - 1))) { 
          mRecyclerView.scrollToPosition(positionStart); 
         } 

        } 
       }); 
+0

興味深いですね。 – Neo

0

このコードは、私はまた、コード内でこのリンクを使用し、あなたを助けるかもしれ

(例えばListViewコントロールとGridViewのような)

https://guides.codepath.com/android/Endless-Scrolling-with-AdapterViews-and-RecyclerView

すべてAdapterViewはいつでもトリガされOnScrollListenerイベントへの結合 をサポートしていますa ユーザーがコレクションをスクロールします。

public abstract class EndlessScrollListener implements AbsListView.OnScrollListener { 
    // The minimum number 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 && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount) { 
     loading = onLoadMore(currentPage + 1, totalItemCount); 
     } 
    } 

    // Defines the process for actually loading more data based on page 
    // Returns true if more data is being loaded; returns false if there is no more data to load. 
    public abstract boolean onLoadMore(int page, int totalItemsCount); 

    @Override 
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // Don't take any action on changed 
    } 
    } 
関連する問題