Java Code Examples for android.support.v7.widget.LinearLayoutManager#findLastCompletelyVisibleItemPosition()

The following examples show how to use android.support.v7.widget.LinearLayoutManager#findLastCompletelyVisibleItemPosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PullToZoomViewEx.java    From Ticket-Analysis with MIT License 6 votes vote down vote up
public boolean isLastItemVisible() {
    if (mRootView != null) {
        final RecyclerView.Adapter adapter = mRootView.getAdapter();
        LinearLayoutManager layoutManager = (LinearLayoutManager) mRootView.getLayoutManager();

        if (adapter == null || adapter.getItemCount() == 0) {
            LogUtil.d(TAG, "空数据");
            return true;
        } else {
            if (layoutManager != null) {
                int lastPosition = layoutManager.findLastCompletelyVisibleItemPosition();
                if (lastPosition == adapter.getItemCount() - 1 && lastPosition > 5) {
                    return true;
                } else {
                    return false;
                }
            }
            return false;
        }
    }
    return false;
}
 
Example 2
Source File: StartSnapHelper.java    From SimpleRecyclerView with Apache License 2.0 6 votes vote down vote up
private View getStartView(RecyclerView.LayoutManager layoutManager,
                          OrientationHelper helper) {
  if (!(layoutManager instanceof LinearLayoutManager)) {
    return super.findSnapView(layoutManager);
  }

  LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;

  int firstChild = linearLayoutManager.findFirstVisibleItemPosition();

  boolean isLastItem = linearLayoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1;

  if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
    return null;
  }

  View child = layoutManager.findViewByPosition(firstChild);

  if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2
    && helper.getDecoratedEnd(child) > 0) {
    return child;
  } else {
    return layoutManager.findViewByPosition(firstChild + 1);
  }
}
 
Example 3
Source File: OnLoadMoreListener.java    From MiPushFramework with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
        layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

        itemCount = layoutManager.getItemCount();
        lastPosition = layoutManager.findLastCompletelyVisibleItemPosition();
    } else {
        Log.e("OnLoadMoreListener", "The OnLoadMoreListener only support LinearLayoutManager");
        return;
    }

    if (lastItemCount != itemCount && (lastPosition > itemCount - 3)) {
        lastItemCount = itemCount;
        this.onLoadMore();
    }
}
 
Example 4
Source File: LoadMoreRecyclerView.java    From ZhihuDaily with Apache License 2.0 6 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    LoadMoreRecyclerView view = (LoadMoreRecyclerView) recyclerView;
    onLoadMoreListener onLoadMoreListener = view.getOnLoadMoreListener();

    onLoadMoreListener.onScrolled(recyclerView, dx, dy);

    //if scroll to bottom
    LinearLayoutManager layoutManager = (LinearLayoutManager) view.getLayoutManager();
    int lastVisibleItem = layoutManager.findLastCompletelyVisibleItemPosition();
    int itemCount = layoutManager.getItemCount();
    if (lastVisibleItem >= itemCount - 1 && !view.getLoadingMore()) {
        onLoadMoreListener.onLoadMore();
        L.i(TAG, "load more: lastVisibleItem = " + lastVisibleItem + ", itemCount " + itemCount);
    } else {
        super.onScrolled(recyclerView, dx, dy);
    }
}
 
Example 5
Source File: PostListFragment.java    From zhizhihu with Apache License 2.0 6 votes vote down vote up
private RecyclerView.OnScrollListener getScrollToBottomListener(final LinearLayoutManager linearLayoutManager) {
    return new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            int lastItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
            int count = mPostListAdapter.getItemCount() - 1;
            boolean isBottom = (lastItem == count);
            if (!mSwipeRefreshLayout.isRefreshing() && isBottom) {
                if (!mIsFirstTimeTouchBottom) {
                    Log.e("onScrolled=>", "refresh.... ");
                    mPresenter.loadNextPage();
                } else {
                    mIsFirstTimeTouchBottom = false;
                }
            }
        }
    };
}
 
Example 6
Source File: OnLoadMoreListener.java    From Toutiao with Apache License 2.0 6 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
        layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

        itemCount = layoutManager.getItemCount();
        lastPosition = layoutManager.findLastCompletelyVisibleItemPosition();
    } else {
        Log.e("OnLoadMoreListener", "The OnLoadMoreListener only support LinearLayoutManager");
        return;
    }

    if (lastItemCount != itemCount && lastPosition == itemCount - 1) {
        lastItemCount = itemCount;
        this.onLoadMore();
    }
}
 
