【问题标题】:Implementing a progressDialog实现进度对话框
【发布时间】:2014-09-02 09:20:04
【问题描述】:

我想在调用我的类时实现一个 progessDialog。我知道如何为使用 AsyncTask 的片段类实现 progressDialog。但是这里我有一个不使用 asyncTask 的片段类。

这个片段的作用是在调用时显示一些新闻细节。我想在新闻加载时显示progressDialog,并在新闻加载并准备好显示时停止progressDialog。我该怎么做?我是新手,请帮帮我

我的片段类

package com.fortuna.cinemalk;

public class NewsDetailFragment extends Fragment {

    private ViewPager view1;
    private ViewPageAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.newsdetail_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        view1 = (ViewPager) view.findViewById(R.id.listviewpager);

        adapter = new ViewPageAdapter(commonVariable.getNewsDescription(),
                Element.NEWS_DETAIL.getType(), activity);

        view1.setAdapter(adapter);



        return view;
    }

}

【问题讨论】:

  • 如何加载你使用的新闻和 Webservie 来加载新的

标签: android android-fragments progressdialog


【解决方案1】:

你需要这样:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

      <ProgressBar
         android:id="@+id/progressBar1"
         style="?android:attr/progressBarStyleLarge"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:indeterminate="true" />

         </RelativeLayout>

加载数据后,只需隐藏此进度条,如

      progressBarObj.setVisibility(false);

【讨论】:

  • 这里的progresBarObj是什么?
【解决方案2】:

对我来说最好的选择是创建一个修改进度组件的自定义处理程序:

public final class ProgressBarComponentViewHandler extends Handler {

    private ProgressBar progressLayout;

    private Activity mActivity;

    public LoadingComponentViewHandler(Activity activity, ProgressBar progressLayout) {
        this.progressLayout = progressLayout;
        this.mActivity=activity
    }

    public void update(int status){
        Message m = new Message();
        Bundle bundle = new Bundle();
        bundle.putInt("status", status);
        m.setData(bundle);
        super.sendMessage(m);
    }

    public void handleMessage(Message msg) {
        Bundle b = msg.getData();
        Integer status = b.getInt("status", 0);
        progressLayout.setProgress(status);
    }
}

在你的片段中:

loadingComponentView = (ProgressBar) yourRootView.findViewById( R.id.progress_bar_component_view );

progressBarDialogHandler = new ProgressBarViewHandler(getActivity(), loadingComponentView);

最后,更新你的观点:

progressBarDialogHandler.update(80);//To set 80% of progressbar.

【讨论】:

    【解决方案3】:

    您可以从 AsyncTask 或 Service 中显示 ProgressDialog 或者您可以在静态片段中初始化 ProgressDialog 并从 AsyncTask 或服务中调用对话框,但您需要检查 progressDialog!=null

    (因为加载新闻时我需要关闭进度)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2018-12-27
      相关资源
      最近更新 更多