Java Code Examples for android.support.v7.widget.StaggeredGridLayoutManager#findLastVisibleItemPositions()

The following examples show how to use android.support.v7.widget.StaggeredGridLayoutManager#findLastVisibleItemPositions() . 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: ViewUtils.java    From VideoMeeting with Apache License 2.0 6 votes vote down vote up
public static int getLastVisiblePosition(RecyclerView recyclerView) {
	RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
	int position;
       if (manager instanceof GridLayoutManager) {
           position = ((GridLayoutManager) manager).findLastVisibleItemPosition();
       } else if (manager instanceof LinearLayoutManager) {
           position = ((LinearLayoutManager) manager).findLastVisibleItemPosition();
       } else if (manager instanceof StaggeredGridLayoutManager) {
           StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) manager;
           int[] lastPositions = layoutManager.findLastVisibleItemPositions(new int[layoutManager.getSpanCount()]);
           position = getMaxPosition(lastPositions);
       } else {
           position = manager.getItemCount() - 1;
       }
       return position;
}
 
Example 2
Source File: LoadMoreRecyclerView.java    From LoadMoreRecyclerView with Apache License 2.0 6 votes vote down vote up
/**
 * 获取最后一条展示的位置
 *
 * @return
 */
private int getLastVisiblePosition() {
    int position;
    if (getLayoutManager() instanceof LinearLayoutManager) {
        position = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
    } else if (getLayoutManager() instanceof GridLayoutManager) {
        position = ((GridLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
    } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager();
        int[] lastPositions = layoutManager.findLastVisibleItemPositions(new int[layoutManager.getSpanCount()]);
        position = getMaxPosition(lastPositions);
    } else {
        position = getLayoutManager().getItemCount() - 1;
    }
    return position;
}
 
Example 3
Source File: CommonRecyclerView.java    From TigerVideo with Apache License 2.0 6 votes vote down vote up
/**
 * 获取最后一条展示的位置
 *
 * @return
 */
public int getLastVisiblePosition() {

    int position;
    if (getLayoutManager() instanceof LinearLayoutManager) {
        position = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
    } else if (getLayoutManager() instanceof GridLayoutManager) {
        position = ((GridLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
    } else if (getLayoutManager() instanceof StaggeredGridLayoutManager) {
        StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) getLayoutManager();
        int[] lastPositions = layoutManager.findLastVisibleItemPositions(new int[layoutManager.getSpanCount()]);
        position = getMaxPosition(lastPositions);
    } else {
        position = getLayoutManager().getItemCount() - 1;
    }
    return position;
}
 
Example 4
Source File: OnRecyclerViewScrollListener.java    From HeaderFooterRecyclerView with Apache License 2.0 6 votes vote down vote up
/**
 * 计算第一个元素的位置
 */
private int calculateFirstVisibleItemPos(RecyclerView.LayoutManager layoutManager, int firstVisibleItemPosition) {
    switch (mLayoutManagerType) {
        case LINEAR:
            mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
            break;
        case GRID:
            mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
            break;
        case STAGGERED_GRID:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (mLastPositions == null) {
                mLastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            mLastPositions = staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPositions);
            mLastVisibleItemPosition = findMax(mLastPositions);
            staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(mLastPositions);
            firstVisibleItemPosition = findMax(mLastPositions);
            break;
    }
    return firstVisibleItemPosition;
}
 
Example 5
Source File: BasePullUpRecyclerAdapter.java    From RecyclerViewDemo with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        layoutManagerType = LINEAR;
    } else if (layoutManager instanceof GridLayoutManager) {
        layoutManagerType = GRID;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        layoutManagerType = STAGGERED_GRID;
    } else {
        throw new RuntimeException(
                "Unsupported LayoutManager used. Valid ones are " +
                        "LinearLayoutManager, GridLayoutManager and " +
                        "StaggeredGridLayoutManager");
    }

    switch (layoutManagerType) {
        case LINEAR:
            lastVisibleItem = ((LinearLayoutManager) layoutManager)
                    .findLastVisibleItemPosition();
            break;
        case GRID:
            lastVisibleItem = ((GridLayoutManager) layoutManager)
                    .findLastVisibleItemPosition();
            break;
        case STAGGERED_GRID:
            StaggeredGridLayoutManager staggeredGridLayoutManager
                    = (StaggeredGridLayoutManager) layoutManager;
            lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItem = findMax(lastPositions);
            break;
    }
}
 
