SwipeRefreshLayout(官方下拉刷新)
2018-08-04 本文已影响0人
大灰狼zz
在布局文件中添加
<?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"
tools:context="jun.com.module_home.activity.SwipeRefreshLayoutActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
在需要使用的地方添加如下代码
public class SwipeRefreshLayoutActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout swipeLayout;
private RecyclerView recyclerView;
private TextAdapter adapter;
private ArrayList<String> stringList;
@Override
public int getLayoutId() {
return R.layout.home_activity_swiperefreshlayout;
}
@Override
public void initData() {
stringList = new ArrayList<String>();
for (int i = 'A'; i < 'z'; i++) {
stringList.add("" + (char) i);
}
}
@Override
public void initView() {
swipeLayout = findView(R.id.swipeRefreshLayout);
recyclerView = findView(R.id.recyclerView);
//为SwipeRefreshLayout设置监听事件
swipeLayout.setOnRefreshListener(this);
//为SwipeRefreshLayout设置刷新时的颜色变化,最多可以设置4种
swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
//RecyclerView相关设置
recyclerView = findViewById(R.id.recyclerView);
// 给每个item添加分割线
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));//垂直分割线
// recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL));//水平分割线
// 如果可以确定每个item的高度是固定的,设置这个选项可以提高性能
recyclerView.setHasFixedSize(true);
// 设置item增加和移除的动画
recyclerView.setItemAnimator(new DefaultItemAnimator());
// 设置布局管理器
//线性
LinearLayoutManager layoutManager = new LinearLayoutManager(this);//默认竖直
// layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//水平
//网格
// GridLayoutManager layoutManager = new GridLayoutManager(this,4);
//瀑布流,可以在适配器的onBindViewHolder方法中为我们的item设置个随机的高度
// StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL);//参数控制水平还是垂直滑动
recyclerView.setLayoutManager(layoutManager);
//绑定适配器
adapter = new TextAdapter(this, stringList);
recyclerView.setAdapter(adapter);
}
@Override
public void initListener() {
}
@Override
public void viewsClick(View view) {
}
@Override
public void onRefresh() {
//刷新
// 模拟一些比较耗时的操作,比如联网获取数据,需要放到子线程去执行
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
refreshData();
swipeLayout.setRefreshing(false);
}
}, 4000);
}
//简单示例,手动添加数据
private void refreshData() {
stringList.add("我很帅");
adapter.notifyDataSetChanged();
}
}