SwipeToLoadLayout下拉刷新

通过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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值