Example 7
Source File: LoadMoreDelegate.java    From AndroidUiKit with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
    if (dy < 0 || loadMoreSubject == null || loadMoreSubject.isLoading()){
        return;
    }

    int lastItem = 0;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    int totalItemCount = layoutManager.getItemCount();
    if (layoutManager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = ((GridLayoutManager) layoutManager);
        //Position to find the final item of the current LayoutManager
        lastItem = gridLayoutManager.findLastCompletelyVisibleItemPosition();
        if (lastItem == -1){
            lastItem = gridLayoutManager.findLastVisibleItemPosition();
        }
    } else if (layoutManager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) layoutManager);
        lastItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
        if (lastItem == -1){
            lastItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGridLayoutManager = ((StaggeredGridLayoutManager) layoutManager);
        // since may lead to the final item has more than one StaggeredGridLayoutManager the particularity of the so here that is an array
        // this array into an array of position and then take the maximum value that is the last show the position value
        int[] lastPositions = staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(null);
        lastItem = findMax(lastPositions);
    }
    final int childCount = layoutManager.getChildCount();
    final boolean shouldLoadMore = (childCount > 0 && lastItem >= totalItemCount - VISIBLE_THRESHOLD);
    if (shouldLoadMore) {
        loadMoreSubject.onLoadMore();
    }
}
 
Example 8
Source File: Utils.java    From ht-refreshrecyclerview with MIT License 5 votes vote down vote up
/**
 * 获取最后一个可见/完全可见item的位置索引
 * @param layoutManager       RecyclerView.LayoutManager对象
 * @param isCompletelyVisible 是否完全可见
 * @return item的索引
 */
public static int getLastVisibleItemPosition(@NonNull RecyclerView.LayoutManager layoutManager, boolean isCompletelyVisible) {
    int lastVisibleItemPosition = -1;
    if (layoutManager instanceof LinearLayoutManager) {//GridLayoutManager继承LinearLayoutManager
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
        lastVisibleItemPosition = isCompletelyVisible ? linearLayoutManager.findLastCompletelyVisibleItemPosition() : linearLayoutManager.findLastVisibleItemPosition();
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
        int[] lastVisibleItemPositions = isCompletelyVisible ? staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(null) : staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(null);
        if (lastVisibleItemPositions != null && lastVisibleItemPositions.length > 0)
            lastVisibleItemPosition = findMax(lastVisibleItemPositions);
    }
    return lastVisibleItemPosition;
}
 
Example 9
Source File: LMRecyclerView.java    From gank with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void onScrollStateChanged(int state) {
       /* LinearLayoutManager mLayoutManager = (LinearLayoutManager) getLayoutManager();
        int lastVisibleItemPosition = 0;
        int totalItemCount = 0;
        if (mLayoutManager instanceof LinearLayoutManager) {
            if (state == RecyclerView.SCROLL_STATE_IDLE) {
                lastVisibleItemPosition = mLayoutManager.findLastCompletelyVisibleItemPosition();
                totalItemCount = mLayoutManager.getItemCount();
                if (lastVisibleItemPosition == (totalItemCount - 1) && isScrollingToBottom) {
                    if (listener != null)
                        listener.loadMore();
                }
            }

        } else if (mLayoutManager instanceof GridLayoutManager) {
            if (state == RecyclerView.SCROLL_STATE_IDLE) {
                lastVisibleItemPosition = mLayoutManager.findLastCompletelyVisibleItemPosition();
                totalItemCount = mLayoutManager.getItemCount();
                if (lastVisibleItemPosition == (totalItemCount - 1) && isScrollingToBottom) {
                    if (listener != null)
                        listener.loadMore();
                }
            }
        }
*/
       LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
        if (state == RecyclerView.SCROLL_STATE_IDLE) {
            int lastVisibleItem = layoutManager.findLastCompletelyVisibleItemPosition();
            int totalItemCount = layoutManager.getItemCount();
            if (lastVisibleItem == (totalItemCount - 1) && isScrollingToBottom) {
                if (listener != null)
                    listener.loadMore();
            }
        }
    }
 
