如何使Recyclerview中的Clicked位置居中

时间:2016-05-17 07:54:08

标签: android android-recyclerview

我想将点击的位置置于Recyclerview的中心位置。我可以将Recyclerview滚动到某个位置,但我想在屏幕中间位置。 我用这种方法滚动到那个位置。

videoRecyclerview.scrollToPosition(position);

7 个答案:

答案 0 :(得分:6)

首先移动滚动到您的项目,但每当recyclerView滚动它只是将项目带到可见区域时,它永远不会确定该项目是否在中心,所以我们找到中心项目,然后检查我们是否在下一个以项目或其后面为中心,这是工作逻辑

recyclerView.smoothScrollToPosition(index);
int firstVisibleItemPosition = rvLayoutManager.findFirstVisibleItemPosition();
int lastVisibleItemPosition = rvLayoutManager.findLastVisibleItemPosition();

int centerPosition = (firstVisibleItemPosition + lastVisibleItemPosition) / 2;

    if (index > centerPosition) {
        recyclerView.smoothScrollToPosition(index + 1);
    } else if (index < centerPosition) {
        recyclerView.smoothScrollToPosition(index - 1);
    }

答案 1 :(得分:6)

如果您使用 RecyclerView LinearLayoutManager ,这将有效:

private void scrollToCenter(View v) {
    int itemToScroll = mRecyclerView.getChildPosition(v);
    int centerOfScreen = mRecyclerView.getWidth() / 2 - v.getWidth() / 2;
    mLayoutManager.scrollToPositionWithOffset(itemToScroll, centerOfScreen);
}

答案 2 :(得分:1)

此代码对我有用:

 layoutManager.scrollToPositionWithOffset(pos - 1,0);

答案 3 :(得分:1)

如果需要水平和垂直的LieanerLayoutManager平滑滚动到中心

复制整个代码,然后简单地调用** scrollToCenter

public void scrollToCenter(LinearLayoutManager layoutManager, RecyclerView recyclerList, int clickPosition) {
    RecyclerView.SmoothScroller smoothScroller = createSnapScroller(recyclerList, layoutManager);

    if (smoothScroller != null) {
        smoothScroller.setTargetPosition(clickPosition);
        layoutManager.startSmoothScroll(smoothScroller);
    }
}

// This number controls the speed of smooth scroll
private static final float MILLISECONDS_PER_INCH = 70f;

private final static int DIMENSION = 2;
private final static int HORIZONTAL = 0;
private final static int VERTICAL = 1;

@Nullable
private LinearSmoothScroller createSnapScroller(RecyclerView mRecyclerView, RecyclerView.LayoutManager layoutManager) {
    if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
        return null;
    }
    return new LinearSmoothScroller(mRecyclerView.getContext()) {
        @Override
        protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
            int[] snapDistances = calculateDistanceToFinalSnap(layoutManager, targetView);
            final int dx = snapDistances[HORIZONTAL];
            final int dy = snapDistances[VERTICAL];
            final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
            if (time > 0) {
                action.update(dx, dy, time, mDecelerateInterpolator);
            }
        }


        @Override
        protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
            return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
        }
    };
}


private int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) {
    int[] out = new int[DIAMENTION];
    if (layoutManager.canScrollHorizontally()) {
        out[HORIZONTAL] = distanceToCenter(layoutManager, targetView,
                OrientationHelper.createHorizontalHelper(layoutManager));
    }

    if (layoutManager.canScrollVertically()) {
        out[VERTICAL] = distanceToCenter(layoutManager, targetView,
                OrientationHelper.createHorizontalHelper(layoutManager));
    }
    return out;
}


private int distanceToCenter(@NonNull RecyclerView.LayoutManager layoutManager,
                             @NonNull View targetView, OrientationHelper helper) {
    final int childCenter = helper.getDecoratedStart(targetView)
            + (helper.getDecoratedMeasurement(targetView) / 2);
    final int containerCenter;
    if (layoutManager.getClipToPadding()) {
        containerCenter = helper.getStartAfterPadding() + helper.getTotalSpace() / 2;
    } else {
        containerCenter = helper.getEnd() / 2;
    }
    return childCenter - containerCenter;
}

答案 4 :(得分:0)

如果使用linearlayoutManager,则可以使用此代码

linearLayoutManager.scrollToPositionWithOffset(2, 20);

(linearLayoutManager.void scrollToPositionWithOffset (int position, 
            int offset))

将偏移设置为0应与顶部

对齐

答案 5 :(得分:0)

有史以来最简单的黑客,对我来说足够了:

videoRecyclerview.scrollToPosition(position+2);

如果position + 2在数组列表中。

答案 6 :(得分:0)

//on the click callback 
    view.OnClickListener { callback?.onItemClicked(it)}        



// code in activity or your fragment
override fun onItemClicked(view: View) {
  val position = recyclerView.getChildLayoutPosition(view)
  recyclerView.smoothScrollToPosition(position)
}