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

问题描述 投票:10回答:2

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

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

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

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

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

谢谢。

android progress-bar android-styles
2个回答
10
投票

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

是的,你可以使用SwipeRefreshLayout显示像按钮点击时ProgressDialog。检查下面。

enter image description here

我在我的布局文件中添加了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) {
        }
    }

编辑:

你可以使用Github的SwipeRefreshLayoutBottom库。和swiperefreshlayout一样。检查下面

enter image description here


0
投票
mSwipeRefreshLayout.setOnRefreshListener(null) 

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

 private void setToRefresh(boolean refresh) {
    if (refresh) {
        mSwipeRefreshLayout.setEnabled(true);
        mSwipeRefreshLayout.setRefreshing(true);
    } else {
        mSwipeRefreshLayout.setRefreshing(false);
        mSwipeRefreshLayout.setEnabled(false);
    }
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.