我爲此創建了一個自定義PreferenceFragment
。
您可以擴展這一個而不是PreferenceFragment
,並將您的電話延遲至addPreferencesFromResource
,直到完成所有加載後,然後隱藏ProgressBar
並顯示您的內容。
public class ProgressBarPreferenceFragment extends PreferenceFragment {
private FrameLayout progress;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
showLoading((ViewGroup) view);
return view;
}
@Override
public void addPreferencesFromIntent(Intent intent) {
super.addPreferencesFromIntent(intent);
hideLoading();
}
@Override
public void addPreferencesFromResource(int preferencesResId) {
super.addPreferencesFromResource(preferencesResId);
hideLoading();
}
private void hideLoading() {
ViewGroup root = (ViewGroup) getView();
if ((root != null) && (progress != null)) {
root.removeView(progress);
}
}
private void showLoading(ViewGroup root) {
progress = new FrameLayout(root.getContext());
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
progress.addView(new ProgressBar(root.getContext()), lp);
LayoutParams lp2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
root.addView(progress, lp2);
}
}
我現在正面臨類似的情況。你能找到解決你的問題嗎? – Phat7