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

ggo*_*eze 10 android listview loading uiview

还有一些其他应用程序正在这样做,如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..." />
Run Code Online (Sandbox Code Playgroud)

Pen*_*m10 7

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

当AsyncTask启动时,我们会重新确定一个具有不确定状态的progressdialog,一旦任务完成,我们就会关闭对话框.

示例代码
适配器在此示例中的作用并不重要,更重要的是要了解您需要使用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();
    }
}
Run Code Online (Sandbox Code Playgroud)


Fed*_*dor 5

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

所以"Loading ..."消息只是ListView中的最后一项.