Android RecyclerView 快速滑到顶部

  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;
        }
    }
}


文章来自:点击打开链接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值