pulltorefresh+recycleview 实现的瀑布流(带下拉刷新,上拉加载更多)

最近项目中要用到瀑布流,要求:pulltorefresh框架下实现下拉和上拉,用recycleview。想此效果网上一大推,应该可以找到源码,后来找了很久也没有找到合适的。最后参考pulltorefreshlistview,自己写了一个。现将核心代码贴出来。


1.注册id。

  在pulltorefresh_lib  的ids中添加

<item type="id" name="straggereddGridLayout" />


2.继承  PullToRefreshBase


package com.handmark.pulltorefresh.library;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.View;

public class PullToRefreshStaggeredGridLayout extends
        PullToRefreshBase<RecyclerView> {
    public PullToRefreshStaggeredGridLayout(Context context) {
        super(context);
    }

    public PullToRefreshStaggeredGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

    }
 //重写4个方法
//1 横竖设置
    @Override
    public final Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    
    //2  滑动的View
    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        RecyclerView view = new RecyclerView(context, attrs);
//设置布局方式,此处的2 表示每行2个item
        StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(
                2, StaggeredGridLayoutManager.VERTICAL);
        view.setLayoutManager(mLayoutManager);
view.setId(R.id.straggereddGridLayout);
        return view;
    }

    //重写4个方法
    //3 是否滑动到底部
    @Override
    protected boolean isReadyForPullEnd() {
        View view = getRefreshableView().getChildAt(getRefreshableView().getChildCount() - 1);
        if (null != view) {
            return getRefreshableView().getBottom() >= view.getBottom();
        }
        return false;
    }

    //重写4个方法
    //4 是否滑动到顶部
    @Override
    protected boolean isReadyForPullStart() {
        View view = getRefreshableView().getChildAt(0);

        if (view != null) {
            return view.getTop() >= getRefreshableView().getTop();
        }
        return false;
    }


}

好了,下面是怎么使用

xml中

<com.handmark.pulltorefresh.library.PullToRefreshStaggeredGridLayoutandroid:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/mStaggeredGridView" android:scrollbars="none" android:listSelector="@android:color/transparent" android:dividerHeight="0dp" android:divider="#00000000" android:cacheColorHint="#00000000" android:layout_margin="@dimen/DIMEN_30PX"> </com.handmark.pulltorefresh.library.PullToRefreshStaggeredGridLayout>

java 代码就不想贴了,会recycleview 和 pulltorefresh的都知道怎么弄了。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值