一步一步封装自己的Recycleview,上拉加载

同样利用Recycleview中adapter的type,使最后一条目是上拉加载更多的view,效果如下:
这里写图片描述

使用和刷新的view一样

        //new出实例并设置到adapter
        loadMoreView = new LoadMoreView(this);
        adapter.setLoadMoreView(loadMoreView);

监听也封装到上拉加载的view中,和recycleview分离

 loadMoreView.setOnLoadMoreListener(new LoadMoreViewAbs.OnLoadMoreListener() {
            @Override
            public void onLoadMoreClick() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        for(int i = 0;i<20;i++){
                            list.add("");
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.notifyDataSetChanged();
                                if(list.size() >= 100){
                                    loadMoreView.loadMoreComplete(false);
                                } else {
                                    loadMoreView.loadMoreComplete(true);
                                }
                            }
                        });
                    }
                },2000);
            }
        });

自定义上拉加载的view 继承封装好的抽象类实现其方法即可

/**
 * Created by shen on 2016/8/19.
 */
public class LoadMoreView extends LoadMoreViewAbs {

    private TextView tv;
    private View view;

    public LoadMoreView(Context context) {
        super(context);
        init();

    }

    public void init(){
        tv = (TextView) loadMoreView.findViewById(R.id.tv);
    }

    @Override
    protected void loadMoring() {
        tv.setText("正在加载更多...");
    }

    @Override
    protected void loadMoreCompleteSub(boolean isLoadMore) {
        if(isLoadMore){
            tv.setText("点击加载更多");
        } else {
            tv.setText("已经加载全部内容");
        }
    }

    @Override
    protected int getEmptyHeight() {
        return ((int) (DensityUtils.px2dp(context,300)));
    }

    @Override
    protected int getLayoutView() {
        return R.layout.loadmore_view;
    }
}

封装好的上拉加载抽象类:


/**
 * Created by shen on 2016/8/19.
 */
public abstract class LoadMoreViewAbs extends LinearLayout{

    private int EMPTY_HEIGHT = 0;//刷新底部空白高度
    private static final int ISLOADMORING = 1000;
    private static final int NORMAL = 1001;
    private int currentState = NORMAL;

    protected LinearLayout loadMoreView;
    protected Context context;
    private int measureHeight;
//    private boolean isLoadMore = true;//是否可以继续加载

    private OnLoadMoreListener onLoadMoreListener;

    public void loadMoreComplete(boolean isLoadMore){
        loadMoreView.setEnabled(isLoadMore);
        loadMoreCompleteSub(isLoadMore);
        setCurrentState(NORMAL);
    }

    protected abstract void loadMoreCompleteSub(boolean allTouch);

    public interface OnLoadMoreListener{
        void onLoadMoreClick();
    }
    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener){
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public LoadMoreViewAbs(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        loadMoreView = (LinearLayout) LayoutInflater.from(context).inflate(getLayoutView(), null);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0,0,0,0);
        this.setLayoutParams(params);
        this.setPadding(0,0,0,0);



        this.addView(loadMoreView,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));

        measure(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        measureHeight = getMeasuredHeight();

        EMPTY_HEIGHT = getEmptyHeight();

        loadMoreView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onLoadMoreListener!= null && currentState != ISLOADMORING){
                    onLoadMoreListener.onLoadMoreClick();
                    setCurrentState(ISLOADMORING);
                    loadMoring();
                }
            }
        });
    }

    public int getCurrentState() {
        return currentState;
    }

    public void setCurrentState(int currentState) {
        this.currentState = currentState;
    }

    public void mationActionUp(float rawY) {
        if(getVisibleHeight() > measureHeight){
            smoothScrollTo(measureHeight);
        }
    }

    public int getMeasureHeight(){
        L.d("measureHeight","measureHeight:"+measureHeight);
        return measureHeight;
    }

    /**
     * 当手指下滑时被调用
     * */
    public void putDown(float dy){
        float countPullDown = getVisibleHeight() - dy/2;

        L.d("refresh_view","countPullDown:"+countPullDown+"dy:"+dy+"measureHeight:"+measureHeight+"EMPTY_HEIGHT:"+EMPTY_HEIGHT);
        if(countPullDown < getMeasureHeight()){
            setRefreshViewHeight(getMeasureHeight());
            return;
        }
        if(countPullDown > measureHeight + EMPTY_HEIGHT){
            countPullDown = measureHeight + EMPTY_HEIGHT;
            return;
        }
        setRefreshViewHeight((int) (countPullDown));
    }

    /**
     * 设置view 的高度
     * */
    private void setRefreshViewHeight(int dy) {
        ViewGroup.LayoutParams layoutParams = loadMoreView.getLayoutParams();
        layoutParams.height = dy;
        loadMoreView.setLayoutParams(layoutParams);

    }

    public void smoothScrollTo(int destHeight) {
        if(destHeight < 0){
            return;
        }
        ValueAnimator animator = ValueAnimator.ofInt(getVisibleHeight(), destHeight);
        animator.setDuration(200).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setRefreshViewHeight((int) animation.getAnimatedValue());
            }
        });
        animator.start();
    }

    public int getVisibleHeight() {
        return loadMoreView.getLayoutParams().height;
    }

    public int getTotalHeight(){
        return measureHeight + EMPTY_HEIGHT;
    }

    protected abstract void loadMoring();

    protected abstract int getEmptyHeight();

    protected abstract int getLayoutView();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值