對com.handmark.pulltorefresh.library下拉擴充瀑布流,pulltorefreshlibrary

來源:互聯網
上載者:User

對com.handmark.pulltorefresh.library下拉擴充瀑布流,pulltorefreshlibrary

轉載請聲明: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<RecyclerView> {    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即可。。。。。。。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.