【Android-SwipeRefreshLayout控件】下拉刷新

Android自带API ,V4包下面的下拉刷新控件

android.support.v4.widget.SwipeRefreshLayout

SwipeRefreshLayout只能包含一个控件

布局例子:

  <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"> </ListView> </android.support.v4.widget.SwipeRefreshLayout>

应用例子:

/**
* SwipeRefreshLayout控件学习
*/ public class RefreshActicity extends AppCompatActivity { SwipeRefreshLayout swipeRefreshLayout;
ListView listView;
List<Product> list = new ArrayList<>();
ProductAdapter adapter; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_refresh); swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
listView = findViewById(R.id.listView); //获取数据
initData(); //设置适配器
adapter = new ProductAdapter(this, list);
listView.setAdapter(adapter); //设置下拉进度的背景颜色,默认是白色
swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
//设置下拉进度的肢体颜色
swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
//下拉时触发下拉动画,动画完毕后回调这个方法
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//开始刷新,设置当前为刷新状态(刷新状态会屏蔽掉下拉事件)
swipeRefreshLayout.setRefreshing(true);
//耗时操作,比如联网获取数据
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
list.clear();
initData();
adapter.notifyDataSetChanged();
//加载完数据,设置为不刷新状态,将下拉进度收起来
swipeRefreshLayout.setRefreshing(false); }
}, 2000);
}
}); } /**
* 生成10条List数据
*/
private void initData() {
for (int i = 1; i <= 10; i++) {
Random random = new Random();
Product product = new Product("100" + i, "testdata", random.nextInt(100));
list.add(product);
}
}
}
上一篇:C#进阶系列——WebApi 路由机制剖析:你准备好了吗?


下一篇:CSS3学习笔记--transform基于原始数据(旋转木马实例)