gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:32:26 24 4
gpt4 key购买 nike

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

谢谢。

最佳答案

我通过为类似于 ListFragment 的 fragment 类编写包装器来完成此操作.

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

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

关于android - 如何在 ListActivity 和 Fragment 中嵌入加载进度指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14637460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com