1. 程式人生 > >詳解巢狀ListView、ScrollView佈局顯示不全的問題

詳解巢狀ListView、ScrollView佈局顯示不全的問題

在專案開發中,可能經常遇到巢狀ListView、ScrollView的問題,百度一搜,都是現成的程式碼,而且都是一樣的,就是重寫onMeasure方法,但是為什麼要那麼寫,估計就沒多少人知道了,這裡進行深入的剖析一下下,重點看onMeasure方法,程式碼如下:

/**
 * Created by hailonghan on 15/5/28.
 */
public class ExpandListView extends ListView {

    public ExpandListView(Context context) {
        super(context);
    }

    public
ExpandListView(Context context, AttributeSet attrs) { super(context, attrs); } public ExpandListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public ExpandListView(Context context, AttributeSet attrs, int
defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2 , MeasureSpec.AT_MOST); super
.onMeasure(widthMeasureSpec, expandSpec); } }

看onMeasure()方法,很多同學很疑惑,那個Integer.MAX_VALUE >> 2是咋個意思,MeasureSpec.AT_MOST又是咋個意思,總之就迷迷糊糊把程式碼拷到專案中了,一執行,好使,立馬高興的不得了,但是原理搞不懂,等別人問的時候,為什麼這樣寫,就是講不出來。

要搞明白原理,就要搞懂MeasureSpec這個類,這裡我貼一下MeasureSpec類的原始碼,如下

public static class MeasureSpec {
        private static final int MODE_SHIFT = 30;
        private static final int MODE_MASK  = 0x3 << MODE_SHIFT;

        /**
         * Measure specification mode: The parent has not imposed any constraint
         * on the child. It can be whatever size it wants.
         */
        public static final int UNSPECIFIED = 0 << MODE_SHIFT;

        /**
         * Measure specification mode: The parent has determined an exact size
         * for the child. The child is going to be given those bounds regardless
         * of how big it wants to be.
         */
        public static final int EXACTLY     = 1 << MODE_SHIFT;

        /**
         * Measure specification mode: The child can be as large as it wants up
         * to the specified size.
         */
        public static final int AT_MOST     = 2 << MODE_SHIFT;

        /**
         * Creates a measure specification based on the supplied size and mode.
         *
         * The mode must always be one of the following:
         * <ul>
         *  <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
         *  <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
         *  <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
         * </ul>
         *
         * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's
         * implementation was such that the order of arguments did not matter
         * and overflow in either value could impact the resulting MeasureSpec.
         * {@link android.widget.RelativeLayout} was affected by this bug.
         * Apps targeting API levels greater than 17 will get the fixed, more strict
         * behavior.</p>
         *
         * @param size the size of the measure specification
         * @param mode the mode of the measure specification
         * @return the measure specification based on size and mode
         */
        public static int makeMeasureSpec(int size, int mode) {
            if (sUseBrokenMakeMeasureSpec) {
                return size + mode;
            } else {
                return (size & ~MODE_MASK) | (mode & MODE_MASK);
            }
        }

        /**
         * Extracts the mode from the supplied measure specification.
         *
         * @param measureSpec the measure specification to extract the mode from
         * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
         *         {@link android.view.View.MeasureSpec#AT_MOST} or
         *         {@link android.view.View.MeasureSpec#EXACTLY}
         */
        public static int getMode(int measureSpec) {
            return (measureSpec & MODE_MASK);
        }

        /**
         * Extracts the size from the supplied measure specification.
         *
         * @param measureSpec the measure specification to extract the size from
         * @return the size in pixels defined in the supplied measure specification
         */
        public static int getSize(int measureSpec) {
            return (measureSpec & ~MODE_MASK);
        }

