I use android.support.v4.widget.SwipeRefreshLayout
在我的 Android 应用程序中。它包裹着一个ListView
。列表视图的内容是从服务器下载的。
当用户向下滑动以从服务器重新加载数据时,会显示进度视图。进度视图看起来像一个在动画过程中不断增大和缩小的圆圈。看来这个进度视图的样式不能自定义太多。不过,我对它的内置风格很满意。
我还在初始数据加载期间显示相同的进度动画。这可以通过调用来实现mySwipeRefreshLayout.setRefreshing(true)
。这也完全没问题。
但是,我想通过整个应用程序显示相同的进度指示。考虑例如另一个看起来像带有提交按钮的表单的活动。既没有一个ListView
nor a SwipeRefreshLayout
在这种形式的活动中。当提交的数据传输到服务器时,应该显示一些进度指示。我想显示一个进度条,其动画与 SwipeRefreshLayout 中的动画相同。
有没有一种简单干净的方法可以让两个应用程序具有相同的进度指示器SwipeRefreshLayout
以及不包含任何列表视图和刷新布局且不支持任何滑动手势的表单活动?
Thanks.
但是,我想通过以下方式显示相同的进度指示
整个应用程序。考虑例如另一项看起来像表单的活动
提交按钮。中既没有 ListView 也没有 SwipeRefreshLayout
这种形式的活动。应该显示一些进度指示
提交的数据被传输到服务器。我想展示一个
进度条具有与 SwipeRefreshLayout 相同的动画。
是的,你可以使用SwipeRefreshLayout
表现出像ProgressDialog
当按钮点击时。检查下面。
我已经添加了SwipeRefreshLayout
在我的布局文件中仍然没有ListView
or ScrollView
。就像下面一样
<?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年后,这里有很好的解释安卓开发者网站 https://developer.android.com/training/swipe/add-swipe-interface
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)