自定义pulltorefresh实现瀑布流下拉属性,上拉加载

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扩展recycleview;
 * 实现recycle的下拉刷新
 *
 */

public class PullToRefreshRecycleView  extends PullToRefreshBase {

    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: 判断第一个条目是否完全可见
     *
     * @return boolean:
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private boolean isFirstItemVisible() {
        final RecyclerView.Adapter adapter = getRefreshableView().getAdapter();

        // 如果未设置Adapter或者Adapter没有数据可以下拉刷新
        if (null == adapter || adapter.getItemCount() == 0) {
            return true;

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

        return false;
    }

    /**
     * @Description: 获取第一个可见子View的位置下标
     *
     * @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: 判断最后一个条目是否完全可见
     *
     * @return boolean:
     * @version 1.0
     * @date 2015-9-23
     * @Author zhou.wenkai
     */
    private boolean isLastItemVisible() {
        final RecyclerView.Adapter adapter = getRefreshableView().getAdapter();

        // 如果未设置Adapter或者Adapter没有数据可以上拉刷新
        if (null == adapter || adapter.getItemCount() == 0) {
            return true;

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

        return false;
    }

    /**
     * @Description: 获取最后一个可见子View的位置下标
     *
     * @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;
    }

}

添加依赖然后

使用自定义

布局


    

代码

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);
        // 设置item动画
        refreshableView.setItemAnimator(new DefaultItemAnimator());

        mFollowAdapter = new FollowAdapter(getActivity(),mFollowGson);
        //2.设置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() {

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

            @Override
            public void onPullUpToRefresh(PullToRefreshBase 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();


    }
}

将重写的方法修改为

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

效果图

自定义pulltorefresh实现瀑布流下拉属性,上拉加载_第1张图片
image.png

你可能感兴趣的:(自定义pulltorefresh实现瀑布流下拉属性,上拉加载)