2

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

谢谢。

4

1 回答 1

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) 以删除加载动画并显示您的内容。

于 2013-02-08T23:39:09.257 回答