lmw
2024-05-27 c00669a852702e1aa1326872bb916f9a079b57e2
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
package com.beloo.widget.chipslayoutmanager.anchor;
 
import android.graphics.Rect;
import android.view.View;
 
import androidx.recyclerview.widget.RecyclerView;
 
import com.beloo.widget.chipslayoutmanager.ChildViewsIterable;
import com.beloo.widget.chipslayoutmanager.layouter.ICanvas;
 
public class ColumnsAnchorFactory extends AbstractAnchorFactory {
 
    private ChildViewsIterable childViews;
 
    public ColumnsAnchorFactory(RecyclerView.LayoutManager lm, ICanvas canvas) {
        super(lm, canvas);
        childViews = new ChildViewsIterable(lm);
    }
 
    /** get the closest views to left border. The highest view will be picked from it. */
    @Override
    public AnchorViewState getAnchor() {
 
        AnchorViewState minPosView = AnchorViewState.getNotFoundState();
 
        int minPosition = Integer.MAX_VALUE;
        int minLeft = Integer.MAX_VALUE;
        int maxRight = Integer.MIN_VALUE;
 
        for (View view : childViews) {
            AnchorViewState anchorViewState = createAnchorState(view);
            int pos = lm.getPosition(view);
            int left = lm.getDecoratedLeft(view);
            int right = lm.getDecoratedRight(view);
 
            Rect viewRect = new Rect(anchorViewState.getAnchorViewRect());
 
            if (getCanvas().isInside(viewRect) && !anchorViewState.isRemoving()) {
                if (minPosition > pos) {
                    minPosition = pos;
                    minPosView = anchorViewState;
                }
 
                if (minLeft > left) {
                    minLeft = left;
                    maxRight = right;
                } else if (minLeft == left) {
                    maxRight = Math.max(maxRight, right);
                }
 
            }
        }
 
        if (!minPosView.isNotFoundState()) {
            minPosView.getAnchorViewRect().left = minLeft;
            minPosView.getAnchorViewRect().right = maxRight;
 
            minPosView.setPosition(minPosition);
        }
 
        return minPosView;
    }
 
    @Override
    public void resetRowCoordinates(AnchorViewState anchorView) {
        if (!anchorView.isNotFoundState()) {
            Rect rect = anchorView.getAnchorViewRect();
            rect.top = getCanvas().getCanvasTopBorder();
            rect.bottom = getCanvas().getCanvasBottomBorder();
        }
    }
}