Android 如何在ListActivity和片段中嵌入加载进度指示器

Android 如何在ListActivity和片段中嵌入加载进度指示器,android,android-fragments,android-activity,Android,Android Fragments,Android Activity,有没有一种方法可以像调用ListFragment一样将加载进度指示器嵌入ListActivity或片段中?我可以使用加载对话框,但我希望保持一致,因为我已经在其他ListFragments中使用了setListShowed() 谢谢。我通过为类似的fragment类编写一个包装器来实现这一点 我将此用于异步填充的片段。若要使用它,请不要重写onCreateView()。相反,在onStart()中膨胀视图并使用它调用setView()。片段将以加载微调器开始。填充完视图后调用setContent

有没有一种方法可以像调用ListFragment一样将加载进度指示器嵌入ListActivity或片段中?我可以使用加载对话框,但我希望保持一致,因为我已经在其他ListFragments中使用了setListShowed()


谢谢。

我通过为类似的fragment类编写一个包装器来实现这一点

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

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);
    }
}