java 一个简单的FrameLayout,可以在UI顶部隐藏/显示不确定的ProgressBar

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 一个简单的FrameLayout,可以在UI顶部隐藏/显示不确定的ProgressBar相关的知识,希望对你有一定的参考价值。

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Alex Fu
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

public class ProgressLayout extends FrameLayout {

  private ProgressBar progressBar;
  private View contentView;
  private boolean loading;

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

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

  public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  public void setContentView(View contentView) {
    this.contentView = contentView;
  }

  public void setLoading(boolean isLoading) {
    if (loading == isLoading) return;

    loading = isLoading;
    invalidateLoadingView();
  }

  private void invalidateLoadingView() {
    if (loading) {
      showLoadingView();
    } else {
      hideLoadingView();
    }
  }

  private void showLoadingView() {
    if (progressBar == null) {
      progressBar = new ProgressBar(getContext());
      LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      lp.gravity = Gravity.CENTER;
      progressBar.setLayoutParams(lp);
    }

    addView(progressBar);
    showContent(false);
  }

  private void hideLoadingView() {
    showContent(true);
    if (progressBar != null) {
      removeView(progressBar);
    }
  }

  private void showContent(boolean show) {
    if (contentView != null) {
      contentView.setVisibility(show ? View.VISIBLE : View.GONE);
    }
  }

}

以上是关于java 一个简单的FrameLayout,可以在UI顶部隐藏/显示不确定的ProgressBar的主要内容,如果未能解决你的问题,请参考以下文章

FrameLayout帧布局

FrameLayout(帧布局)

2.2.4 FrameLayout(帧布局)

Android五大布局之一帧布局(FrameLayout)

FragmentTabHost+FrameLayout实现底部导航栏

FrameLayout和TableLayout