2013-07-25 105 views
2

我想創建一個相對通用的ViewGroup容器,以便在加載任務就緒時切換到進度條並再次隱藏它(+切換到內容)。這應該很容易使用,適合大多數(不是全部)情況。addView在自定義FrameLayout中不可見,但可點擊

我知道這可能有幾次被問過,但是我沒有找到一個合適的解決方案,在這個工作4-5小時後。

什麼不行? 進度條完全不顯示。我用可見性嘗試了它,並以幾種方式刪除/添加所有視圖。它根本不顯示。如果您調用hideProgressBar()方法(以xml指定),例如一個listview,顯示完美。只有ProgressBar似乎是看不見的。雖然進度條是可點擊的。

任何想法?

這裏是源:

public class ProgressBarContainer extends FrameLayout { 

    private static final int DEFAULT_PROGRESSBAR_STYLE = android.R.style.Widget_Holo_Light_ProgressBar_Large; 

    private boolean isIndeterminate = true; // default is true 
    private int mProgressBarStyle = DEFAULT_PROGRESSBAR_STYLE; 

    private ProgressBar mProgressBar; 

    private List<View> childCache = new ArrayList<View>(); 

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

    public ProgressBarContainer(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(attrs); 
    } 

    public ProgressBarContainer(Context context, AttributeSet attrs, 
      int defStyle) { 
     super(context, attrs, defStyle); 
     init(attrs); 
    } 

    /** 
    * Shows the ProgressBar and hides all "Content" Views 
    */ 
    public void showProgressBar() { 
     pushBackChilds(); 
     // 
     // mProgressBar.setVisibility(View.VISIBLE); 
     // mProgressBar.bringToFront(); 
     // setContentVisibility(View.INVISIBLE); 

    } 

    private void pushBackChilds() { 
     for (int i = 0; i < getChildCount(); i++) { 
      View child = this.getChildAt(i); 
      if (child != mProgressBar) { 
       childCache.add(child); 
       removeView(child); 
      } else { 
       Logging.d("ProgressBarContainer", "is ProgressBar at" + i); 
      } 
     } 

     addView(mProgressBar); 
    } 

    /** 
    * Hides the ProgressBar and shows all "Content" Views 
    */ 
    public void hideProgressBar() { 
     // mProgressBar.setVisibility(View.INVISIBLE); 
     // 
     // setContentVisibility(View.VISIBLE); 
     rescueChilds(); 

    } 

    private void rescueChilds() { 

     removeView(mProgressBar); 
     for (View child : childCache) { 
      if (child != null) { 
       this.addView(child); 
      } 
     } 
    } 

    /** 
    * Changes visibility of all content except the ProgressBar 
    * 
    * @param visibility 
    */ 
    public void setContentVisibility(int visibility) { 
     for (int i = 0; i < getChildCount(); i++) { 
      View child = this.getChildAt(i); 
      if (child != mProgressBar) { 
       child.setVisibility(visibility); 
      } else { 
       Logging.d("ProgressBarContainer", "is ProgressBar at" + i); 
      } 
     } 
    } 

    public ProgressBar getProgressBar() { 
     return mProgressBar; 
    } 

    private void init(AttributeSet attrs) { 
     // get styleables 
     TypedArray a = getContext().obtainStyledAttributes(attrs, 
       R.styleable.ProgressBarContainer); 
     isIndeterminate = a.getBoolean(
       R.styleable.ProgressBarContainer_isIndeterminate, true); 
     mProgressBarStyle = a.getInt(
       R.styleable.ProgressBarContainer_progressBarStyle, 
       DEFAULT_PROGRESSBAR_STYLE); 

     a.recycle(); 

     // init progressbar and stuff 

     mProgressBar = new ProgressBar(getContext(), null, mProgressBarStyle); 
     mProgressBar.setVisibility(ProgressBar.VISIBLE); 
     mProgressBar.setIndeterminate(isIndeterminate); 

     mProgressBar.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Logging.d("ProgressBar", "Clicked progressbar"); 

      } 
     }); 

     LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     lp.gravity = Gravity.CENTER; 
     mProgressBar.setLayoutParams(lp); 
     // addView(mProgressBar); 
    } 
} 

這裏是解決方案: http://pastebin.com/2xjnCDLS

回答

0

,而不僅僅是創建自己的自定義視圖切換視圖,爲什麼不使用ViewSwitcher呢?

它的工作方式是將2個視圖放在viewSwitcher中,其中一個可能是progressBar(或者是一個包含它的佈局,也可能是一個textView),另一個是內容本身。

當您準備好切換時,您可以使用viewSwitcher。 showNext()。

爲了確保顯示正確的視圖,您可以檢查從getCurrentView()返回的內容。

+0

明確的一個想法,沒有想到這一點。肯定會嘗試並報告。 – Aeefire

+0

是的,它甚至可以按照您的計劃從FrameLayout擴展... :)您還可以在轉換之間設置動畫。 –

+0

感謝您的好評。它完美無瑕。 (FINAL SOLUTION:http://pastebin.com/2xjnCDLS) – Aeefire