        static int adjust(int measureSpec, int delta) {
            final int mode = getMode(measureSpec);
            if (mode == UNSPECIFIED) {
                // No need to adjust size for UNSPECIFIED mode.
                return makeMeasureSpec(0, UNSPECIFIED);
            }
            int size = getSize(measureSpec) + delta;
            if (size < 0) {
                Log.e(VIEW_LOG_TAG, "MeasureSpec.adjust: new size would be negative! (" + size +
                        ") spec: " + toString(measureSpec) + " delta: " + delta);
                size = 0;
            }
            return makeMeasureSpec(size, mode);
        }

        /**
         * Returns a String representation of the specified measure
         * specification.
         *
         * @param measureSpec the measure specification to convert to a String
         * @return a String with the following format: "MeasureSpec: MODE SIZE"
         */
        public static String toString(int measureSpec) {
            int mode = getMode(measureSpec);
            int size = getSize(measureSpec);

            StringBuilder sb = new StringBuilder("MeasureSpec: ");

            if (mode == UNSPECIFIED)
                sb.append("UNSPECIFIED ");
            else if (mode == EXACTLY)
                sb.append("EXACTLY ");
            else if (mode == AT_MOST)
                sb.append("AT_MOST ");
            else
                sb.append(mode).append(" ");

            sb.append(size);
            return sb.toString();
        }
    }

程式碼不是很長,其實裡面最重要的是3種模式和3個方法

3種模式

  1. UNSPECIFIED模式,官方意思是:父佈局沒有給子佈局強加任何約束,子佈局想要多大就要多大,說白了就是不確定大小
  2. EXACTLY模式,官方意思是:父佈局給子佈局限定了準確的大小,子佈局的大小就是精確的,父親給多大就是多大
  3. AT_MOST模式,官方意思是:父佈局給定了一個最大的值,子佈局的大小不能超過這個值,當然可以比這個值小

3個方法

1.public static int makeMeasureSpec(int size, int mode) ,這個方法的作用是根據大小和模式來生成一個int值,這個int值封裝了模式和大小資訊
2.public static int getMode(int measureSpec),這個方法的作用是通過一個int值來獲取裡面的模式資訊
3.public static int getSize(int measureSpec),這個方法的作用是通過一個int值來獲取裡面的大小資訊

在Android裡面,一個控制元件所佔的模式和大小是通過一個整數int來表示的,這裡很多同學就疑惑了,一個int值是怎麼來表示模式的大小的,這裡來看一張圖片:

這裡寫圖片描述

原來,Android裡面把int的最高2兩位來表示模式,最低30位來表示大小

private static final int MODE_SHIFT = 30;

  • public static final int UNSPECIFIED = 0 << MODE_SHIFT;
  • public static final int EXACTLY = 1 << MODE_SHIFT;
  • public static final int AT_MOST = 2 << MODE_SHIFT;

    不確定模式是0左移30位,也就是int型別的最高兩位是00
    精確模式是1左移30位,也就是int型別的最高兩位是01
    最大模式是是2左移30位,也就是int型別的最高兩位是10

現在在回頭看我們之前的程式碼,如下:

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2
                , MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

講到這裡很多同學就明白了,我們呼叫了makeMeasureSpec方法,這個方法是用來生成一個帶有模式和大小資訊的int值的,第一個引數Integer.MAX_VALUE >> 2,這個引數是傳的一個大小值,為什麼是這個值呢,我們現在已經知道了,我們要生成的控制元件,它的大小最大值是int的最低30位的最大值,我們先取Integer.MAX_VALUE來獲取int值的最大值,然後左移2位就得到這個臨界值最大值了
當然,我們在手機上的控制元件的大小不可能那麼大,極限值就那麼大,實際肯定比那個小,所以這個模式就得選擇MeasureSpec.AT_MOST了,最後將生成的這個大小傳遞給父控制元件就可以了,super.onMeasure(widthMeasureSpec, expandSpec),這個函式只改變的是控制元件的高度,寬度沒有改變,實際開發當中不管listview有多少條資料,都能一次性展現出來