SwipeRefrshLayout是Google官方更新的一个Widget,可以实现下拉刷新的效果。该控件集成自ViewGroup在support-v4兼容包下,不过我们需要升级supportlibrary的版本到19.1以上。基本使用的方法如下:
- setOnRefreshListener(OnRefreshListener):添加下拉刷新监听器
- setRefreshing(boolean):显示或者隐藏刷新进度条
- isRefreshing():检查是否处于刷新状态
- setColorSchemeResources():设置进度条的颜色主题,最多设置四种,以前的setColorScheme()方法已经弃用了。
- 布局代码:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/refresh" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="随机产生1到100的整数:" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout> - 显示代码:
- public class MainActivity extends Activity
{
SwipeRefreshLayout swipeRefreshLayout;
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);
mTextView = (TextView) findViewById(R.id.tv1);
swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener()
{
@Override
public void onRefresh()
{
// TODO Auto-generated method stub
swipeRefreshLayout.setRefreshing(true);
// swipeRefreshLayout.setColorSchemeResources();
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
// TODO Auto-generated method stub
int mat = (int) (Math.random() * 100 + 1);
swipeRefreshLayout.setRefreshing(false);
mTextView.setText(String.valueOf(mat));
}
}, 3000);
}
});
}
}
- 问题:动画样式在support library不同时有两种样式。一种是圆形,另一种是进度条(好丑)。想要是圆形,就把support升级到最新。