【问题标题】:smoothScrollToPosition not working in recyclerViewsmoothScrollToPosition 在 recyclerView 中不起作用
【发布时间】:2019-06-12 13:14:13
【问题描述】:

我创建了自定义 LinearLayoutManager 类。我的目标是用动画平滑ScrollToPosition。这是我的代码:

public class LinearLayoutManagerWithSmoothScroller extends LinearLayoutManager {
private static final float MILLISECONDS_PER_INCH = 100f;

public LinearLayoutManagerWithSmoothScroller(Context context) {
    super(context, VERTICAL, false);
}
@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 LinearLayoutManagerWithSmoothScroller.this
                .computeScrollVectorForPosition(targetPosition);
    }

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

    @Override
    protected int getVerticalSnapPreference() {
        return SNAP_TO_START;
    }
}

}

另外,我在 RecyclerView 中创建了自定义 LayoutAnimation。这是一个 xml 代码

<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/item_animation_from_bottom"
android:delay="15%"
android:animationOrder="normal"
/>

这是我的 java 代码。

        leaderBoardAdapter = new SPGamificationLeaderBoardAdapter(response.list, getContext());
    leaderBoardRecyclerView.setAdapter(leaderBoardAdapter);
    leaderBoardRecyclerView.setHasFixedSize(true);
    leaderBoardRecyclerView.setNestedScrollingEnabled(false);

    LayoutAnimationController controller =
            AnimationUtils.loadLayoutAnimation(getContext(), R.anim.layout_animation_from_bottom);
    leaderBoardRecyclerView.setLayoutManager(smoothScroller);

    leaderBoardRecyclerView.setLayoutAnimation(controller);
    leaderBoardRecyclerView.scheduleLayoutAnimation();
    leaderBoardRecyclerView.post(() -> leaderBoardRecyclerView.smoothScrollToPosition(getPosition(response)));

我的问题是,两个选项(smoothScrollToPosition 和 LayoutAnimation)不能同时工作。我删除了 smoothScrollToPosition 和布局动画工作,并删除了 smoothScrollToPosition - 布局动画工作。 有什么方法可以同时使用这两个功能吗?我的代码有什么问题?

【问题讨论】:

    标签: android android-recyclerview android-animation android-xml


    【解决方案1】:

    您可以使用scrollToPosition(itemNo) 来实现您的目标。

    如下:

    recyclerview.scrollToPosition(20);
    

    使用 smoothScrollToPosition ,项目滚动非常快,这就是动画不起作用的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多