{"id":949,"date":"2023-03-11T18:57:50","date_gmt":"2023-03-11T10:57:50","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=949"},"modified":"2023-04-29T15:29:41","modified_gmt":"2023-04-29T07:29:41","slug":"android-ptrframelayout-achieves-pull-down-refresh-and-pull-up-loading-more","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-ptrframelayout-achieves-pull-down-refresh-and-pull-up-loading-more\/","title":{"rendered":"Android PtrFrameLayout\u5b9e\u73b0\u4e0b\u62c9\u5237\u65b0\u4e0e\u4e0a\u62c9\u52a0\u8f7d\u66f4\u591a"},"content":{"rendered":"<p>CommonPullToRefresh\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/Chanven\/CommonPullToRefresh\">https:\/\/github.com\/Chanven\/CommonPullToRefresh<\/a><\/p>\n<h2>Gradle\u4f9d\u8d56<\/h2>\n<pre><code>\/\/\u4e0b\u62c9\u5237\u65b0\u53ca\u4e0a\u62c9\u52a0\u8f7d\ncompile &#039;com.chanven.lib:cptr:1.1.0&#039;<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u4fee\u6539PtrFrameLayout<\/h2>\n<p>\u4fee\u6539PtrFrameLayout\u7684setLoadMoreEnable\u65b9\u6cd5\uff0c\u4ee5\u652f\u6301LinearLayout\u5305\u88f9GridView\u3001ListView\u3001RecyclerView\u5b9e\u73b0\u4e0a\u62c9\u52a0\u8f7d\u66f4\u591a\u3002<\/p>\n<pre><code class=\"language-java\">public void setLoadMoreEnable(boolean loadMoreEnable) {\n    ...\n\n    if (null == mLoadMoreHandler) {\n        if (mContentView instanceof GridView) {\n            mLoadMoreHandler = new GridViewHandler();\n        } else if (mContentView instanceof AbsListView) {\n            mLoadMoreHandler = new ListViewHandler();\n        } else if (mContentView instanceof RecyclerView) {\n            mLoadMoreHandler = new RecyclerViewHandler();\n        } else if (mContentView instanceof LinearLayout &amp;&amp; ((LinearLayout) mRootView).getChildCount() &gt; 0) {\n            View childView = ((LinearLayout) mRootView).getChildAt(0);\n            if (childView instanceof GridView) {\n                mContentView = childView;\n                mLoadMoreHandler = new GridViewHandler();\n            } else if (childView instanceof AbsListView) {\n                mContentView = childView;\n                mLoadMoreHandler = new ListViewHandler();\n            } else if (childView instanceof RecyclerView) {\n                mContentView = childView;\n                mLoadMoreHandler = new RecyclerViewHandler();\n            }\n        }\n    }\n\n    ...\n}<\/code><\/pre>\n<h2>\u81ea\u5b9a\u4e49PtrFrameLayout<\/h2>\n<pre><code class=\"language-java\">public class MyPullRefreshFrameLayout extends PtrFrameLayout {\n    public MyPullRefreshFrameLayout(Context context) {\n        super(context);\n        initViews();\n    }\n\n    public MyPullRefreshFrameLayout(Context context, AttributeSet attrs) {\n        super(context, attrs);\n        initViews();\n    }\n\n    public MyPullRefreshFrameLayout(Context context, AttributeSet attrs, int defStyle) {\n        super(context, attrs, defStyle);\n        initViews();\n    }\n\n    private void initViews() {\n        ThreeDotScaleHeader header = new ThreeDotScaleHeader(getContext());\n        setHeaderView(header);\n        addPtrUIHandler(header);\n\n        ILoadMoreViewFactory loadMoreViewFactory = new DefaultLoadMoreViewFooter();\n        setFooterView(loadMoreViewFactory);\n    }\n\n    private float startY;\n    private float startX;\n    \/\/ \u8bb0\u5f55viewPager\u662f\u5426\u62d6\u62fd\u7684\u6807\u8bb0\n    private boolean mIsHorizontalMove;\n    \/\/ \u8bb0\u5f55\u4e8b\u4ef6\u662f\u5426\u5df2\u88ab\u5206\u53d1\n    private boolean isDeal;\n    private int mTouchSlop = 0;\n\n    @Override\n    public boolean dispatchTouchEvent(MotionEvent ev) {\n        int action = ev.getAction();\n        switch (action) {\n            case MotionEvent.ACTION_DOWN:\n                \/\/ \u8bb0\u5f55\u624b\u6307\u6309\u4e0b\u7684\u4f4d\u7f6e\n                startY = ev.getY();\n                startX = ev.getX();\n                \/\/ \u521d\u59cb\u5316\u6807\u8bb0\n                mIsHorizontalMove = false;\n                isDeal = false;\n                break;\n            case MotionEvent.ACTION_MOVE:\n                \/\/ \u5982\u679c\u5df2\u7ecf\u5224\u65ad\u51fa\u662f\u5426\u7531\u6a2a\u5411\u8fd8\u662f\u7eb5\u5411\u5904\u7406\uff0c\u5219\u8df3\u51fa\n                if (isDeal) {\n                    break;\n                }\n                \/**\u62e6\u622a\u7981\u6b62\u4ea4\u7ed9Ptr\u7684 dispatchTouchEvent\u5904\u7406**\/\n                mIsHorizontalMove = true;\n                \/\/ \u83b7\u53d6\u5f53\u524d\u624b\u6307\u4f4d\u7f6e\n                float endY = ev.getY();\n                float endX = ev.getX();\n                float distanceX = Math.abs(endX - startX);\n                float distanceY = Math.abs(endY - startY);\n                if (distanceX != distanceY) {\n                    \/\/ \u5982\u679cX\u8f74\u4f4d\u79fb\u5927\u4e8eY\u8f74\u4f4d\u79fb\uff0c\u90a3\u4e48\u5c06\u4e8b\u4ef6\u4ea4\u7ed9viewPager\u5904\u7406\n                    if (distanceX &gt; mTouchSlop &amp;&amp; distanceX &gt; distanceY) {\n                        mIsHorizontalMove = true;\n                        isDeal = true;\n                    } else if (distanceY &gt; mTouchSlop) {\n                        mIsHorizontalMove = false;\n                        isDeal = true;\n                    }\n                }\n                break;\n            case MotionEvent.ACTION_UP:\n            case MotionEvent.ACTION_CANCEL:\n                \/\/\u4e0b\u62c9\u5237\u65b0\u72b6\u6001\u65f6\u5982\u679c\u6eda\u52a8\u4e86ViewPager \u6b64\u65f6mIsHorizontalMove\u4e3atrue \u4f1a\u5bfc\u81f4PtrFrameLayout\u65e0\u6cd5\u6062\u590d\u539f\u4f4d\n                \/\/\u521d\u59cb\u5316\u6807\u8bb0\n                mIsHorizontalMove = false;\n                isDeal = false;\n                break;\n        }\n        if (mIsHorizontalMove) {\n            return dispatchTouchEventSupper(ev);\n        }\n        return super.dispatchTouchEvent(ev);\n    }\n}<\/code><\/pre>\n<h2>XML\u5e03\u5c40<\/h2>\n<pre><code class=\"language-xml\">&lt;me.yezhou.lib.ui_widget.layout.MyPullRefreshFrameLayout\n    android:id=&quot;@+id\/layout_pull_refresh&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;0dp&quot;\n    android:layout_weight=&quot;1&quot;\n    &gt;\n    &lt;LinearLayout\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;match_parent&quot;\n        android:orientation=&quot;vertical&quot;\n        &gt;\n        &lt;android.support.v7.widget.RecyclerView\n            android:id=&quot;@+id\/rv_order_list&quot;\n            android:layout_width=&quot;match_parent&quot;\n            android:layout_height=&quot;match_parent&quot;\n            \/&gt;\n        &lt;include\n            android:id=&quot;@+id\/layout_order_empty&quot;\n            layout=&quot;@layout\/layout_order_empty&quot;\n            android:layout_width=&quot;fill_parent&quot;\n            android:layout_height=&quot;fill_parent&quot;\n            android:visibility=&quot;gone&quot;\n            \/&gt;\n    &lt;\/LinearLayout&gt;\n&lt;\/me.yezhou.lib.ui_widget.layout.MyPullRefreshFrameLayout&gt;<\/code><\/pre>\n<h2>\u8bbe\u7f6e\u76d1\u542c\u5668<\/h2>\n<pre><code class=\"language-java\">mOrderListAdapter = new OrderListAdapter(mActivity, mOrderList);\nmOrderListAdapterWithHF = new RecyclerAdapterWithHF(mOrderListAdapter);\nmOrderListRecyclerView.setAdapter(mOrderListAdapterWithHF);<\/code><\/pre>\n<h2>\u4e0b\u62c9\u76d1\u542c<\/h2>\n<pre><code class=\"language-java\">mLayoutPullRefresh.setPtrHandler(new PtrDefaultHandler() {\n    @Override\n    public void onRefreshBegin(PtrFrameLayout frame) {\n        mIsPullRefresh = true;\n\n    }\n\n    @Override\n    public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {\n        return super.checkCanDoRefresh(frame, mOrderListRecyclerView == null ? content : mOrderListRecyclerView, header);\n        \/\/return super.checkCanDoRefresh(frame, \u9700\u8981\u5237\u65b0\u7684\u5f53\u524dView == null ? content : \u9700\u8981\u5237\u65b0\u7684\u5f53\u524dView , header);\n    }\n});<\/code><\/pre>\n<h2>\u4e0a\u62c9\u52a0\u8f7d\u66f4\u591a<\/h2>\n<pre><code class=\"language-java\">mLayoutPullRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {\n    @Override\n    public void loadMore() {\n        mIsPullRefresh = false;\n\n    }\n});<\/code><\/pre>\n<h2>\u505c\u6b62\u5237\u65b0<\/h2>\n<pre><code class=\"language-java\">@Override\npublic void onGetOrderListSuccess(List&lt;Order&gt; orderList, int[] totals) {\n    if (mIsPullRefresh) {\n        mLayoutPullRefresh.refreshComplete();\n        mLayoutPullRefresh.setLoadMoreEnable(true);\n    } else {\n        if (orderList.size() &lt; totals[0]) {\n            mLayoutPullRefresh.loadMoreComplete(true);\n        } else {\n            mLayoutPullRefresh.loadMoreComplete(false);\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>CommonPullToRefresh\uff1ahttps:\/\/github.com\/Chanven\/CommonPu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-949","post","type-post","status-publish","format-standard","hentry","category-android-ui"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=949"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/949\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}