Android RecyclerView 快速滑到顶部

时间:2022-11-17 23:00:06
  1. 创建FastScrollLinearLayoutManager,继承LinearLayoutManager
  2. 复写smoothScrollToPosition()方法,主要复写LinearSmoothScroller中方法

代码如下,解释全在注释中:

public class FastScrollLinearLayoutManager extends LinearLayoutManager {
public FastScrollLinearLayoutManager(Context context) {
super(context);
}

@Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {

@Override
public PointF computeScrollVectorForPosition(int targetPosition) {
return FastScrollLinearLayoutManager.this.computeScrollVectorForPosition(targetPosition);
}

//该方法控制速度。
//if returned value is 2 ms, it means scrolling 1000 pixels with LinearInterpolation should take 2 seconds.
@Override
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
/*
控制单位速度, 毫秒/像素, 滑动1像素需要多少毫秒.

默认为 (25F/densityDpi) 毫秒/像素

mdpi上, 1英寸有160个像素点, 25/160,
xxhdpi,1英寸有480个像素点, 25/480,
*/

//return 10F / displayMetrics.densityDpi;//可以减少时间,默认25F
return super.calculateSpeedPerPixel(displayMetrics);
}

//该方法计算滑动所需时间。在此处间接控制速度。
//Calculates the time it should take to scroll the given distance (in pixels)
@Override
protected int calculateTimeForScrolling(int dx) {
/*
控制距离, 然后根据上面那个方(calculateSpeedPerPixel())提供的速度算出时间,

默认一次 滚动 TARGET_SEEK_SCROLL_DISTANCE_PX = 10000个像素,

在此处可以减少该值来达到减少滚动时间的目的.
*/

//间接计算时提高速度,也可以直接在calculateSpeedPerPixel提高
if (dx > 3000) {
dx = 3000;
}

int time = super.calculateTimeForScrolling(dx);
LogUtil.d(time);//打印时间看下

return time;
}
};

linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
}


使用方法(代码链接:点击打开链接):

package com.example.testing.myapplication.module.fastscrooll;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import com.example.testing.myapplication.R;
import com.example.testing.myapplication.module.loadmoreRecyclerView.SimpleTextAdapter;
import com.example.testing.myapplication.util.LogUtil;

/**
* author: baiiu
* date: on 16/7/6 14:49
* description:
*/
public class FastScrollFragment extends Fragment implements View.OnClickListener {

private RecyclerView recyclerView;
private SimpleTextAdapter mAdapter;
private int mVisibleCount;

@Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fastscroll, container, false);

recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setBackgroundColor(Color.BLUE);

LinearLayoutManager linearLayoutManager = new FastScrollLinearLayoutManager(getContext());
recyclerView.setLayoutManager(linearLayoutManager);

mAdapter = new SimpleTextAdapter(getContext(), 500);
recyclerView.setAdapter(mAdapter);

recyclerView.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override public void onGlobalLayout() {
recyclerView.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);

LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

mVisibleCount = linearLayoutManager.findLastVisibleItemPosition()
- linearLayoutManager.findFirstVisibleItemPosition() + 1;
LogUtil.d("显示这么多个: " + mVisibleCount);
}
});


view.findViewById(R.id.fast_top)
.setOnClickListener(this);
view.findViewById(R.id.fast_top_zhihuway)
.setOnClickListener(this);
view.findViewById(R.id.fast_end)
.setOnClickListener(this);

return view;
}

@Override public void onClick(View v) {
switch (v.getId()) {
case R.id.fast_top_zhihuway:
/*
仿知乎,先直接到一个位置,然后再滑动到顶部
*/
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();

if (firstVisibleItemPosition > mVisibleCount) {
recyclerView.scrollToPosition(mVisibleCount);
}
recyclerView.smoothScrollToPosition(0);

break;

/*
这两个都是在LinearSmoothScroller里缩短了单位时间距离以达到减少时间,快速滑动
*/
case R.id.fast_top:
recyclerView.smoothScrollToPosition(10000);
break;
case R.id.fast_end:
recyclerView.smoothScrollToPosition(0);
break;
}
}
}


文章来自:点击打开链接