72 lines
2.5 KiB
Java
72 lines
2.5 KiB
Java
/*
|
||
* Copyright 2018 GcsSloop
|
||
*
|
||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
* you may not use this file except in compliance with the License.
|
||
* You may obtain a copy of the License at
|
||
*
|
||
* http://www.apache.org/licenses/LICENSE-2.0
|
||
*
|
||
* Unless required by applicable law or agreed to in writing, software
|
||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
* See the License for the specific language governing permissions and
|
||
* limitations under the License.
|
||
*
|
||
* Last modified 2018-04-09 23:56:59
|
||
*
|
||
* GitHub: https://github.com/GcsSloop
|
||
* WeiBo: http://weibo.com/GcsSloop
|
||
* WebSite: http://www.gcssloop.com
|
||
*/
|
||
|
||
package com.shayu.onetoone.widget;
|
||
|
||
import android.util.DisplayMetrics;
|
||
import android.view.View;
|
||
|
||
import androidx.annotation.NonNull;
|
||
import androidx.recyclerview.widget.LinearSmoothScroller;
|
||
import androidx.recyclerview.widget.RecyclerView;
|
||
|
||
import static com.shayu.onetoone.widget.PagerConfig.Logi;
|
||
|
||
|
||
/**
|
||
* 作用:用于处理平滑滚动
|
||
* 作者:GcsSloop
|
||
* 摘要:用于用户手指抬起后页面对齐或者 Fling 事件。
|
||
*/
|
||
public class PagerGridSmoothScroller extends LinearSmoothScroller {
|
||
private RecyclerView mRecyclerView;
|
||
|
||
public PagerGridSmoothScroller(@NonNull RecyclerView recyclerView) {
|
||
super(recyclerView.getContext());
|
||
mRecyclerView = recyclerView;
|
||
}
|
||
|
||
@Override
|
||
protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
|
||
RecyclerView.LayoutManager manager = mRecyclerView.getLayoutManager();
|
||
if (null == manager) return;
|
||
if (manager instanceof PagerGridLayoutManager) {
|
||
PagerGridLayoutManager layoutManager = (PagerGridLayoutManager) manager;
|
||
int pos = mRecyclerView.getChildAdapterPosition(targetView);
|
||
int[] snapDistances = layoutManager.getSnapOffset(pos);
|
||
final int dx = snapDistances[0];
|
||
final int dy = snapDistances[1];
|
||
Logi("dx = " + dx);
|
||
Logi("dy = " + dy);
|
||
final int time = calculateTimeForScrolling(Math.max(Math.abs(dx), Math.abs(dy)));
|
||
if (time > 0) {
|
||
action.update(dx, dy, time, mDecelerateInterpolator);
|
||
}
|
||
}
|
||
}
|
||
|
||
@Override
|
||
protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
|
||
return PagerConfig.getMillisecondsPreInch() / displayMetrics.densityDpi;
|
||
}
|
||
}
|