如何在ListActivity和Fragment中嵌入加载进度指示器

时间:2013-02-01 00:04:26

标签: android android-fragments android-activity

我是否有办法在ListActivity或Fragment中嵌入加载进度指示器,就像我在ListFragment中调用setListShown()一样?我可以使用加载对话框,但我更喜欢保持一致,因为我已经在其他ListFragments中使用了setListShown()。

感谢。

1 个答案:

答案 0 :(得分:2)

我通过编写类似于ListFragment的片段类的包装器来实现这一点。

public class AsyncFragment extends Fragment {

static final int INTERNAL_PROGRESS_CONTAINER_ID = 0x00ff0001;
static final int INTERNAL_CONTENT_CONTAINER_ID = 0x00ff0002;

View mProgressContainer;
View mContentContainer;
boolean mContentShown = true;

public AsyncFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final Context context = getActivity();

    FrameLayout root = new FrameLayout(context);

    // ------------------------------------------------------------------

    LinearLayout pframe = new LinearLayout(context);
    pframe.setId(INTERNAL_PROGRESS_CONTAINER_ID);
    pframe.setOrientation(LinearLayout.VERTICAL);
    pframe.setVisibility(View.GONE);
    pframe.setGravity(Gravity.CENTER);

    ProgressBar progress = new ProgressBar(context, null,
            android.R.attr.progressBarStyleLarge);
    pframe.addView(progress, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

    root.addView(pframe, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    // ------------------------------------------------------------------

    FrameLayout lframe = new FrameLayout(context);
    lframe.setId(INTERNAL_CONTENT_CONTAINER_ID);

    root.addView(lframe, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    // ------------------------------------------------------------------

    root.setLayoutParams(new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

    return root;
}

public void setView(View v) {

    FrameLayout lframe = (FrameLayout) getView().findViewById(INTERNAL_CONTENT_CONTAINER_ID);
    lframe.addView(v, new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mProgressContainer = getView().findViewById(INTERNAL_PROGRESS_CONTAINER_ID);
    mContentContainer = getView().findViewById(INTERNAL_CONTENT_CONTAINER_ID);
    setContentShown(false, false);
}

@Override
public void onDestroyView() {
    mContentShown = false;
    mProgressContainer = mContentContainer = null;
    super.onDestroyView();
}


public void setContentShown(boolean shown) {
    setContentShown(shown, true);
}

public void setContentShownNoAnimation(boolean shown) {
    setContentShown(shown, false);
}

private void setContentShown(boolean shown, boolean animate) {

    mProgressContainer = getView().findViewById(INTERNAL_PROGRESS_CONTAINER_ID);
    mContentContainer = getView().findViewById(INTERNAL_CONTENT_CONTAINER_ID);

    if (mContentShown == shown) {
        return;
    }
    mContentShown = shown;
    if (shown) {
        if (animate) {
            mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_out));
            mContentContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_in));
        } else {
            mProgressContainer.clearAnimation();
            mContentContainer.clearAnimation();
        }
        mProgressContainer.setVisibility(View.GONE);
        mContentContainer.setVisibility(View.VISIBLE);
    } else {
        if (animate) {
            mProgressContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_in));
            mContentContainer.startAnimation(AnimationUtils.loadAnimation(
                    getActivity(), android.R.anim.fade_out));
        } else {
            mProgressContainer.clearAnimation();
            mContentContainer.clearAnimation();
        }
        mProgressContainer.setVisibility(View.VISIBLE);
        mContentContainer.setVisibility(View.GONE);
    }
}

我将此用于我异步填充的片段。要使用它,请不要覆盖onCreateView()。相反,在onStart()中膨胀你的视图并用它调用setView()。片段将以加载微调器开始。完成填充视图以删除加载动画并显示内容后,调用setContentShow(true)。