java getitemcount_Java LayoutManager.getItemCount方法代码示例

import android.support.v7.widget.RecyclerView.LayoutManager; //导入方法依赖的package包/类

/***

* Well composition does not work so we copy this method from {@link LinearSnapHelper}.

* That sucks!

*

* @param layoutManager

* @param velocityX

* @param velocityY

* @return

*/

@Override

public int findTargetSnapPosition(final LayoutManager layoutManager, final int velocityX, final int velocityY) {

if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {

return RecyclerView.NO_POSITION;

}

final int itemCount = layoutManager.getItemCount();

if (itemCount == 0) {

return RecyclerView.NO_POSITION;

}

final View currentView = findSnapView(layoutManager);

if (currentView == null) {

return RecyclerView.NO_POSITION;

}

final int currentPosition = layoutManager.getPosition(currentView);

if (currentPosition == RecyclerView.NO_POSITION) {

return RecyclerView.NO_POSITION;

}

RecyclerView.SmoothScroller.ScrollVectorProvider vectorProvider =

(RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager;

// deltaJumps sign comes from the velocity which may not match the order of children in

// the LayoutManager. To overcome this, we ask for a vector from the LayoutManager to

// get the direction.

PointF vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1);

if (vectorForEnd == null) {

// cannot get a vector for the given position.

return RecyclerView.NO_POSITION;

}

int vDeltaJump, hDeltaJump;

if (layoutManager.canScrollHorizontally()) {

hDeltaJump = estimateNextPositionDiffForFling(layoutManager,

getHorizontalHelper(layoutManager), velocityX, 0);

if (vectorForEnd.x < 0) {

hDeltaJump = -hDeltaJump;

}

} else {

hDeltaJump = 0;

}

if (layoutManager.canScrollVertically()) {

vDeltaJump = estimateNextPositionDiffForFling(layoutManager,

getVerticalHelper(layoutManager), 0, velocityY);

if (vectorForEnd.y < 0) {

vDeltaJump = -vDeltaJump;

}

} else {

vDeltaJump = 0;

}

int deltaJump = layoutManager.canScrollVertically() ? vDeltaJump : hDeltaJump;

if (deltaJump == 0) {

return RecyclerView.NO_POSITION;

}

int targetPos = currentPosition + deltaJump;

if (targetPos < 0) {

targetPos = 0;

}

if (targetPos >= itemCount) {

targetPos = itemCount - 1;

}

return targetPos;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值