admin
2020-10-10 8039a1b2fbfa3471b6f726d3e839d7867c81a84f
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
package com.lcjian.library.widget;
 
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
 
public class MyViewPager extends ViewPager {
 
    public MyViewPager(Context context) {
        super(context);
    }
 
    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthSpecMode != MeasureSpec.EXACTLY || heightSpecMode != MeasureSpec.EXACTLY) {
            measureChildren(widthMeasureSpec, heightMeasureSpec);
            if (widthSpecMode != MeasureSpec.EXACTLY) {
                int width = 0;
                for (int i = 0; i < getChildCount(); i++) {
                    View child = getChildAt(i);
                    int w = child.getMeasuredWidth();
                    if (w > width) {
                        width = w;
                    }
                }
                widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            }
            if (heightSpecMode != MeasureSpec.EXACTLY) {
                int height = 0;
                for (int i = 0; i < getChildCount(); i++) {
                    View child = getChildAt(i);
                    int h = child.getMeasuredHeight();
                    if (h > height) {
                        height = h;
                    }
                }
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}