自定義pulltorefresh實(shí)現(xiàn)瀑布流下拉屬性,上拉加載

package com.handmark.pulltorefresh.library.extras;

/**
 * Created by Administrator on 2018/8/22 0022.
 */

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

import com.handmark.pulltorefresh.library.PullToRefreshBase;

/**
 * Created by Administrator on 2017/11/7.
 * pulltorefresh擴(kuò)展recycleview;
 * 實(shí)現(xiàn)recycle的下拉刷新
 *
 */

public class PullToRefreshRecycleView  extends PullToRefreshBase<RecyclerView> {

    private RecyclerView mRecyclerView;

    public PullToRefreshRecycleView(Context context) {
        super(context);
    }

    public PullToRefreshRecycleView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PullToRefreshRecycleView(Context context, PullToRefreshBase.Mode mode) {
        super(context, mode);
    }

    public PullToRefreshRecycleView(Context context, Mode mode, AnimationStyle style) {
        super(context, mode, style);
    }

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

    @Override
    protected RecyclerView createRefreshableView(Context context,
                                                 AttributeSet attrs) {
        mRecyclerView = new RecyclerView(context, attrs);
        return mRecyclerView;
    }

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

    @Override
    protected boolean isReadyForPullEnd() {
        return isLastItemVisible();
    }

