2016-03-01 141 views
0

我試圖調整viewpager到頁面的其餘部分。我有這個globallayout聽衆:Viewpager填充頁面的其餘部分

ViewTreeObserver viewTreeObserver = pager.getViewTreeObserver(); 
     viewTreeObserver 
       .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

        @Override 
        public void onGlobalLayout() { 

         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
           LinearLayout.LayoutParams.WRAP_CONTENT, 
           LinearLayout.LayoutParams.WRAP_CONTENT); 

         int viewPagerWidth = pager.getWidth(); 
         float viewPagerHeight = Constants.screenHeight - findViewById(R.id.view_above_pager).getBottom(); 
         Log.i("","viewpager height:" + viewPagerHeight); 
         layoutParams.width = viewPagerWidth; 
         layoutParams.height = (int) viewPagerHeight; 

         pager.setLayoutParams(layoutParams); 
         pager.setMinimumHeight((int)viewPagerHeight); 
         pager.getViewTreeObserver() 
           .removeGlobalOnLayoutListener(this); 
         Log.i("","viewpager height2:" + pager.getHeight()); 
        } 
       }); 

顯然,第一個日誌,在viewPagerHeight顯示:viewpager高度:530.0 但第二個呼機上顯示爲0。爲什麼會出現這種情況?它不應該刷新高度嗎?

回答

1

這是一個滾動型嘗試,那就是我的問題。重構了一下設計,現在它可以工作。 我不確定,但我認爲ScrollView強制孩子們包裝內容,所以然後我的viewpager的高度不能設置自定義。

1

嘗試使用這個UTIL,也與onPreDraw

public class UViews { 
    /** 
    * Calls the listener when the onPreDraw method of the {@link ViewTreeObserver.OnPreDrawListener} is called. 
    * This method will handle it so the onPreDraw is only called once and will remove the listener. 
    * 
    * @param view 
    * @param listener 
    */ 
    public static void waitForOnPreDraw(final View view, final IOnPreDawListener listener) { 
     if (view == null) { 
      return; 
     } 
     ViewTreeObserver treeObs = view.getViewTreeObserver(); 
     if (treeObs == null) { 
      if (listener != null) { 
       listener.onPreDraw(view); 
      } 
      return; 
     } 
     treeObs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
      @Override 
      public boolean onPreDraw() { 
       if (view == null) { 
        if (listener != null) { 
         listener.onPreDraw(null); 
        } 
        return true; 
       } 
       // IMPORTANT !!! If not removed, it will enter an infinite loop 
       ViewTreeObserver treeObs = view.getViewTreeObserver(); 
       if (treeObs != null && treeObs.isAlive()) { 
        treeObs.removeOnPreDrawListener(this); 
       } 
       if (listener != null) { 
        listener.onPreDraw(view); 
       } 
       return true; 
      } 
     }); 

    } 

    /** 
    * Calls the listener when the onGlobalLayout method of the {@link ViewTreeObserver.OnGlobalLayoutListener} is called. 
    * This method will handle it so the onGlobalLayout is only called once and will remove the listener. 
    * 
    * @param view 
    * @param listener 
    */ 
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
    public static void waitForOnGlobalLayout(final View view, final IOnGlobalLayoutListener listener) { 
     if (view == null) { 
      return; 
     } 
     ViewTreeObserver treeObs = view.getViewTreeObserver(); 
     if (treeObs == null) { 
      if (listener != null) { 
       listener.onGlobalLayout(view); 
      } 
      return; 
     } 
     treeObs.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       if (view == null) { 
        if (listener != null) { 
         listener.onGlobalLayout(null); 
        } 
        return; 
       } 
       // IMPORTANT !!! If not removed, it will enter an infinite loop 
       ViewTreeObserver treeObs = view.getViewTreeObserver(); 
       if (treeObs != null && treeObs.isAlive()) { 
        treeObs.removeOnGlobalLayoutListener(this); 
       } 
       if (listener != null) { 
        listener.onGlobalLayout(view); 
       } 
      } 
     }); 

    } 



    //------------------------------------------------- INTERFACE(start) 
    public interface IOnPreDawListener { 
     void onPreDraw(View view); 
    } 

    public interface IOnGlobalLayoutListener { 
     void onGlobalLayout(View view); 
    } 
    //------------------------------------------------- INTERFACE(end) 
} 
+0

nope :)))在預繪製時不起作用,仍然是0 –

相關問題