SwipeRefreshLayout: How to use
来源:互联网 发布:具体网络架构图 编辑:程序博客网 时间:2024/06/10 22:47
Using this new layout is really easy, but here it is a simple guide to make it work in a few seconds.
What do you need to know?
Almost nothing. Swipe refresh layout is a ViewGroup with the particularity that it can only hold one scrollable view as a children. It is basically a layout decorator that manages touch events and shows an indeterminate progress animation below the action bar when the user swipes down. The effect is similar to the one seen in Google Now app.
Methods are quite few:
- setOnRefreshListener(OnRefreshListener): adds a listener to let other parts of the code know when refreshing begins.
- setRefreshing(boolean): enables or disables progress visibility.
- isRefreshing(): checks whether the view is refreshing.
- setColorScheme(): it receive four different colors that will be used to colorize the animation.
The layout
As said before, you only need to decorate the swipable content (probable the whole layout) with this new layout. This view must be scrollable, such a ScrollView or a ListView. As a simple example:
<
android.support.v4.widget.SwipeRefreshLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:id
=
"@+id/swipe_container"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
ScrollView
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
TextView
android:text
=
"@string/hello_world"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_marginTop
=
"16dp"
android:gravity
=
"center"
/>
</
ScrollView
>
</
android.support.v4.widget.SwipeRefreshLayout
>
The code
We just need to get the layout, and assign some colours and the listener. The refreshing listener is a post delayed handler.
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(
this
);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
@Override
public
void
onRefresh() {
new
Handler().postDelayed(
new
Runnable() {
@Override
public
void
run() {
swipeLayout.setRefreshing(
false
);
}
},
5000
);
}
A convenient activity and a trick
In order to simplify the use of this layout, I created a SwipeRefreshActivity which adds this layout to any activity. It can by found at myGithub Gist.
As a trick, you may be found interesting to disable manual swipe gesture, maybe temporarily or because you only want to show progress animation programmatically. What you need to do is to use the methodsetEnabled() and set it to false.
Conclusion
Finally there is a standard way to create this common design pattern, and be sure we’ll begin to see it more and more in future apps and updates. Unfortunately I didn’t find a way to change the animation to make it similar to other Apps such as Gmail or Google+, but maybe some subclassing and overriding would do the trick. Keep you informed if I get into it.
- SwipeRefreshLayout: How to use
- How To Use DataGird
- how to use typedef
- how to use gz
- How to use chkconfig
- How to use ,,,,
- how to use dialog
- How to use UIDs
- How to use dmalloc
- How to use abld
- How to use BSTR
- How to use Ant
- how to use mstsclib
- how to use gcc
- how to use thread
- how to use ffmpeg
- How to use RDebug
- How to use regex
- 如何灵活运用PHP判断字符串的包含 PHP数组包含
- 解决Sql server分页时第二页以上查询结果不正常的问题
- iOS 交互式动画
- matlab转c
- 每日算法之十六:4sum
- SwipeRefreshLayout: How to use
- 百字3元的论文摘要,——我们要买来些什么
- vc++使用EmptyWorkingSet来清理程序的内存
- OCP 1Z0 051 126
- 三星南京研发中心2014暑期实习嵌入式开发面试笔试部分
- 机房收费系统集锦(一)---打开重复窗体
- 深入理解Activity的生存期(二)
- SPI接口调试
- 每天,每周,每月,每年程序员该做的事情