通过SwipeToLoadLayout实现一个刷新加载的效果
1、如何集成
在总项目中build.gradle中添加:
repositories {
maven { url "https://jitpack.io" }
}
在单个项目中build.gradle中添加:
dependencies {
compile 'com.github.Aspsine:SwipeToLoadLayout:v1.0.0'
}
2,开始自定义刷新效果
swipeToLoadLayout提供了一套接口,刷新的头部自定义一个View实现SwipeTrigger和SwipeRefreshTrigger就行了,刷新的尾部自定义一个View实现SwipeLoadMoreTrigger和SwipeTrigger。
下拉刷新顶部效果:
public class CustomRefreshHeadView extends TextView implements SwipeRefreshTrigger, SwipeTrigger {
public CustomRefreshHeadView(Context context) {
super(context);
}
public CustomRefreshHeadView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomRefreshHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onRefresh() {
setText("正在拼命加载数据...");
}
@Override
public void onPrepare() {
}
@Override
public void onSwipe(int i, boolean b) {
setText("释放刷新");
}
@Override
public void onRelease() {
}
@Override
public void complete() {
setText("刷新成功");
}
@Override
public void onReset() {
}
}
上拉加载底部自定义效果:
public class Bbb extends TextView implements SwipeLoadMoreTrigger,SwipeTrigger {
public Bbb(Context context) {
super(context);
}
public Bbb(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public Bbb(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onLoadMore() {
setText("正在拼命加载数据...");
}
@Override
public void onPrepare() {
}
@Override
public void onSwipe(int i, boolean b) {
}
@Override
public void onRelease() {
setText("刷新成功");
}
@Override
public void complete() {
setText("释放刷新");
}
@Override
public void onReset() {
}
}
布局Xml中代码:
注意,swipetoloadlayout中布局包裹的View id是指定的,不能乱改,否则找不到
<item name="swipe_target" type="id" />刷新目标
<item name="swipe_refresh_header" type="id" />刷新头部
<item name="swipe_load_more_footer" type="id" />刷新尾部
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="along.mifeng.us.mykaoshi.MainActivity">
<com.aspsine.swipetoloadlayout.SwipeToLoadLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipeToLoadLayout">
//自定义的头部布局
<along.mifeng.us.mykaoshi.CustomRefreshHeadView
android:id="@+id/swipe_refresh_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0f0"
android:text="界面"
android:id="@+id/swipe_target">
</android.support.v4.view.ViewPager>
//自定义的底部布局
<along.mifeng.us.mykaoshi.Bbb
android:id="@id/swipe_load_more_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"/>
</com.aspsine.swipetoloadlayout.SwipeToLoadLayout>
</RelativeLayout>
MainActivity代码中调用:
public class MainActivity extends AppCompatActivity implements OnRefreshListener, OnLoadMoreListener {
private SwipeToLoadLayout swipeToLoadLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeToLoadLayout = (SwipeToLoadLayout) findViewById(R.id.swipeToLoadLayout);
//下拉刷新的滑动事件
swipeToLoadLayout.setOnRefreshListener(this);
//上来加载的滑动事件
swipeToLoadLayout.setOnLoadMoreListener(this);
}
@Override
public void onRefresh() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
//设置是否下拉刷新中
swipeToLoadLayout.setRefreshing(false);
}
},2000);
}
@Override
public void onLoadMore() {
swipeToLoadLayout.postDelayed(new Runnable() {
@Override
public void run() {
//设置是否上拉加载中
swipeToLoadLayout.setLoadingMore(false);
}
},2000);
}
}