如何显示“正在加载...”检索 ListView 项目时的文本

发布于 2024-09-11 02:09:50 字数 614 浏览 8 评论 0原文

还有一些其他应用程序可以执行此操作,例如 Twitter、Facebook,甚至是 Android Market 等本机应用程序。当您想要显示从互联网检索到的项目列表时,这看起来像是向用户显示有关正在进行的操作的一些通知的标准方法。这是一个白色背景屏幕,带有动画旋转轮和“正在加载...”文本。

有人知道该怎么做吗?

我已经能够用这段代码做类似的事情,但我还不太喜欢它。仍在进行中:

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

   <TextView android:id="@android:id/loading" 
       android:background="@color/white"
       android:layout_width="fill_parent"     
       android:layout_height="fill_parent" 
       android:gravity="center" android:text="Loading..." />

There are some others applications doing this, like Twitter, Facebook, or even native applications such as Android Market. When you want to display a list of items retrieved from the internet, this looks like a standard way for displaying the user some notification about action in progress. This is a white background screen with an animated spinning wheel and a "Loading..." text.

Does somebody know how to do this?

I've been able to do something similar with this code, but I don't like it too much yet. Still work in progress:

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

   <TextView android:id="@android:id/loading" 
       android:background="@color/white"
       android:layout_width="fill_parent"     
       android:layout_height="fill_parent" 
       android:gravity="center" android:text="Loading..." />

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

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

发布评论

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

评论(3

2024-09-18 02:09:50

这是在 AsyncTask (智能后台)的帮助下完成的thread) 和 ProgressDialog

当 AsyncTask 启动时,我们提出一个进度对话框处于不确定状态,任务完成后我们将关闭该对话框。

示例代码
适配器在此示例中执行的操作并不重要,更重要的是要了解您需要使用 AsyncTask 来显示进度对话框。

private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected ContactsListCursorAdapter doInBackground(Void... params) {
        cur1 = objItem.getContacts();
        startManagingCursor(cur1);

        adapter1 = new ContactsListCursorAdapter (viewContacts.this,
                R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {});

        return adapter1;
    }

    protected void onPostExecute(ContactsListCursorAdapter result) {
        list.setAdapter(result);
        dialog.dismiss();
    }
}

That's done with the help of AsyncTask (an intelligent backround thread) and ProgressDialog

When the AsyncTask starts we reaise a progressdialog with indeterminate state, once the task is finished we dismiss the dialog.

Example code
What the adapter does in this example is not important, more important to understand that you need to use AsyncTask to display a dialog for the progress.

private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected ContactsListCursorAdapter doInBackground(Void... params) {
        cur1 = objItem.getContacts();
        startManagingCursor(cur1);

        adapter1 = new ContactsListCursorAdapter (viewContacts.this,
                R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {});

        return adapter1;
    }

    protected void onPostExecute(ContactsListCursorAdapter result) {
        list.setAdapter(result);
        dialog.dismiss();
    }
}
心作怪 2024-09-18 02:09:50

当用户滚动到底部时,适配器的 getView 应返回一个带有“正在加载..”文本和旋转进度的视图。同时,应启动 AsyncTask 或后台线程来下载新的内容块。当内容准备就绪时,调用adapter.notifyDatasetChanged(),并且ListView重新显示包括新项目在内的所有内容。

所以“正在加载...”消息只是 ListView 中的最后一项。

When user scrolls to the bottom your adapter's getView should return a view with "Loading.." text and spinning progress. At the same time AsyncTask or background thread should be started that downloads a new chunk of content. When content is ready adapter.notifyDatasetChanged() is called and ListView redisplays all content including the new items.

So "Loading..." message is just a last item in the ListView.

葵雨 2024-09-18 02:09:50

我不知道它是否完全符合您的要求,简单的方法是将加载文本最初设置为 android-empty-id-view

 <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Loading... Please Wait"
        android:textSize="20dip" />

,如果没有找到列表项,则将 android-empty-id-view 设置为不同的消息异步回调

((TextView) findViewById(android.R.id.empty)).setText("Your List is empty");

它将向用户提供足够的指示,表明某些事情正在进行中..即列表正在加载

I don't know whether it will completely fits your requirement, simple way is just set the loading text initially to android-empty-id-view like

 <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Loading... Please Wait"
        android:textSize="20dip" />

and if no list items found then set android-empty-id-view to different message in async callback

((TextView) findViewById(android.R.id.empty)).setText("Your List is empty");

it will give enough indication to user that something is progressing..i.e list is loading

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