gpt4 book ai didi

android - GridView 的平滑滚动

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:14:18 32 4
gpt4 key购买 nike

GridViewsmoothScrollToPosition() 方法是否正常工作?我找到了一个开放的 bug report我的也不能正常工作。

setSelection() 工作正常,但我想要平滑的滚动效果。

你对这个问题有什么想法吗?

如果这是一个长期存在的问题,我应该从哪里开始实现一个好的滚动效果?

最佳答案

虽然我在运行您发布的示例代码时没有发现任何问题,但如果您在应用程序中没有看到一致的结果,那么创建自己的滚动 Controller 并不太难。这是您可以使用的示例实现:

private class ScrollPositioner {
private static final int SCROLL_DURATION = 20;
private static final int DIR_UP = 1;
private static final int DIR_DOWN = 2;

int mTargetPosition = AdapterView.INVALID_POSITION;
int mDirection = AdapterView.INVALID_POSITION;
int mLastSeenPosition = AdapterView.INVALID_POSITION;
int mExtraScroll;
GridView mGrid;

public ScrollPositioner(GridView grid) {
mGrid = grid;
mExtraScroll = ViewConfiguration.get(mGrid.getContext()).getScaledFadingEdgeLength();
}

Handler mHandler = new Handler();
Runnable mScroller = new Runnable() {
public void run() {
int firstPos = mGrid.getFirstVisiblePosition();
switch(mDirection) {
case DIR_UP: {
if (firstPos == mLastSeenPosition) {
// No new views, let things keep going.
mHandler.postDelayed(mScroller, SCROLL_DURATION);
return;
}

final View firstView = mGrid.getChildAt(0);
if (firstView == null) {
return;
}
final int firstViewTop = firstView.getTop();
final int extraScroll = firstPos > 0 ? mExtraScroll : mGrid.getPaddingTop();

mGrid.smoothScrollBy(firstViewTop - extraScroll, SCROLL_DURATION);

mLastSeenPosition = firstPos;

if (firstPos > mTargetPosition) {
mHandler.postDelayed(mScroller, SCROLL_DURATION);
}
break;
}

case DIR_DOWN: {
final int lastViewIndex = mGrid.getChildCount() - 1;
final int lastPos = firstPos + lastViewIndex;

if (lastViewIndex < 0) {
return;
}

if (lastPos == mLastSeenPosition) {
// No new views, let things keep going.
mHandler.postDelayed(mScroller, SCROLL_DURATION);
return;
}

final View lastView = mGrid.getChildAt(lastViewIndex);
final int lastViewHeight = lastView.getHeight();
final int lastViewTop = lastView.getTop();
final int lastViewPixelsShowing = mGrid.getHeight() - lastViewTop;
final int extraScroll = lastPos < mGrid.getAdapter().getCount() - 1 ? mExtraScroll : mGrid.getPaddingBottom();

mGrid.smoothScrollBy(lastViewHeight - lastViewPixelsShowing + extraScroll, SCROLL_DURATION);

mLastSeenPosition = lastPos;
if (lastPos < mTargetPosition) {
mHandler.postDelayed(mScroller, SCROLL_DURATION);
}
break;
}

default:
break;
}
}
};

public void scrollToPosition(int position) {
mTargetPosition = position;
mLastSeenPosition = AdapterView.INVALID_POSITION;

if(position < mGrid.getFirstVisiblePosition()) {
mDirection = DIR_UP;
} else if (position > mGrid.getLastVisiblePosition()) {
mDirection = DIR_DOWN;
} else {
return;
}
mHandler.post(mScroller);
}
}

这是您链接的示例中的一个 fragment ,其中包括这个新类在哪里代替框架实现进行滚动:

GridView g;
boolean t= false;
@Override
public void onBackPressed() {
//Instantiate and attach the custom positioner
if(mPositioner == null) {
mPositioner = new ScrollPositioner(g);
}
//Use the custom object to scroll the view
mPositioner.scrollToPosition(0);
if(t) {
super.onBackPressed();
}
else {
t = true;
}
}

如果你想添加一个功能,即使滚动方向向下,所选位置也总是滚动到顶部,你可以这样做。 这不是框架的实现旨在做的事情,但您可以通过在 DIR_DOWN 中添加一些代码以继续滚动直到第一个可见位置匹配目标(如 DIR_UP 确实如此)。您还必须注意滚动在位置到达顶部之前结束的情况,因此您不会经常发布处理程序,以防永远不会得到不同的结果。

HTH

关于android - GridView 的平滑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8903212/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com