【问题标题】:Android ProgressBar styled like progress view in SwipeRefreshLayoutAndroid ProgressBar 样式类似于 SwipeRefreshLayout 中的进度视图
【发布时间】:2016-02-09 14:54:39
【问题描述】:

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

当用户向下滑动以从服务器重新加载数据时,会显示一个进度视图。进度视图看起来像一个在动画期间增长和缩小的圆圈。这个进度视图的样式好像不能自定义太多。不过,我对它的内置样式很好。

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

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

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

谢谢。

【问题讨论】:

    标签: android progress-bar android-styles


    【解决方案1】:

    但是,我想通过 整个应用程序。考虑例如另一个看起来像表单的活动 提交按钮。中既没有 ListView 也没有 SwipeRefreshLayout 这种形式的活动。应显示一些进度指示,同时 提交的数据被传输到服务器。我想展示一个 进度条与 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>
    

    现在在我的活动中,我使用 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这里有很好的解释

    【讨论】:

    • 感谢您提供复杂的示例 (+1)。我必须承认我懒得自己尝试这种方法。我必须用SwipeRefreshLayout 包装我的所有活动,只是为了“窃取”它的进度动画。我认为这是一个“肮脏”的解决方案。我希望可能有例如一种将一些样式设置为ProgressBar 的方法,使其看起来类似于SwipeRefreshLayout 的进度视图。
    • 不确定我是否会认为这是一个肮脏的解决方案,它很容易实现 webview 并且看起来比标准进度条更好。谢谢!
    • 在命令 mSwipeRefreshLayout.setRefreshing(false) 和 mSwipeRefreshLayout.setEnable(false) 之后禁用 SwipeRefreshLayout 也很重要。为了在滑动时停用动画。当然,您必须在再次调用 mSwipeRefreshLayout.setRefreshing(true) 之前重新激活 SwipeRefreshLayout。
    【解决方案2】:
    mSwipeRefreshLayout.setOnRefreshListener(null) 
    

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

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

    【讨论】:

      猜你喜欢
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      相关资源
      最近更新 更多