上拉刷新下拉加载

首先先写个RefreshLayout

package com.liuxuyang.myapplication.base;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

import com.liuxuyang.myapplication.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 继承自SwipeRefreshLayout,从而实现滑动到底部时上拉加载更多的功能.
 *
 * @author mrsimple
 */
public class MyRefreshLayout extends SwipeRefreshLayout implements OnScrollListener {

    /**
     * 是一个距离,表示滑动的时候,手的移动要大于这个距离才开始移动控件。如果小于这个距离就不触发移动控件
     */
    private int mTouchSlop;
    /**
     * listview实例
     */
    private ListView mListView;

    /**
     * 上拉监听器, 到了最底部的上拉加载操作
     */
    private OnLoadListener mOnLoadListener;

    /**
     * ListView的加载中footer
     */
    private View mListViewFooter;

    /**
     * 按下时的y坐标
     */
    private int mYDown;
    /**
     * 抬起时的y坐标, mYDown一起用于滑动到底部时判断是上拉还是下拉
     */
    private int mLastY;
    /**
     * 是否在加载中 ( 上拉加载更多 )
     */
    private boolean isLoading = false;
    private boolean isMoved;

    /**
     * @param context
     */
    public MyRefreshLayout(Context context) {
        this(context, null);
    }

    @SuppressLint("InflateParams")
    public MyRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        //判断
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();


        mListViewFooter = LayoutInflater.from(context).inflate(
                R.layout.listview_footer, null, false);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        // 初始化ListView对象
        if (mListView == null) {
            getListView();
        }
    }

    /**
     * 获取ListView对象
     */
    private void getListView() {
        int childs = getChildCount();
        LogUtils.e(childs+"ggggggg");
        if (childs > 0) {
            View childView = getChildAt(0);
            if (childView instanceof ListView) {
                mListView = (ListView) childView;
                // 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
                mListView.setOnScrollListener(this);
                Log.d(VIEW_LOG_TAG, "### 找到listview");
            }
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        final int action = event.getAction();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                // 按下
                mYDown = (int) event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                // 移动
                isMoved = true;
                mLastY = (int) event.getRawY();
                break;

            case MotionEvent.ACTION_UP:
                // 抬起
//                if (canLoad()) {
//                    loadData();
//                }
                break;
            default:
                break;
        }

        return super.dispatchTouchEvent(event);
    }

    /**
     * 是否可以加载更多, 条件是到了最底部, listview不在加载中, 且为上拉操作.
     *
     * @return
     */
    private boolean canLoad() {
        return isBottom() && !isLoading && isMoved && isPullUp();
    }

    /**
     * 判断是否到了最底部
     */
    private boolean isBottom() {

        if (mListView != null && mListView.getAdapter() != null) {
            return mListView.getLastVisiblePosition() == (mListView
                    .getAdapter().getCount() - 1);
        }
        return false;
    }

    /**
     * 是否是上拉操作
     *
     * @return
     */
    private boolean isPullUp() {
        return (mYDown - mLastY) >= mTouchSlop;
    }

    /**
     * 如果到了最底部,而且是上拉操作.那么执行onLoad方法
     */
    private void loadData() {
        if (mOnLoadListener != null) {
            // 设置状态
            setLoading(true);
            //
            mOnLoadListener.onLoad();
        }
    }

    /**
     * @param loading
     */
    public void setLoading(boolean loading) {
        isLoading = loading;
        if (isLoading) {
            mListView.addFooterView(mListViewFooter);
        } else {
            mListView.removeFooterView(mListViewFooter);
            mYDown = 0;
            mLastY = 0;
            isMoved = false;
        }
    }

    /**
     * @param loadListener
     */
    public void setOnLoadListener(OnLoadListener loadListener) {
        mOnLoadListener = loadListener;
    }

    public void removeOnLoadListener() {
        mOnLoadListener = null;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        // 滚动时到了最底部也可以加载更多
        if (canLoad()) {
            loadData();
        }
    }

    /**
     * 设置刷新
     */
    public static void setRefreshing(SwipeRefreshLayout refreshLayout,
                                     boolean refreshing, boolean notify) {
        Class<? extends SwipeRefreshLayout> refreshLayoutClass = refreshLayout
                .getClass();
        if (refreshLayoutClass != null) {

            try {
                Method setRefreshing = refreshLayoutClass.getDeclaredMethod(
                        "setRefreshing", boolean.class, boolean.class);
                setRefreshing.setAccessible(true);
                setRefreshing.invoke(refreshLayout, refreshing, notify);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 加载更多的监听器
     *
     * @author mrsimple
     */
    public interface OnLoadListener {
        void onLoad();
    }
}
然后在在布局XML文件里把需要做上拉刷新下拉加载的ListView用自定义MyRefreshLayout包裹上

<com.liuxuyang.myapplication.base.MyRefreshLayout
   android:id="@+id/one_MyRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/one_xiao_shou_xiang_qing"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>
</com.liuxuyang.myapplication.base.MyRefreshLayout>
再到Activity里进行设置及调用

首先在onCreateView里面写

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_xiao_shou_one, null, false);
        ButterKnife.bind(this, view);
//        initData();
//        EventBus.getDefault().register(this);
//        dengjiPopupWindow();
//        shaixuanPopupWindow();
//        initPopupWindow();
        oneMyRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page =1 ;//页码
                loadMore = false;//是否加载更多
                initData();//刷新数据
                oneMyRefreshLayout.setRefreshing(false);//停止显示刷新圆圈
            }
        });
        oneMyRefreshLayout.setOnLoadListener(this);//设置加载更多监听
        return view;
    }

在Activity里添加个方法刷新数据

@Override//设置加载更多回调
public void onLoad() {
    if (loadMore) {
        initData();
    }
}
private void initData() {
    Map<String, String> params = new HashMap<>();
    params.put("page", page + "");
    params.put("categoryId", categoryId);
    params.put("classId", classId);
    params.put("class", nameId);
    params.put("levelId", levelId);
    params.put("startTime", startTime);
    params.put("endTime", endTime);
    params.put("salesman", salesman);
    params.put("orderStatus", orderStatus);
    RequestManager.request(API.XIAO_SHOU_DING_DAN_LIE_BIAO, XiaoShouDingDanInfo.class, params, new RequestManager.OnResponseListener<XiaoShouDingDanInfo>() {
        @Override
        public void onResponse(XiaoShouDingDanInfo xiaoShouDingDanInfo) {
            list = xiaoShouDingDanInfo.getInfo().getOrderList();

            if (!loadMore) {
                datalist = list;
                onexiaoShouXiangQingAdapter = new oneXiaoShouXiangQingAdapter(oneXiaoShouXiangQing, datalist);
                oneXiaoShouXiangQing.setAdapter(onexiaoShouXiangQingAdapter);
                if (list.size() == 20) {
                    loadMore = true;//数据有20就可以加载更多
                } else {
                    loadMore = false;//不可以加载更多
                    oneMyRefreshLayout.removeOnLoadListener();//关闭设置回调的监听
                }
            } else {
                datalist.addAll(list);//添加数据
                oneMyRefreshLayout.setLoading(false);//关闭加载更多的状态
                onexiaoShouXiangQingAdapter.notifyDataSetChanged();//通知刷新List数据
            }
            page = page + 1;//页码
        }
    });

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值