Android ProgressBar的样式类似于SwipeRefreshLayout中的进度视图 [英] Android ProgressBar styled like progress view in SwipeRefreshLayout

查看:61
本文介绍了Android ProgressBar的样式类似于SwipeRefreshLayout中的进度视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use android.support.v4.widget.SwipeRefreshLayout in my Android app. It wraps a ListView. The content of the list view is downloaded from a server.

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

A progress view is shown when user swipes down in order to reload data from server. The progress view looks like a piece of circle that grows and shrinks during the animation. It seems that the style of this progress view cannot be customized much. However, I am fine with its built-in style.

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

I also show the same progress animation during initial data loading. This can be achieved by calling mySwipeRefreshLayout.setRefreshing(true). That's perfectly OK too.

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

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和不包含任何列表视图和刷新布局且不支持任何滑动手势的表单活动,是否有一种简单而干净的方法来具有相同的进度指示器?

Is there a simple and clean way to have the same progress indicator for both a SwipeRefreshLayout and a form activity that does not contain any list view and refresh layout and does not support any swipe gesture?

谢谢.

推荐答案

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

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.

是的,单击按钮时,可以使用SwipeRefreshLayoutProgressDialog一样显示.检查下面.

yes you can use SwipeRefreshLayout to show like ProgressDialog when button click. check below.

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

i have added SwipeRefreshLayout inside my layout file still there is not ListView or ScrollView. just like below

<?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)停止滑动手势.

now in my activity i used AsyncTask to show SwipeRefreshLayout. also use mSwipeRefreshLayout.setOnRefreshListener(null) to stop swipe gesture.

@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) {
        }
    }


编辑时间:20/02/15 4年后,这是 Android开发者网站上的绝佳解释


EDITED:15/02/20 After 4 year, Here is great explanation on android developer site

这篇关于Android ProgressBar的样式类似于SwipeRefreshLayout中的进度视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