summaryrefslogtreecommitdiff
path: root/library/src/main/java/moe/yuuta/flow/widgets/FlowPager.java
blob: e76f319d964c3008ab07ca5e68737ab5ccb39f4a (plain)
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
package moe.yuuta.flow.widgets;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class FlowPager extends ViewPager {
    public FlowPager(@NonNull Context context) {
        super(context);
    }

    public FlowPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    // Thanks to https://stackoverflow.com/a/32488566/6792243
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if(null != getAdapter()) {
            int height = 0;
            View child = ((FragmentStatePagerAdapter) getAdapter()).getItem(getCurrentItem()).getView();
            if (child != null) {
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                height = child.getMeasuredHeight();
                // TODO: Support api-14 and api-15?
                if (Build.VERSION.SDK_INT >= 16 && height < getMinimumHeight()) {
                    height = getMinimumHeight();
                }
            }

            int newHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            if (getLayoutParams().height != 0 && heightMeasureSpec != newHeight) {
                getLayoutParams().height = height;

            } else {
                heightMeasureSpec = newHeight;
            }
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    // Thanks to https://stackoverflow.com/a/13437997/6792243
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return false;
    }

    // Thanks to https://stackoverflow.com/a/13437997/6792243
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return false;
    }

}