介绍
这是google提供的控件,在5.0后开始支持的下拉控件。
需要使用它只要通过4个步骤就可以了。
步骤:
1. 布局中使用SwipeRefreshLayout
2. SwipeRefreshLayout只能包含一个child
child必须为可以滚动的View,如:Listview,ScrollView
3. 实例化SwipeRefreshLayout的Activity,要实现OnRefreshListener接口
- 实现onRefresh()方法
- 为SwipeRefreshLayout设置RefreshListener
4. SwipeRefreshLayout调用setRefreshing(true)方法,以显示刷新过程动画
代码
public class SwipRefreshActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipeRefreshLayout = null;
TextView refreshView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swip_refresh);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.SwipeRefreshLayout);//获取刷新布局
swipeRefreshLayout.setOnRefreshListener(this);//设置监听器
refreshView = (TextView) findViewById(R.id.SwipeRefreshTextView);
}
@Override
public void onRefresh() {//相应刷新手势
swipeRefreshLayout.setRefreshing(true);//开启刷新手势
(new Handler()).postDelayed(new Runnable() {//延时3秒执行的线程
@Override
public void run() {
swipeRefreshLayout.setRefreshing(false);//停止刷新手势
//得到-99~99的随机整数
Random random = new Random(); //随机
refreshView.setText((random.nextInt())%100+""); //设置
//得到1~100的整数
refreshView.setText((int)(Math.random()*100+1)+“”); //设置随机数
}
}, 3000);
}
}