【问题标题】:Android progress bar in ListView loadingListView加载中的Android进度条
【发布时间】:2013-03-04 12:24:04
【问题描述】:

我想在我的应用程序ListView 中添加LinearLayout,当正在加载而不是列表时有进度条。这类似于系统应用程序,您可以在其中管理您的应用程序。

我找到了一些关于它的信息,但没有一个是好的。一个,似乎最好的说法是创建两个布局(一个带有进度条,另一个带有 ListView),然后在onPreExecute()(我为此使用AsyncTask)显示进度条。然后在onPostExecute() 中隐藏它,仅此而已。

我试图实现它,但我遇到了 ScrapView 错误,并且没有更多信息,如何实现它。

所以,我的问题是,你会建议我怎么做? 我不想要任何进度对话框。

编辑:

这是我的代码片段,展示了我到目前为止所做的事情:

TestActivity.java

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mylist_layout);
            wordList = (ListView) findViewById(R.id.wordsList);

            //...
    }

        private class AsyncDBDownload extends AsyncTask<String, Integer, String> {
                LinearLayout linlaHeaderProgress = (LinearLayout) findViewById(R.id.linlaHeaderProgress);

                @Override
                protected String doInBackground(String... params) {
                    try {
                        refreshList();//upload of contetn and set of adapter
                    } catch (Exception ex) {
                        Log.e(TAG, ex.toString());
                    }
                    return null;
                }

                @Override
                protected void onPostExecute(String result) {
                    linlaHeaderProgress.setVisibility(View.GONE);
                    wordList.setVisibility(View.VISIBLE);
                }

                @Override
                protected void onPreExecute() {
                    linlaHeaderProgress.setVisibility(View.VISIBLE);
                }
            }

    private void refreshList() {
            //List content download into Cursor - adapterCursor 
            //...

            wordList.setAdapter(adapterCursor);
        }

word_list.xml

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

    <LinearLayout
        android:id="@+id/linlaHeaderProgress"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone" >

        <ProgressBar
            android:id="@+id/pbHeaderProgress"
            style="@android:style/Widget.ProgressBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>
    </LinearLayout>

    <ListView
        android:id="@+id/wordsList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fastScrollEnabled="true" >
    </ListView>

两个额外的东西:

  • onResume() 中调用了要执行的AsyncTask;
  • mylist_layout.xml 中应列出的位置包含包含在此布局中的包含word_list.xml 布局

其他:

  • 我不想使用进度对话框,因为它不符合我的需要,而且我觉得它看起来不适合我的目的。我想替换列表,不再做广告。
  • ListFragment 看起来还不错,但我认为可能还有其他更好或更简单的解决方案来实现我想要的。

我正在开发兼容 Android 2.2/2.3 的应用程序,我忘了提及,所以如果 android.support.v4.app.Fragment 中没有某些东西,我就不会使用它。

【问题讨论】:

  • 请分享你所做的...
  • 你为什么不想使用进度对话框?
  • @AmeerMoaaviah 添加代码

标签: android android-listview android-asynctask


【解决方案1】:

您可以使用ListFragment。使用 setListShown()true/false 来隐藏/显示加载程序而不是列表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-20
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2012-06-29
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多