Android:将 ProgressDialog 限制到子视图

发布于 2024-11-19 23:10:47 字数 392 浏览 2 评论 0原文

我想显示一个 ProgressDialog 但让它限制其对子视图的可见性。据我所知,ProgressDialog 占据了整个屏幕并禁止访问屏幕的任何部分,直到 ProgressDialog 被关闭。

我滚动了自己的选项卡控件,当一个选项卡下的内容忙于执行一项冗长的任务时,我希望 ProgressDialog 仅显示在该选项卡的内容区域中。当另一个选项卡正忙于执行其任务并且显示其 ProgressDialog 时,用户始终可以选择另一个选项卡。

我怀疑这是不可能的,并且必须推出我自己的 ProgressDialog。但是,由于创建 ProgressDialog 中的第一个参数是上下文,并且通常设置为“this”,所以我想知道是否可以从子视图中检索上下文,但仅限于子视图。视图上的 getContext 似乎获取应用程序本身的上下文。

I want to display a ProgressDialog but have it limit its visibility to a child view. As far as I can tell, the ProgressDialog takes up the entire screen and disables accessing any part of the screen until the ProgressDialog is dismissed.

I rolled my own tab control and when the content under one tab is busy doing a lengthy task, I want the ProgressDialog to show only within the content area for that tab. The user can always select another tab while the other tab is busy doing its task and its ProgressDialog is showing.

I suspect that it is not possible and will have to roll my own ProgressDialog. However, since the first parameter in creating a ProgressDialog is the context, and is usually set to "this", I was wondering if perhaps the context can be retrieved from a child view but is confined only to the child view. The getContext on a View seems to get the context of the app itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久随 2024-11-26 23:10:47

你可以通过一种棘手的方式实现这一点,tat对我有用..

在指定选项卡内容的布局文件中,将进度条设置为:

<?xml version="1.0" encoding="utf-8"?>
<Layout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <ListView
            android:id="@+id/updatesList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>

        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />
    </FrameLayout>

</LinearLayout>

并在活动tat中显示选项卡内容,显示列表或进度条(一次只有一个)通过:

if(list != null){
            progressBar.setVisibility(View.INVISIBLE);
            updatesListView.setVisibility(View.VISIBLE);
                        adapter = new YourListAdapter(this, list);
            updatesListView.setAdapter(adapter);
        } else{
            progressBar.setVisibility(View.VISIBLE);
            updatesListView.setVisibility(View.INVISIBLE);
            new Thread(downloadListContent).start();
        }

并在下载线程完成时,通过处理程序或 runOnUIThread,使可见性反转为

progressBar.setVisibility(View.INVISIBLE);
updatesListView.setVisibility(View.VISIBLE);

you can achieve this in a tricky way, tat worked for me..

In the layout file, which specifies the tab content, put a progress bar as :

<?xml version="1.0" encoding="utf-8"?>
<Layout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <ListView
            android:id="@+id/updatesList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>

        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />
    </FrameLayout>

</LinearLayout>

and in the activity tat displays the tab content, display either the list or progress bar(only one at a time) by :

if(list != null){
            progressBar.setVisibility(View.INVISIBLE);
            updatesListView.setVisibility(View.VISIBLE);
                        adapter = new YourListAdapter(this, list);
            updatesListView.setAdapter(adapter);
        } else{
            progressBar.setVisibility(View.VISIBLE);
            updatesListView.setVisibility(View.INVISIBLE);
            new Thread(downloadListContent).start();
        }

and on completion of download thread, via a handler or runOnUIThread, make the visiblity to reverse as

progressBar.setVisibility(View.INVISIBLE);
updatesListView.setVisibility(View.VISIBLE);
甜中书 2024-11-26 23:10:47

考虑到 Android 在任何给定时间在前台只有一个 Activity,我相信这对于正常的 Activity 来说可能是不可能的(尽管可能是错误的)。但我想 Fragments 可能就是您正在寻找的东西?

Considering that Android only has one Activity in the foreground at any given time, I believe this may not be possible with normal Activities (may be mistaken though). But I imagine Fragments may be what you are looking for?

堇年纸鸢 2024-11-26 23:10:47

基本的 ProgressDialog 控制了您的线程,因此不允许您按照您的意愿进行操作。

一个常规的小部件(而不是对话框),类似于普通的进度小部件或您自己滚动的任何东西都可以做到这一点。

如果选项卡上已经有其他繁忙内容并且您想要“覆盖”该内容,则可以使用 Fragment 相反。

最容易实现的很可能是一个覆盖选项卡内容并隐藏其他元素的小部件。代码中最好的方法是使用后者。

The base ProgressDialog takes control of your thread and thus does not allow you to do as you want.

A regular Widget (instead of a Dialog), similar to the normal Progress one or anything you roll on your own can do the trick.

If you already have other content on the tab that is busy and you want to 'cover' that, you can use a Fragment instead..

Easiest to implement would most likely be a Widget that covers your tab content and hiding the other elements.. Nicest in code is using the latter..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文