我想创建一个相对通用的 ViewGroup 容器,让它在加载任务准备好时切换到进度条并再次隐藏它(+切换到内容)。这应该易于使用并且适合大多数(不是全部)情况。
我知道这可能已经被问过好几次了,但是在为此工作了 4-5 小时后我找不到合适的解决方案。
什么不起作用? 进度条根本不显示。我尝试了 Visibility 并以多种方式删除/添加所有视图。它根本没有出现。如果您调用 hideProgressBar() 子项(在 xml 中指定),例如列表视图,则显示得非常好。只是 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