lmw
2023-03-11 4df5bb59e5fe9f9d140e5610f7772dd8a05a28d4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.beloo.widget.chipslayoutmanager;
 
import android.content.Context;
import android.graphics.PointF;
import android.view.View;
import android.view.animation.LinearInterpolator;
 
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;
 
import com.beloo.widget.chipslayoutmanager.anchor.AnchorViewState;
import com.beloo.widget.chipslayoutmanager.layouter.IStateFactory;
 
class VerticalScrollingController extends ScrollingController implements IScrollingController {
 
    private ChipsLayoutManager lm;
 
    VerticalScrollingController(ChipsLayoutManager layoutManager, IStateFactory stateFactory, IScrollerListener scrollerListener) {
        super(layoutManager, stateFactory, scrollerListener);
        this.lm = layoutManager;
    }
 
    @Override
    public RecyclerView.SmoothScroller createSmoothScroller(@NonNull Context context, final int position, final int timeMs, final AnchorViewState anchor) {
        return new LinearSmoothScroller(context) {
            /*
             * LinearSmoothScroller, at a minimum, just need to know the vector
             * (x/y distance) to travel in order to get from the current positioning
             * to the target.
             */
            @Override
            public PointF computeScrollVectorForPosition(int targetPosition) {
                int visiblePosition = anchor.getPosition();
                //determine scroll up or scroll down needed
                return new PointF(0, position > visiblePosition ? 1 : -1);
            }
 
            @Override
            protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
                super.onTargetFound(targetView, state, action);
                int desiredTop = lm.getPaddingTop();
                int currentTop = lm.getDecoratedTop(targetView);
 
                int dy = currentTop - desiredTop;
 
                //perform fit animation to move target view at top of layout
                action.update(0, dy, timeMs, new LinearInterpolator());
            }
        };
    }
 
    @Override
    public boolean canScrollVertically() {
        canvas.findBorderViews();
        if (lm.getChildCount() > 0) {
            int top = lm.getDecoratedTop(canvas.getTopView());
            int bottom = lm.getDecoratedBottom(canvas.getBottomView());
 
            if (canvas.getMinPositionOnScreen() == 0
                    && canvas.getMaxPositionOnScreen() == lm.getItemCount() - 1
                    && top >= lm.getPaddingTop()
                    && bottom <= lm.getHeight() - lm.getPaddingBottom()) {
                return false;
            }
        } else {
            return false;
        }
 
        return lm.isScrollingEnabledContract();
    }
 
    @Override
    public boolean canScrollHorizontally() {
        return false;
    }
 
    @Override
    void offsetChildren(int d) {
        lm.offsetChildrenVertical(d);
    }
 
}