天天看點

自定義SwipeRefreshLayout實作ListView上拉加載下拉重新整理

說實話現在大部分人都不在用ListView了,不過說實話如果僅僅是普通的清單其實用哪個都無所謂的。

可能有人會說有好多第三方的下拉重新整理上拉加載的架構,但是我覺得吧,有些東西自己能實作的就還是用自己寫的好。

不羅嗦了,直接上代碼,注釋都已寫好

/**
 * 繼承自SwipeRefreshLayout,進而實作滑動到底部時上拉加載更多的功能.
 */
public class RefreshLayout 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;

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

    @SuppressLint("InflateParams")
    public RefreshLayout(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();
        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:
            // 移動
            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 && 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;
        }
    }

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

    @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();
            }
        }
    }



    /**
     * 加載更多的監聽器
     */
    public static interface OnLoadListener {
        public void onLoad();
    }

}           

複制

下面寫一下如何使用

  • 在布局中使用自定義的RefreshLayout包裹ListView
<RefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="8">

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:divider="@null" />
    </RefreshLayout>           

複制

  • 在Activity中調用refresh()方法,做RefreshLayout的監聽和顔色設定
private void refresh() {
        refreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent, R.color.colorAccent, R.color.colorPrimaryDark);
//下拉重新整理
        refreshLayout.setOnRefreshListener(this);
//上拉加載
        refreshLayout.setOnLoadListener(this);
    }           

複制

  • 最後在實作的重新整理和上拉監聽中加refreshLayout.setLoading(false);使下拉和上拉的加載框消失
  • 下拉重新整理加在onRefresh方法最後即可
@Override
    public void onRefresh() {
        //加在最後
        refreshLayout.setRefreshing(false);
    }           

複制

  • 上拉監聽需加在響應方法中即可
@Override
    public void onLoad() {
        page = ++page;
        yNProgressDialog.show();
        NetTool.getInstance().rxPostNet().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<>() {
                    @Override
                    public void accept(@NonNull Bean eBean) throws Exception {
                        NProgressDialog.dismiss();
                        if (eBean.getSuccess().equals("1")) {
                            refreshLayout.setLoading(false);
                            eAdapter.addData(eBean);
                        } else {
                            refreshLayout.setLoading(false);
                            refreshLayout.setOnLoadListener(null);
                            Toast.makeText(getApplicationContext().getApplicationContext(), eBean.getMsg().toString(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        refreshLayout.setLoading(false);
                        NProgressDialog.dismiss();
                        Toast.makeText(getApplicationContext().getApplicationContext(), "連接配接異常", Toast.LENGTH_SHORT).show();
                    }
                });

    }           

複制

好了到這裡上拉重新整理和下拉加載就都完成了