对com.handmark.pulltorefresh.library下拉扩充瀑布流

转载请声明:http://blog.csdn.net/yoyo_newbie/article/details/48108417

很多人都会和我一样郁闷,这个com.handmark.pulltorefresh.library下拉库唯一遗憾的是没有下拉的RecyclerView瀑布流。

于是本人看了下源码分析,原来对他扩充很简单,只要了解PullToRefreshBase的

isReadyForPullStart, isReadyForPullEnd这两个方法就可以轻松实现。当然还有个注意的方法就是
getPullToRefreshScrollDirection。这个决定是上下拉还是左右拉。用过
PullToRefreshViewPager的人就知道,是左右拉刷新的,
PullToRefreshListView,
PullToRefreshGridVie等这些是上下啦的。

 
  
isReadyForPullStart这个方法是通知当前控件是否能下拉,
isReadyForPullEnd这个方法是通知当前控件是否能上啦。
那么我们只要实现监听某个控件的是否滚动已到头部(完全显示头部)让isReadyForPullStart提供true or false,
是否滚动到末尾(完全显示末尾)提供true or false就可以扩充com.handmark.pulltorefresh.library拖拽刷新事件

下面这个例子是对其扩充一个 2列的瀑布流下拉,居于RecyclerView的。如果你像实现多列,那么你可以对以下的例子自定义修改。通过在com.handmark.pulltorefresh.library的values文件夹的attrs.xml添加属性进行列的属性控制列数,会自定义view的人都知道,就不多说了。


实现如下:
注意,要添加v4包和v7包的RecyclerView
首先在这个库里面的ids.xml添加 <item type="id" name="straggereddGridLayout" />
添加后为
xml version="1.0" encoding="utf-8"?>
<resources>

    <item type="id" name="gridview" />
    <item type="id" name="webview" />
    <item type="id" name="scrollview" />
    
    <item type="id" name="straggereddGridLayout" />
resources>

然后直接贴代码到
 
  
com.handmark.pulltorefresh.library.extras包下即可

 
  
 
  
package com.handmark.pulltorefresh.library.extras;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.R;

import java.util.concurrent.atomic.AtomicIntegerArray;

/**
 * 瀑布流下啦刷新
 * Created by sam on 15-8-30.
 */
public class PullToRefreshStaggeredGridLayout extends PullToRefreshBase {
    public PullToRefreshStaggeredGridLayout(Context context) {
        super(context);
    }

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


    }

    @Override
    public final Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    private RecyclerView recyclerView;
    private boolean isScrollOnHeader = true;
    private boolean isScrollOnFooter = false;
    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        recyclerView = new RecyclerView(context, attrs);
        recyclerView.setId(R.id.straggereddGridLayout);
        final  StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);

        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener()
            {
            int[] lastVisibleItem;
            int[] fistVisibleItem;
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,
                                             int newState) {

                    if(null != fistVisibleItem)
                    {
                        isScrollOnHeader =  0 ==fistVisibleItem[0];
                    }
                    else
                    {
                        isScrollOnHeader =true;
                    }

                    if(null != lastVisibleItem)
                    {
                        boolean isLast = mLayoutManager.getItemCount() -1  ==lastVisibleItem[0]||mLayoutManager.getItemCount()  ==lastVisibleItem[1]
                       || mLayoutManager.getItemCount() -1  ==lastVisibleItem[1]||mLayoutManager.getItemCount()  ==lastVisibleItem[0];
                        isScrollOnFooter =   newState == RecyclerView.SCROLL_STATE_IDLE&& isLast ;
                    }
                    else
                    {
                        isScrollOnFooter =true;
                    }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                fistVisibleItem = mLayoutManager.findFirstCompletelyVisibleItemPositions(new int[2]);
                lastVisibleItem = mLayoutManager.findLastCompletelyVisibleItemPositions(new int[2]);
            }

        });
        return recyclerView;
    }




    @Override
    protected boolean isReadyForPullStart() {
        return isScrollOnHeader;
    }

    @Override
    protected boolean isReadyForPullEnd() {

        return isScrollOnFooter;
    }

}


使用如下
<com.handmark.pulltorefresh.library.extras.PullToRefreshStaggeredGridLayout
    xmlns:ptr="http://schemas.android.com/apk/res-auto"
    android:id="@+id/lv_index_jiongtu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ptr:ptrAnimationStyle="rotate"
    ptr:ptrHeaderBackground="#00000000"
    ptr:ptrHeaderTextColor="#ffffff"
    ptr:ptrMode="both"
    ptr:ptrOverScroll="false"
    ptr:ptrShowIndicator="false"

  />

详细就不多说了,至要你会使用RecyclerView即可。。。。。。。










 
  










你可能感兴趣的:(android)