更改片段时显示 ProgressBar - Android

Posted

技术标签:

【中文标题】更改片段时显示 ProgressBar - Android【英文标题】:Show ProgressBar when changing fragment - Android 【发布时间】:2016-11-15 19:20:37 【问题描述】:

我的应用程序中有一个片段,它执行大量后台工作,因此需要很长时间才能加载。我想在我的 Main Activity 中显示一个 ProgressBar,并在加载片段后将其隐藏。

问题是,进度条不显示,或者卡住了,因为整个 UI 冻结了。

如何在片段加载时显示 ProgressBar? 谢谢。

编辑:片段内的代码:

  public override void OnCreate(Bundle savedInstanceState)
    
        base.OnCreate(savedInstanceState);
        // Create your fragment here

        fragments = new Fragment[]
       
           new HalachotFragment(),
           new BooksFragment(),
       ;

        var titles = CharSequence.ArrayFromStringArray(new[]
       
                "הלכות",
                "ספרים",
        );

        tabsAdapter = new TabsFragmentPagerAdapter(ChildFragmentManager, fragments, titles);

    

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    
        // Use this to return your custom view for this Fragment

        var view = inflater.Inflate(Resource.Layout.HalachaSection, container, false);

        //var progressBar = Activity.FindViewById<ProgressBar>(Resource.Id.progressBar);

        halachotPager = view.FindViewById<ViewPager>(Resource.Id.halachotPager);

        halachotPager.Adapter = tabsAdapter;
        halachotPager.OffscreenPageLimit = 2;
        halachotPager.SetCurrentItem(1, true);
        // Bind the tabs to the ViewPager
        tabs = Activity.FindViewById<TabLayout>(Resource.Id.tabs);

        tabs.SetupWithViewPager(halachotPager);
        //var toolbar = Activity.FindViewById<android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
        tabs.Visibility = ViewStates.Visible;

        //progressBar.Visibility = ViewStates.Gone;
        return view;

    

书片段:

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
        View view = inflater.Inflate(Resource.Layout.Books, container, false);

        mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.booksRecyclerView);
        mRecyclerView.NestedScrollingEnabled = false;

        mBooks = new List<Book>();
        ImageView cover = new ImageView(Activity);
        //cover = view.FindViewById<ImageView>(Resource.Id.coverImage);
        cover.SetImageResource(Resource.Drawable.Torat_Hamachane1);
        mBooks.Add(new Book()  Title = "תורת המחנה א' - הלכות יום ויום", Cover = cover, imageResourceId = Resource.Drawable.Torat_Hamachane1); 

        //Create our layout manager
        mLayoutManager = new GridLayoutManager(Activity, 2);
        mLayoutManager.AutoMeasureEnabled = true;
        //mRecyclerView.SetForegroundGravity(GravityFlags.CenterHorizontal);
        mRecyclerView.SetLayoutManager(mLayoutManager);
        SimpleItemAnimator x = null;

        mRecyclerView.SetItemAnimator(x);
        mAdapter = new RecyclerAdapter(mBooks, mRecyclerView, Activity);
        mRecyclerView.SetAdapter(mAdapter);
        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.kitzurTHM);
        mBooks.Add(new Book()  Title = "קיצור תורת המחנה - הלכות יום ויום ושבת", Cover = cover, imageResourceId = Resource.Drawable.kitzurTHM, Content = "KitzurTHM/KitzurTHMFull.html" );

        cover = new ImageView(Activity);
        ImageView newCover = new ImageView(Activity);
        newCover.SetImageResource(Resource.Drawable.halkasPurim);
        mBooks.Add(new Book()  Title = "הלכה כסדרה - הלכות פורים", Cover = newCover, imageResourceId = Resource.Drawable.halkasPurim );

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.halkasPesach);
        mBooks.Add(new Book()  Title = "הלכה כסדרה - הלכות פסח", Cover = cover, imageResourceId= Resource.Drawable.halkasPesach );

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.netzach);
        mBooks.Add(new Book()  Title = "שו\"ת נצח יהודה", Cover = cover , imageResourceId = Resource.Drawable.netzach);

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.tankim);
        mBooks.Add(new Book()  Title = "טיפול בטנקים בשבת", Cover = cover , imageResourceId = Resource.Drawable.tankim);

        cover = new ImageView(Activity);
        cover.SetImageResource(Resource.Drawable.tipulBeChalalim);
        mBooks.Add(new Book()  Title = "טיפול בחללים בשבת", Cover = cover , imageResourceId = Resource.Drawable.tipulBeChalalim, Content = "TipulBechalalimShabbat.html" );

        Refresh();
        return view;

    

HalachotFragment 解析一个文档,这也使得它需要很长时间。

【问题讨论】:

在此处发布您的代码 您是否使用 asynctask 进行后台工作? 因为整个 UI 都冻结了。意味着不在工作线程中工作?显示部分代码 哪个代码?碎片里面?还是交易?? @amitairos: 由于 Fragment 加载需要时间而产生的代码 【参考方案1】:

您现在可以在主活动中使用 ProgressBar 并将 Progressbar 对象设为静态,只要您更改片段显示该进度条并且当您的片段加载时隐藏该进度条。

【讨论】:

对不起,我不太明白你的意思。【参考方案2】:

每当 Fragment 发生变化时,尝试runOnUiThread 作为以下代码,

runOnUiThread(new Runnable() 

    @Override
    public void run() 
        // TODO Auto-generated method stub

        ProgressDialog pd = new ProgressDialog(Activity.this);
        pd.setMessage("Testing...");
        pd.show();

    
); 

如果你想隐藏ProgressDialog 表示使用,

pd.hide();

希望这对你有帮助。

【讨论】:

谢谢。您能否看看我的代码并告诉我如何使用 Async 使其不会冻结 UI?另外,我应该把这段代码放在哪里?分片交易后?【参考方案3】:

首先你需要发布一些代码。

如何在片段加载时显示 ProgressBar?

当您从活动中调用片段时,将调用片段的onAttach() 方法。因此,那时您需要在该方法中获取您的活动对象引用。喜欢,

MainActivity mainActivity; // this should be global
@Override
public void onAttach(Activity activity) 
    // TODO Auto-generated method stub
    super.onAttach(activity);
    mainActivity = (MainActivity)activity;

因此,使用活动对象,您可以访问进度条对象并将其隐藏在 setUserVisibleHint() 方法中,在该方法中片段将被完全加载。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) 
    // TODO Auto-generated method stub
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser) 
      // hide progress bar
    
 

【讨论】:

谢谢。能否请您看一下我的代码并告诉我如何使用 Async 使其不会冻结 UI?

以上是关于更改片段时显示 ProgressBar - Android的主要内容,如果未能解决你的问题,请参考以下文章

添加新片段时显示的按钮

更改'当我按下Ctrl键时显示指针的位置'来自代码的Windows设置

从库中复制文件时显示 ProgressDialog

AlertDialog 更改片段中的背景颜色 [重复]

Android - 如何更改导航抽屉中的片段

加载 iframe 并更改其源时显示加载符号?