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 HorizontalScrollingController extends ScrollingController implements IScrollingController {
 
    private ChipsLayoutManager layoutManager;
 
    HorizontalScrollingController(ChipsLayoutManager layoutManager, IStateFactory stateFactory, IScrollerListener scrollerListener) {
        super(layoutManager, stateFactory, scrollerListener);
        this.layoutManager = 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(position > visiblePosition ? 1 : -1, 0);
            }
 
            @Override
            protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
                super.onTargetFound(targetView, state, action);
                int currentLeft = layoutManager.getPaddingLeft();
                int desiredLeft = layoutManager.getDecoratedLeft(targetView);
 
                int dx = desiredLeft - currentLeft;
 
                //perform fit animation to move target view at top of layoutX
                action.update(dx, 0, timeMs, new LinearInterpolator());
            }
        };
    }
 
    @Override
    public boolean canScrollVertically() {
        return false;
    }
 
    @Override
    public boolean canScrollHorizontally() {
        canvas.findBorderViews();
        if (layoutManager.getChildCount() > 0) {
            int left = layoutManager.getDecoratedLeft(canvas.getLeftView());
            int right = layoutManager.getDecoratedRight(canvas.getRightView());
 
            if (canvas.getMinPositionOnScreen() == 0
                    && canvas.getMaxPositionOnScreen() == layoutManager.getItemCount() - 1
                    && left >= layoutManager.getPaddingLeft()
                    && right <= layoutManager.getWidth() - layoutManager.getPaddingRight()) {
                return false;
            }
        } else {
            return false;
        }
 
        return layoutManager.isScrollingEnabledContract();
    }
 
    @Override
    void offsetChildren(int d) {
        layoutManager.offsetChildrenHorizontal(d);
    }
 
}