1. 程式人生 > >Android RecyclerView 快速滑到頂部

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