pulltorefresh 实现瀑布流的方式(基于recycleview)



我在项目中的需求是:

1、项目本身我是集进人了pulltofresh的类库实现下拉刷新及上拉加载

2、某一个页面要实现瀑布流的形式,第一想到的是recycleview的staggredGridLayoutManager 的布局管理器实现。

3、将pulltoRefresh的控件对recycleview实现扩展

4、处理下拉、上拉之后的逻辑


一、实现的第一步:

要想用pulltorefresh实现下拉刷新,你首先得导入pulltorefresh的类库,具体的代码先不细说了。

如图:pulltorefresh 实现瀑布流的方式(基于recycleview)_第1张图片


二、添加recycleview的依赖

  compile 'com.android.support:cardview-v7:23.1.0'
    compile 'com.android.support:recyclerview-v7:23.1.0'


三、在导入的pulltofresh 的类库中添加一个支持recycleview的类


package com.sinolbs.seeyou.pulltorefresh.library.extras.recyclerview;

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

import com.sinolbs.seeyou.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;
    }

}


四、对控件进行引用


  

五、使用的方法

  mRecycle_view = (PullToRefreshRecycleView) baseview.findViewById(R.id.recycle_view);

 //获取pulltofresh的实体控件
        final RecyclerView refreshableView = mRecycle_view.getRefreshableView();

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        SpacesItemDecoration decoration=new SpacesItemDecoration(8);
        refreshableView.addItemDecoration(decoration);
        layoutManager.setReverseLayout(false);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        refreshableView.setLayoutManager(layoutManager);
        // 设置item动画
        refreshableView.setItemAnimator(new DefaultItemAnimator());

        RecommendGridAdapter gridAdapter = new RecommendGridAdapter(getActivity(), datas);
        //2.设置adapter
        refreshableView.setAdapter(gridAdapter);
        gridAdapter.getRandomHeight(datas);

        mRecycle_view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(getActivity(), "Pull Down!", Toast.LENGTH_SHORT).show();

                new Handler().postDelayed(new Runnable(){
                    @Override
                    public void run() {
                        Log.e("延时两秒","延时两秒检索");
                        mRecycle_view.onRefreshComplete();
                    }
                }, 3000);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                Toast.makeText(getActivity(), "Pull Up!", Toast.LENGTH_SHORT).show();

            }
        });

六、完美实现

欧耶!!!






你可能感兴趣的:(java)