Example 10
Source File: BounceTouchListener.java    From Bounce with Apache License 2.0 5 votes vote down vote up
private boolean hasHitBottom() {
    if (mMainView instanceof ScrollView) {
        ScrollView scrollView = (ScrollView) mMainView;
        View view = scrollView.getChildAt(scrollView.getChildCount() - 1);
        int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));// Calculate the scrolldiff
        return diff == 0;
    } else if (mMainView instanceof ListView) {
        ListView listView = (ListView) mMainView;
        if (listView.getAdapter() != null) {
            if (listView.getAdapter().getCount() > 0) {
                return listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1 &&
                        listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight();
            }
        }
    } else if (mMainView instanceof RecyclerView) {
        RecyclerView recyclerView = (RecyclerView) mMainView;
        if (recyclerView.getAdapter() != null && recyclerView.getLayoutManager() != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter.getItemCount() > 0) {
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                    return linearLayoutManager.findLastCompletelyVisibleItemPosition() == adapter.getItemCount() - 1;
                } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                    int[] checks = staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(null);
                    for (int check : checks) {
                        if (check == adapter.getItemCount() - 1)
                            return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example 11
Source File: GravitySnapHelper.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
private boolean isAtEdgeOfList(LinearLayoutManager lm) {
    if ((!lm.getReverseLayout() && gravity == Gravity.START)
            || (lm.getReverseLayout() && gravity == Gravity.END)
            || (!lm.getReverseLayout() && gravity == Gravity.TOP)
            || (lm.getReverseLayout() && gravity == Gravity.BOTTOM)) {
        return lm.findLastCompletelyVisibleItemPosition() == lm.getItemCount() - 1;
    } else if (gravity == Gravity.CENTER) {
        return lm.findFirstCompletelyVisibleItemPosition() == 0
                || lm.findLastCompletelyVisibleItemPosition() == lm.getItemCount() - 1;
    } else {
        return lm.findFirstCompletelyVisibleItemPosition() == 0;
    }
}
 
Example 12
Source File: LMRecyclerView.java    From Gank.io with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onScrollStateChanged(int state) {
  LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
  if (state == RecyclerView.SCROLL_STATE_IDLE) {
    int lastVisibleItem = layoutManager.findLastCompletelyVisibleItemPosition();
    int totalItemCount = layoutManager.getItemCount();
    if (lastVisibleItem == (totalItemCount - 1) && isScrollingToBottom) {
      if (listener != null)
        listener.loadMore();
    }
  }
}
 
Example 13
Source File: AppDetailsRecyclerViewAdapter.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
private void toggleExpanded() {
    if (busyIndicator.getVisibility() == View.VISIBLE) {
        // Don't allow collapsing the view when the busy indicator
        // is shown because the APK is being downloaded and it's quite important
        return;
    }

    boolean expand = !versionsExpandTracker.get(apk.apkName);
    expand(expand);

    if (expand) {
        // Scroll the versions view to a correct position so it can show the whole item
        final LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
        final int currentPosition = getAdapterPosition();
        if (currentPosition >= lm.findLastCompletelyVisibleItemPosition()) {
            // Do it only if the item is near the bottom of current viewport
            recyclerView.getViewTreeObserver()
                    .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            // Expanded item dimensions should be already calculated at this moment
                            // so it's possible to correctly scroll to a given position
                            recyclerView.smoothScrollToPosition(currentPosition);
                            recyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        }
                    });
        }
    }
}
 
Example 14
Source File: MessageListPanelEx.java    From NIM_Android_UIKit with MIT License 4 votes vote down vote up
private boolean isLastMessageVisible() {
    LinearLayoutManager layoutManager = (LinearLayoutManager) messageListView.getLayoutManager();
    int lastVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();
    return lastVisiblePosition >= adapter.getBottomDataPosition();
}
 
Example 15
Source File: ChatRoomMsgListPanel.java    From NIM_Android_UIKit with MIT License 4 votes vote down vote up
private boolean isLastMessageVisible() {
    LinearLayoutManager layoutManager = (LinearLayoutManager) messageListView.getLayoutManager();
    int lastVisiblePosition = layoutManager.findLastCompletelyVisibleItemPosition();
    return lastVisiblePosition >= adapter.getBottomDataPosition();
}