android 置顶窗口位置,RecyclerView选中项居中(横向、竖向),指定位置置顶(竖向)...

需要实现对LinearLayoutManager的重写

中间显示类

import android.content.Context;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.LinearSmoothScroller;

import android.support.v7.widget.RecyclerView;

import android.util.DisplayMetrics;

public class CenterLayoutManager extends LinearLayoutManager {

static int lastPositon = 0;

static int targetPosion = 0;

public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {

super(context, orientation, reverseLayout);

}

@Override

public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {

CenterSmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());

smoothScroller.setTargetPosition(position);

startSmoothScroll(smoothScroller);

}

public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int lastpositon, int position) {

this.lastPositon = lastpositon;

this.targetPosion = position;

smoothScrollToPosition(recyclerView, state, position);

}

public static class CenterSmoothScroller extends LinearSmoothScroller {

private static float duration = 800f;//显示到中间的动画时长

public CenterSmoothScroller(Context context) {

super(context);

}

@Override

public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {

return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);

}

@Override

protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {

float newDuration = duration / (Math.abs(targetPosion - lastPositon));//重新计算相近两个位置的滚动间隔

return newDuration / displayMetrics.densityDpi;

}

@Override

protected int calculateTimeForScrolling(int dx) {

return super.calculateTimeForScrolling(dx);

}

}

}

置顶显示类

import android.content.Context;

import android.graphics.PointF;

import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.LinearSmoothScroller;

import android.support.v7.widget.RecyclerView;

public class TopLinearLayoutManager extends LinearLayoutManager {

public TopLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {

super(context, orientation, reverseLayout);

}

@Override

public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,

int position) {

RecyclerView.SmoothScroller smoothScroller = new TopSnappedSmoothScroller(recyclerView.getContext());

smoothScroller.setTargetPosition(position);

startSmoothScroll(smoothScroller);

}

private class TopSnappedSmoothScroller extends LinearSmoothScroller {

public TopSnappedSmoothScroller(Context context) {

super(context);

}

@Override

public PointF computeScrollVectorForPosition(int targetPosition) {

return TopLinearLayoutManager.this

.computeScrollVectorForPosition(targetPosition);

}

@Override

protected int getVerticalSnapPreference() {

return SNAP_TO_START;

}

}

}

调用方法,按需采用

//初始化置顶

TopLinearLayoutManager topLinearLayoutManager = new TopLinearLayoutManager(getContext(),

LinearLayoutManager.VERTICAL, false);

//初始化中间竖向

CenterLayoutManager centerLayoutManager = new CenterLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

//初始化中间横向

CenterLayoutManager centerLayoutManager = new CenterLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);

//绑定置顶

mRecyclerView.setLayoutManager(topLinearLayoutManager);

//绑定中间

mRecyclerView.setLayoutManager(centerLayoutManager );

//置顶显示调用

mRecyclerView.smoothScrollToPosition(position);

//中间显示调用 lastPostion是临时保存的目前处在中间的position

centerLayoutManager.smoothScrollToPosition(mRecyclerView, new RecyclerView.State(), lastPostion, position);

//记住当前选中的位置作为下一次选中的位置的上一次位置

if (lastPostion != position)

lastPostion = position;

本文地址:https://blog.csdn.net/u011652925/article/details/110957247

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值