Android列表条目跳转,Android RrecyclerView条目跳转到指定位置

效果图如下:侧滑点击第几张     主界面显示第几章

921865015630

921865015630

工具类:

public class MoveToPosition {

//目标项是否在最后一个可见项之后

private static boolean mShouldScroll;

//记录目标项位置

private static int mToPosition;

/**

* RecyclerView 移动到当前位置, 不带动画效果

* @param manager  设置RecyclerView对应的manager

* @param mRecyclerView  当前的RecyclerView

* @param n  要跳转的位置

*/

public static void MoveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, int n) {

int firstItem = manager.findFirstVisibleItemPosition();

int lastItem = manager.findLastVisibleItemPosition();

if (n <= firstItem) {

mRecyclerView.scrollToPosition(n);

}else if (n <= lastItem) {

int top = mRecyclerView.getChildAt(n - firstItem).getTop();

mRecyclerView.scrollBy(0, top);

}else {

//            mRecyclerView.scrollToPosition(n);

manager.scrollToPositionWithOffset(n,0);

}

}

/**

* 滑动到指定位置  带动画效果

*/

public static void smoothMoveToPosition(LinearLayoutManager manager, RecyclerView mRecyclerView, final int position) {

// 第一个可见位置

int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));

// 最后一个可见位置

int lastItem =    mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() -1));

if (position < firstItem) {

// 第一种可能:跳转位置在第一个可见位置之前

mRecyclerView.scrollToPosition(position);

}else if (position <= lastItem) {

// 第二种可能:跳转位置在第一个可见位置之后

int movePosition = position - firstItem;

if (movePosition >=0 && movePosition < mRecyclerView.getChildCount()) {

int top = mRecyclerView.getChildAt(movePosition).getTop();

mRecyclerView.scrollBy(0, top);

}

}else {

// 第三种可能:跳转位置在最后可见项之后

//            mRecyclerView.smoothScrollToPosition(position);

//            mToPosition = position;

//            mShouldScroll = true;

//            mRecyclerView.scrollToPosition(position);

manager.scrollToPositionWithOffset(position,0);

mToPosition = position;

mShouldScroll =true;

}

}

}

调用方法:

titleAdapter.setOnItemClickLitener(new TitleAdapter1.OnItemClickLitener() {

@Override

public void onItemClick(View view, int position) {

drawerlayout.closeDrawers();

MoveToPosition.smoothMoveToPosition(ContentLinearLayoutManager, recyclerView, position);

}

});

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值