Android ProgressBar 的样式类似于 SwipeRefreshLayout 中的进度视图

Android ProgressBar styled like progress view in SwipeRefreshLayout

我在 Android 应用程序中使用 android.support.v4.widget.SwipeRefreshLayout。它包装了一个 ListView。列表视图的内容是从服务器下载的。

当用户向下滑动以从服务器重新加载数据时,会显示进度视图。进度视图看起来像一块圆圈,在动画过程中会变大和变小。这个progress view的样式好像不能定制太多。但是,我很喜欢它的内置样式。

我还在初始数据加载期间显示相同的进度动画。这可以通过调用 mySwipeRefreshLayout.setRefreshing(true) 来实现。那也完全可以。

但是,我想在整个应用程序中显示相同的进度指示。考虑例如另一个 activity 看起来像带有提交按钮的表单。这种形式 activity 中既没有 ListView 也没有 SwipeRefreshLayout。当提交的数据传输到服务器时,应该显示一些进度指示。我想用与 SwipeRefreshLayout 中相同的动画显示进度条。

是否有一种简单明了的方法可以让 SwipeRefreshLayout 和不包含任何列表视图和刷新布局且不支持任何滑动的表单 activity 具有相同的进度指示器手势?

谢谢。

However, I would like to show the same progress indication through the whole app. Consider e.g. another activity that looks like a form with submit button. There is neither a ListView nor a SwipeRefreshLayout in this form activity. Some progress indication should be displayed while the submitted data are transferred to the server. I want to show a progress bar with the same animation like in SwipeRefreshLayout.

是的,您可以使用 SwipeRefreshLayout 在单击按钮时显示为 ProgressDialog。检查下面。

我已经在布局文件中添加了 SwipeRefreshLayout,但仍然没有 ListViewScrollView。就像下面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="niu.com.djandroid.jdroid.androidstack.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/activity_main_swipe_refresh_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />

            <SeekBar
                android:id="@+id/seekBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="54dp" />

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button" />
        </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>


</LinearLayout>

现在在我的 activity 中我使用 AsyncTask 来显示 SwipeRefreshLayout。还可以使用 mSwipeRefreshLayout.setOnRefreshListener(null) 停止滑动手势。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.activity_main_swipe_refresh_layout);


        mSwipeRefreshLayout.setOnRefreshListener(null);

        ((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // mSwipeRefreshLayout.setRefreshing(true);
                // call AsyncTask
                new LongOperation().execute("");
            }
        });

    }

    private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    Thread.interrupted();
                }
            }
            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            // stop mSwipeRefreshLayout
            mSwipeRefreshLayout.setRefreshing(false);
        }

        @Override
        protected void onPreExecute() {
            // start mSwipeRefreshLayout
            mSwipeRefreshLayout.setRefreshing(true);
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }

编辑:15/02/20 4 年后,android developer site

有了很好的解释
mSwipeRefreshLayout.setOnRefreshListener(null) 

对我不起作用,所以我做了这个方法来启用和禁用刷新。

 private void setToRefresh(boolean refresh) {
    if (refresh) {
        mSwipeRefreshLayout.setEnabled(true);
        mSwipeRefreshLayout.setRefreshing(true);
    } else {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setEnabled(false);
    }
}