Example 6
Source File: EndlessRecyclerOnScrollListener.java    From HeaderFooterRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    if (layoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LayoutManagerType.LinearLayout;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LayoutManagerType.GridLayout;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LayoutManagerType.StaggeredGridLayout;
        } else {
            throw new RuntimeException(
                    "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }

    switch (layoutManagerType) {
        case LinearLayout:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GridLayout:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case StaggeredGridLayout:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            break;
    }
}
 
Example 7
Source File: EndlessRecyclerOnScrollListener.java    From HeaderAndFooterRecyclerView with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    if (layoutManagerType == null) {
        if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LayoutManagerType.GridLayout;
        } else if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LayoutManagerType.LinearLayout;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LayoutManagerType.StaggeredGridLayout;
        } else {
            throw new RuntimeException(
                    "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }

    switch (layoutManagerType) {
        case LinearLayout:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GridLayout:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case StaggeredGridLayout:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            break;
    }
}
 
Example 8
Source File: DRecyclerViewScrollListener.java    From Pas with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    if (layoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LayoutManagerType.LinearLayout;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LayoutManagerType.GridLayout;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LayoutManagerType.StaggeredGridLayout;
        } else {
            throw new RuntimeException(
                    "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }

    switch (layoutManagerType) {
        case LinearLayout:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GridLayout:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case StaggeredGridLayout:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            break;
    }


}
 
Example 9
Source File: BasePullUpRecyclerAdapter.java    From CoreModule with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (layoutManager instanceof LinearLayoutManager) {
        layoutManagerType = LINEAR;
    } else if (layoutManager instanceof GridLayoutManager) {
        layoutManagerType = GRID;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        layoutManagerType = STAGGERED_GRID;
    } else {
        throw new RuntimeException(
                "Unsupported LayoutManager used. Valid ones are " +
                        "LinearLayoutManager, GridLayoutManager and " +
                        "StaggeredGridLayoutManager");
    }

    switch (layoutManagerType) {
        case LINEAR:
            lastVisibleItem = ((LinearLayoutManager) layoutManager)
                    .findLastVisibleItemPosition();
            break;
        case GRID:
            lastVisibleItem = ((GridLayoutManager) layoutManager)
                    .findLastVisibleItemPosition();
            break;
        case STAGGERED_GRID:
            StaggeredGridLayoutManager staggeredGridLayoutManager
                    = (StaggeredGridLayoutManager) layoutManager;
            lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItem = findMax(lastPositions);
            break;
    }
}
 
Example 10
Source File: EndlessScrollListener.java    From photosearcher with Apache License 2.0 5 votes vote down vote up
public boolean shouldLoadNext(@NonNull StaggeredGridLayoutManager manager) {
    int lastItemPosition = manager.getItemCount() - 1;
    int[] lastVisibleItemPositions = manager.findLastVisibleItemPositions(null);

    if (lastVisibleItemPositions.length == 0) {
        return true;
    }

    int lastVisibleItemPosition = lastVisibleItemPositions[lastVisibleItemPositions.length - 1];

    return (lastItemPosition < lastVisibleItemPosition + getLoadingTriggerOffsetSize());
}
 
Example 11
Source File: XRecyclerView.java    From ARecyclerView with MIT License 5 votes vote down vote up
private int getFirstVisibleItemPosition(RecyclerView.LayoutManager layoutManager) {
    int firstVisibleItemPosition = -1;
    if (layoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LayoutManagerType.LINEAR;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LayoutManagerType.GRID;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LayoutManagerType.STAGGERED_GRID;
        } else {
            throw new RuntimeException("Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }

    switch (layoutManagerType) {
        case LINEAR:
            firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
            break;
        case GRID:
            firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
            break;
        case STAGGERED_GRID:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastScrollPositions == null)
                lastScrollPositions = new int[staggeredGridLayoutManager.getSpanCount()];

            staggeredGridLayoutManager.findLastVisibleItemPositions(firstScrollPositions);
            firstVisibleItemPosition = findMin(firstScrollPositions);
            break;
    }
    return firstVisibleItemPosition;
}
 
Example 12
Source File: EasyRefreshLayout.java    From EasyRefreshLayout with Apache License 2.0 5 votes vote down vote up
/**
 * 最后一个的位置
 */
private int getLastVisiBleItem() {
    RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
    int lastVisibleItemPosition = -1;

    int layoutManagerType = 0;
    if (layoutManager instanceof GridLayoutManager) {
        layoutManagerType = 1;
    } else if (layoutManager instanceof LinearLayoutManager) {
        layoutManagerType = 0;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
        layoutManagerType = 2;
    } else {
        throw new RuntimeException(
                "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
    }

    switch (layoutManagerType) {
        case 0:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case 1:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case 2:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            int[] lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            break;
    }
    return lastVisibleItemPosition;
}
 
Example 13
Source File: EndlessRecyclerOnScrollListener.java    From CoordinatorLayoutExample with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    if (layoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LayoutManagerType.LinearLayout;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LayoutManagerType.GridLayout;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LayoutManagerType.StaggeredGridLayout;
        } else {
            throw new RuntimeException(
                    "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }

    switch (layoutManagerType) {
        case LinearLayout:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GridLayout:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case StaggeredGridLayout:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            break;
    }

}
 
Example 14
Source File: LoadMoreRecyclerView.java    From ZhiHu-TopAnswer with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(int dx, int dy) {
    super.onScrolled(dx, dy);

    LayoutManager layoutManager = getLayoutManager(); //拿到layoutmanager用来判断类型,拿到最后一个可见的view
    if (layoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
        } else {
            throw new RuntimeException(
                    "不支持的LayoutManager ,目前只支持 LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }
    /**
     * 拿到最后一个可见的view
     */
    switch (layoutManagerType) {
        case LINEAR:
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GRID:
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case STAGGERED_GRID:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            break;
    }
}
 
Example 15
Source File: RecyclerViewScrollListener.java    From Social with Apache License 2.0 4 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

    Log.d(TAG, "onScrolled: dy = " + dy);

    /**
     * 获取布局参数
     */
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();

    //如果为null,第一次运行,确定布局类型
    if (mLayoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            mLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT;
        } else if (layoutManager instanceof GridLayoutManager) {
            mLayoutManagerType = LayoutManagerType.GRID_LAYOUT;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            mLayoutManagerType = LayoutManagerType.STAGGERED_GRID_LAYOUT;
        } else {
            throw new RuntimeException("LayoutManager should be LinearLayoutManager,GridLayoutManager,StaggeredGridLayoutManager");
        }
    }

    //对于不太能够的布局参数,不同的方法获取到当前显示的最后一个条目数
    switch (mLayoutManagerType) {
        case LINEAR_LAYOUT:
            mLastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GRID_LAYOUT:
            mLastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            int firstItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
            Log.d(TAG, "onScrolled: firstPosition = " + firstItemPosition);
            break;
        case STAGGERED_GRID_LAYOUT:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (mLastPostions == null) {
                mLastPostions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(mLastPostions);
            mLastVisibleItemPosition = findMax(mLastPostions);
            break;
        default:
            break;
    }

}
 
Example 16
Source File: WXRecyclerViewOnScrollListener.java    From weex-uikit with MIT License 4 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  super.onScrolled(recyclerView, dx, dy);
  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  listener.get().onBeforeScroll(dx,dy);
  //  int lastVisibleItemPosition = -1;
  if (layoutManagerType == null) {
    if (layoutManager instanceof LinearLayoutManager) {
      layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
    } else if (layoutManager instanceof GridLayoutManager) {
      layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
    } else {
      throw new RuntimeException(
          "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
    }
  }

  switch (layoutManagerType) {
    case LINEAR:
      lastVisibleItemPosition = ((LinearLayoutManager) layoutManager)
          .findLastVisibleItemPosition();
      listener.get().notifyAppearStateChange(((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition()
          ,lastVisibleItemPosition
          ,dx
          ,dy);
      break;
    case GRID:
      lastVisibleItemPosition = ((GridLayoutManager) layoutManager)
          .findLastVisibleItemPosition();
      break;
    case STAGGERED_GRID:
      StaggeredGridLayoutManager staggeredGridLayoutManager
          = (StaggeredGridLayoutManager) layoutManager;
      if (lastPositions == null) {
        lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
      }
      staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
      lastVisibleItemPosition = findMax(lastPositions);
      break;
  }
}
 
Example 17
Source File: LuRecyclerView.java    From LRecyclerView with Apache License 2.0 4 votes vote down vote up
@Override
public void onScrolled(int dx, int dy) {
    super.onScrolled(dx, dy);

    int firstVisibleItemPosition = 0;
    RecyclerView.LayoutManager layoutManager = getLayoutManager();

    if (layoutManagerType == null) {
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LayoutManagerType.LinearLayout;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LayoutManagerType.GridLayout;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LayoutManagerType.StaggeredGridLayout;
        } else {
            throw new RuntimeException(
                    "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }
    }

    switch (layoutManagerType) {
        case LinearLayout:
            firstVisibleItemPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case GridLayout:
            firstVisibleItemPosition = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
            break;
        case StaggeredGridLayout:
            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
            if (lastPositions == null) {
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
            }
            staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
            lastVisibleItemPosition = findMax(lastPositions);
            staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(lastPositions);
            firstVisibleItemPosition = findMax(lastPositions);
            break;
        default:
            break;
    }

    // 根据类型来计算出第一个可见的item的位置,由此判断是否触发到底部的监听器
    // 计算并判断当前是向上滑动还是向下滑动
    calculateScrollUpOrDown(firstVisibleItemPosition, dy);
    // 移动距离超过一定的范围,我们监听就没有啥实际的意义了
    mScrolledXDistance += dx;
    mScrolledYDistance += dy;
    mScrolledXDistance = (mScrolledXDistance < 0) ? 0 : mScrolledXDistance;
    mScrolledYDistance = (mScrolledYDistance < 0) ? 0 : mScrolledYDistance;
    if (mIsScrollDown && (dy == 0)) {
        mScrolledYDistance = 0;
    }
    //Be careful in here
    if (null != mLScrollListener) {
        mLScrollListener.onScrolled(mScrolledXDistance, mScrolledYDistance);
    }

    //如果想要滑动到底部自动加载更多,mIsManualLoadMore必须为false
    if (mIsManualLoadMore) {
        if (!isNoMore) {
            Log.e("lzx","onScrooo set visible");
            mLoadingData = true;
            mLoadMoreFooter.setOnClickLoadMoreListener(mLoadMoreListener);
        }

    } else {
        if (mLoadMoreListener != null && mLoadMoreEnabled) {
            int visibleItemCount = layoutManager.getChildCount();
            int totalItemCount = layoutManager.getItemCount();
            if (visibleItemCount > 0
                    && lastVisibleItemPosition >= totalItemCount - 1
                    && (isCritical ? totalItemCount >= visibleItemCount : totalItemCount > visibleItemCount)
                    && !isNoMore
                    && !mRefreshing) {

                mFootView.setVisibility(View.VISIBLE);
                if (!mLoadingData) {
                    mLoadingData = true;
                    mLoadMoreFooter.onLoading();
                    mLoadMoreListener.onLoadMore();
                }

            }

        }
    }

}
 
Example 18
Source File: RecyclerViewLoadHelper.java    From GankMeizhi with Apache License 2.0 4 votes vote down vote up
public static boolean isToBottom(RecyclerView recyclerView) {
    int lastVisibleItemPosition = -1;
    int[] lastPositions;
    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
    if (null != layoutManager) {
        LAYOUT_MANAGER_TYPE layoutManagerType;
        if (layoutManager instanceof LinearLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
        } else if (layoutManager instanceof GridLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
        } else {
            throw new ClassCastException(
                    "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
        }

        switch (layoutManagerType) {
            case LINEAR:
                lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                break;
            case GRID:
                lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
                break;
            case STAGGERED_GRID:
                StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
                lastVisibleItemPosition = findMax(lastPositions);

                break;
        }

        int visibleItemCount = layoutManager.getChildCount();
        int totalItemCount = layoutManager.getItemCount();
        if ((visibleItemCount > 0 && (lastVisibleItemPosition) >= totalItemCount - 1)) {
            //到达最后
            return true;
        }

    }
    return false;
}
 
Example 19
Source File: WXRecyclerViewOnScrollListener.java    From weex with Apache License 2.0 4 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  super.onScrolled(recyclerView, dx, dy);
  RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
  //  int lastVisibleItemPosition = -1;
  if (layoutManagerType == null) {
    if (layoutManager instanceof LinearLayoutManager) {
      layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
    } else if (layoutManager instanceof GridLayoutManager) {
      layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
      layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
    } else {
      throw new RuntimeException(
          "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
    }
  }

  switch (layoutManagerType) {
    case LINEAR:
      lastVisibleItemPosition = ((LinearLayoutManager) layoutManager)
          .findLastVisibleItemPosition();
      listener.get().notifyAppearStateChange(((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition()
          ,lastVisibleItemPosition
          ,dx
          ,dy);
      break;
    case GRID:
      lastVisibleItemPosition = ((GridLayoutManager) layoutManager)
          .findLastVisibleItemPosition();
      break;
    case STAGGERED_GRID:
      StaggeredGridLayoutManager staggeredGridLayoutManager
          = (StaggeredGridLayoutManager) layoutManager;
      if (lastPositions == null) {
        lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
      }
      staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
      lastVisibleItemPosition = findMax(lastPositions);
      break;
  }
}
 
Example 20
Source File: OnRVLoadMoreScrollListener.java    From RecyclerViewAdapter with Apache License 2.0 4 votes vote down vote up
@Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        //  int lastVisibleItemPosition = -1;
        if (layoutManagerType == null) {
            if (layoutManager instanceof LinearLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
            } else if (layoutManager instanceof GridLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
            } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
            } else {
                throw new RuntimeException(
                        "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
            }
        }

        switch (layoutManagerType) {
            case LINEAR:
                lastVisibleItemPosition = ((LinearLayoutManager) layoutManager)
                        .findLastVisibleItemPosition();
                break;
            case GRID:
                lastVisibleItemPosition = ((GridLayoutManager) layoutManager)
                        .findLastVisibleItemPosition();
                break;
            case STAGGERED_GRID:
                StaggeredGridLayoutManager staggeredGridLayoutManager
                        = (StaggeredGridLayoutManager) layoutManager;
                if (lastPositions == null) {
                    lastPositions = new int[staggeredGridLayoutManager.getSpanCount()];
                }
                staggeredGridLayoutManager.findLastVisibleItemPositions(lastPositions);
                lastVisibleItemPosition = findMax(lastPositions);
                break;
        }
//        if (!recyclerView.canScrollVertically(-1)) {
//            onScrolledToTop();
//        } else if (!recyclerView.canScrollVertically(1)) {
//            onScrolledToBottom();
//        } else if (dy < 0) {
//            onScrolledUp();
//        } else if (dy > 0) {
//            onScrolledDown();
//        }


    }