美文网首页
项目重构(1)——下拉刷新MVP

项目重构(1)——下拉刷新MVP

作者: 比尔王BillWang | 来源:发表于2017-04-13 10:17 被阅读0次

    基本上所有的程序都会用到下拉刷新,但是需求的不同使得我们需要去使用不同的下拉刷新控件,在我的项目中使用了Pull-To-Refresh与SwipeRefreshLayout两种,现在要统一效果,但是又怕直接替换后会出问题,所以就把逻辑与控件抽离,因为逻辑简单,就只实现了V部分。

    ISwipeV 接口

    import android.support.v7.widget.RecyclerView;
    
    public interface ISwipeV {
        Object getSwipeObject();
    
        /**
         * 设置刷新状态
         * @param refreshing
         */
        void setRefreshing(boolean refreshing);
    
        /**
         * 设置是否可用
         * @param enable 是否可用
         * @param recyclerView 关联的RecyclerView
         * @param listener 下拉刷新回调(如果禁用,则为null)
         */
        void setEnable(boolean enable, RecyclerView recyclerView, SwipeRefreshListener listener);
    
        interface SwipeRefreshListener {
            void onRefreshBegin();
        }
    }
    

    Pull-To-Refresh

    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.View;
    
    import in.srain.cube.views.ptr.PtrDefaultHandler;
    import in.srain.cube.views.ptr.PtrFrameLayout;
    import in.srain.cube.views.ptr.PtrHandler;
    
    public class PullToRefreshV implements ISwipeV {
        private PtrFrameLayout ptrMaterialFrameLayout;
    
        public PullToRefreshV(View view) {
            init(view);
        }
    
        private void init(View view) {
            ptrMaterialFrameLayout = (PtrFrameLayout) view.findViewById(R.id.list_material_style_ptr_frame);
        }
    
        @Override
        public Object getSwipeObject() {
            return ptrMaterialFrameLayout;
        }
    
        @Override
        public void setRefreshing(final boolean refreshing) {
            if (ptrMaterialFrameLayout == null) return;
            ptrMaterialFrameLayout.post(new Runnable() {
                @Override
                public void run() {
                    change(refreshing);
                }
            });
        }
    
        @Override
        public void setEnable(boolean enable, final RecyclerView recyclerView, final SwipeRefreshListener listener) {
            ptrMaterialFrameLayout.setEnabled(enable);
            if (enable) {
                ptrMaterialFrameLayout.setPtrHandler(new PtrHandler() {
                    @Override
                    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                        return PtrDefaultHandler.checkContentCanBePulledDown(frame, recyclerView, header);
                    }
    
                    @Override
                    public void onRefreshBegin(PtrFrameLayout frame) {
                        listener.onRefreshBegin();
                    }
                });
            }
        }
    
        private void change(boolean refreshing) {
            if (ptrMaterialFrameLayout != null && ptrMaterialFrameLayout.isRefreshing() != refreshing) {
                Log.d("loading", "setRefreshing " + refreshing);
    
                if (refreshing) {
                    ptrMaterialFrameLayout.autoRefresh(true);
                } else {
                    ptrMaterialFrameLayout.refreshComplete();
                }
            }
        }
    }
    

    SwipeRefreshLayout

    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    
    import com.caidou.R;
    
    public class SwipeV implements ISwipeV {
        private SwipeRefreshLayout swipeRefreshLayout;
        private SwipeRefreshListener mListener;
    
        public SwipeV(View view) {
            swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.list_material_style_ptr_frame);
            if (swipeRefreshLayout != null)
                swipeRefreshLayout.setColorSchemeResources(R.color.blue);
        }
    
        @Override
        public Object getSwipeObject() {
            return swipeRefreshLayout;
        }
    
        @Override
        public void setRefreshing(boolean refreshing) {
            if (swipeRefreshLayout != null)
                swipeRefreshLayout.setRefreshing(refreshing);
            if (refreshing)
                mListener.onRefreshBegin();
        }
    
        @Override
        public void setEnable(boolean enable, RecyclerView recyclerView, SwipeRefreshListener listener) {
            this.mListener = listener;
            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    mListener.onRefreshBegin();
                }
            });
        }
    }
    

    具体的使用就看各位了

    约定:

    V = View,M = Model,P = Presenter

    相关文章

      网友评论

          本文标题:项目重构(1)——下拉刷新MVP

          本文链接:https://www.haomeiwen.com/subject/mtxbattx.html