    /**
     * @Description: 判斷第一個(gè)條目是否完全可見(jiàn)
     *
     * @return boolean:
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private boolean isFirstItemVisible() {
        final RecyclerView.Adapter<?> adapter = getRefreshableView().getAdapter();

        // 如果未設(shè)置Adapter或者Adapter沒(méi)有數(shù)據(jù)可以下拉刷新
        if (null == adapter || adapter.getItemCount() == 0) {
            return true;

        } else {
            // 第一個(gè)條目完全展示,可以刷新
            if (getFirstVisiblePosition() == 0) {
                return mRecyclerView.getChildAt(0).getTop() >= mRecyclerView
                        .getTop();
            }
        }

        return false;
    }

    /**
     * @Description: 獲取第一個(gè)可見(jiàn)子View的位置下標(biāo)
     *
     * @return int: 位置
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private int getFirstVisiblePosition() {
        View firstVisibleChild = mRecyclerView.getChildAt(0);
        return firstVisibleChild != null ? mRecyclerView
                .getChildAdapterPosition(firstVisibleChild) : -1;
    }

    /**
     * @Description: 判斷最后一個(gè)條目是否完全可見(jiàn)
     *
     * @return boolean:
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private boolean isLastItemVisible() {
        final RecyclerView.Adapter<?> adapter = getRefreshableView().getAdapter();

        // 如果未設(shè)置Adapter或者Adapter沒(méi)有數(shù)據(jù)可以上拉刷新
        if (null == adapter || adapter.getItemCount() == 0) {
            return true;

        } else {
            // 最后一個(gè)條目View完全展示,可以刷新
            int lastVisiblePosition = getLastVisiblePosition();
            if(lastVisiblePosition >= mRecyclerView.getAdapter().getItemCount()-1) {
                return mRecyclerView.getChildAt(
                        mRecyclerView.getChildCount() - 1).getBottom() <= mRecyclerView
                        .getBottom();
            }
        }

        return false;
    }

    /**
     * @Description: 獲取最后一個(gè)可見(jiàn)子View的位置下標(biāo)
     *
     * @return int: 位置
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private int getLastVisiblePosition() {
        View lastVisibleChild = mRecyclerView.getChildAt(mRecyclerView
                .getChildCount() - 1);
        return lastVisibleChild != null ? mRecyclerView
                .getChildAdapterPosition(lastVisibleChild) : -1;
    }

}

添加依賴(lài)然后

使用自定義

布局


    <com.handmark.pulltorefresh.library.extras.PullToRefreshRecycleView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/recycle_view_follow1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ptr:ptrHeaderBackground="#fff"
        ptr:ptrHeaderTextColor="#999"
        ptr:ptrMode="both"
        />

代碼

package com.example.administrator.kuaisoapp.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.kuaisoapp.R;
import com.example.administrator.kuaisoapp.activity.PlayActivity;
import com.example.administrator.kuaisoapp.adapter.FollowAdapter;
import com.example.administrator.kuaisoapp.gson.FollowGson;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.extras.PullToRefreshRecycleView;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class FollowFragment extends Fragment {
    private PullToRefreshRecycleView recycleViewFollow;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 222) {
                mFollowAdapter.updateAdapter(mFollowGson);
            }
        }
    };


    View mView;
    private FollowGson mFollowGson;
    private FollowAdapter mFollowAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView=View.inflate(getContext(),R.layout.fragment_follow,null);
        init(mView);
        initData();
    }

    private void init(View view) {
        recycleViewFollow = (PullToRefreshRecycleView) view.findViewById(R.id.recycle_view_follow1);

        final RecyclerView refreshableView = recycleViewFollow.getRefreshableView();

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setReverseLayout(false);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        refreshableView.setLayoutManager(layoutManager);
        // 設(shè)置item動(dòng)畫(huà)
        refreshableView.setItemAnimator(new DefaultItemAnimator());

        mFollowAdapter = new FollowAdapter(getActivity(),mFollowGson);
        //2.設(shè)置adapter
        refreshableView.setAdapter(mFollowAdapter);
        mFollowAdapter.setInterface(new FollowAdapter.IReflashListner() {
            @Override
            public void startActivity(int pos) {
                Intent intent = new Intent(getActivity(), PlayActivity.class);
                intent.putExtra("url",mFollowGson.getData().get(pos).getVideoUrls().get(1));
                intent.putExtra("name",mFollowGson.getData().get(pos).getPosterScreenName());
                intent.putExtra("text",mFollowGson.getData().get(pos).getDescription());
                intent.putExtra("viewCount",mFollowGson.getData().get(pos).getViewCount());
                intent.putExtra("lickCount",mFollowGson.getData().get(pos).getLikeCount());
                intent.putExtra("publisdata",mFollowGson.getData().get(pos).getPublishDateStr());
                intent.putExtra("comment",mFollowGson.getData().get(pos).getCommentCount());
                intent.putExtra("id",mFollowGson.getData().get(pos).getId());
                intent.putExtra("image",mFollowGson.getData().get(pos).getCoverUrl());

                getActivity().startActivity(intent);
            }
        });
        recycleViewFollow.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<RecyclerView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                        recycleViewFollow.onRefreshComplete();
                    }
                }, 500);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                        recycleViewFollow.onRefreshComplete();
                    }
                }, 500);

            }
        });

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        container  = (ViewGroup) mView.getParent();
        if(container!=null){
            container.removeAllViewsInLayout();
        }
        return mView;
    }
    public  void initData(){
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url = new URL("http://120.76.205.241:8000/video/gifshow?pageToken=0&apikey=tZ1HPx0MRcC6njHLZXLhzseYk8DpRx6tMLvzDa7gYRE0mLGC5stHpuZE2Svt63ed");
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setConnectTimeout(3000);
                    urlConnection.connect();
                    int code = urlConnection.getResponseCode();
                    if(code==200){
                        InputStream inputStream = urlConnection.getInputStream();
                        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                        byte[]data=new byte[1024];
                        int lenth=-1;
                        while ((lenth=inputStream.read(data))!=-1){
                            byteArrayOutputStream.write(data,0,lenth);
                            byteArrayOutputStream.flush();
                        }
                        byteArrayOutputStream.close();
                        inputStream.close();
                        String s = byteArrayOutputStream.toString();
                        Gson gson = new Gson();
                        mFollowGson = gson.fromJson(s, FollowGson.class);
                        mHandler.sendEmptyMessage(222);
                    }else{

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();


    }
}

將重寫(xiě)的方法修改為

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        try {
            super.onRestoreInstanceState(state);
        }catch (Exception e) {}
        state=null;
      }

效果圖

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市梭稚,隨后出現(xiàn)的幾起案子多搀,更是在濱河造成了極大的恐慌领舰,老刑警劉巖蝌焚,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件移剪,死亡現(xiàn)場(chǎng)離奇詭異喜喂,居然都是意外死亡瓤摧,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)玉吁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)照弥,“玉大人,你說(shuō)我怎么就攤上這事进副≌獯В” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵影斑,是天一觀的道長(zhǎng)给赞。 經(jīng)常有香客問(wèn)我,道長(zhǎng)矫户,這世上最難降的妖魔是什么片迅? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮皆辽,結(jié)果婚禮上柑蛇,老公的妹妹穿的比我還像新娘。我一直安慰自己驱闷,他們只是感情好耻台,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著空另,像睡著了一般盆耽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,573評(píng)論 1 305
  • 那天征字,我揣著相機(jī)與錄音都弹,去河邊找鬼。 笑死匙姜,一個(gè)胖子當(dāng)著我的面吹牛畅厢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播氮昧,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼框杜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了袖肥?” 一聲冷哼從身側(cè)響起咪辱,我...
    開(kāi)封第一講書(shū)人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎椎组,沒(méi)想到半個(gè)月后油狂,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡寸癌,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年专筷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒸苇。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡磷蛹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出溪烤,到底是詐尸還是另有隱情味咳,我是刑警寧澤,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布檬嘀,位于F島的核電站槽驶,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏鸳兽。R本人自食惡果不足惜掂铐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望贸铜。 院中可真熱鬧堡纬,春花似錦、人聲如沸蒿秦。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)棍鳖。三九已至炮叶,卻和暖如春碗旅,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背镜悉。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工祟辟, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人侣肄。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓旧困,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親稼锅。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吼具,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1、通過(guò)CocoaPods安裝項(xiàng)目名稱(chēng)項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫(kù)組件 SD...
    陽(yáng)明先生_X自主閱讀 15,981評(píng)論 3 119
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,139評(píng)論 25 707
  • 人生十難: 1矩距、最難改變的是習(xí)慣拗盒; 2、最難提高的是素質(zhì)锥债; 3陡蝇、最難把握的是機(jī)遇;...
    河北南和劉志玉閱讀 167評(píng)論 2 3
  • 黃直錄【11】 【原文】 門(mén)人作文送友行哮肚。問(wèn)先生曰:“作文免不了費(fèi)心思登夫,作了后又一二日常記在懷≌雷螅” 曰:“文字思索...
    大珊老師閱讀 608評(píng)論 0 8
  • (1) 當(dāng)方寧終于說(shuō)他不喜歡我了的時(shí)候拼窥,我的心里竟然不如我想的那般輕松,倒有些難過(guò)了蹋凝。 我笑了笑鲁纠,說(shuō):“寧子,你終...
    貓左閱讀 438評(píng)論 0 10