{"id":44,"date":"2021-09-28T11:01:09","date_gmt":"2021-09-28T03:01:09","guid":{"rendered":"http:\/\/blog.pbin.xin\/?p=44"},"modified":"2026-03-03T21:04:24","modified_gmt":"2026-03-03T13:04:24","slug":"%e3%80%90android%e3%80%91%e4%ba%8b%e4%bb%b6%e5%88%86%e5%8f%91","status":"publish","type":"post","link":"http:\/\/blog.percy.homes\/index.php\/2021\/09\/28\/%e3%80%90android%e3%80%91%e4%ba%8b%e4%bb%b6%e5%88%86%e5%8f%91\/","title":{"rendered":"\u3010Android\u3011\u4e8b\u4ef6\u5206\u53d1"},"content":{"rendered":"<h1>\u3010\u7b14\u8bb0\u3011\u3010Android\u3011\u4e8b\u4ef6\u5206\u53d1<\/h1>\n<h2>\u7b80\u8ff0<\/h2>\n<p>&nbsp;\u5728Android\u5f00\u53d1\u4e2d\u4f1a\u9047\u5230\u591a\u4e2aView\u3001ViewGroup\u5d4c\u5957\u7684\u60c5\u666f\uff0c\u4f8b\u5982ViewPager\u4e2d\u5d4c\u5957Fragment\uff0c\u540c\u65f6\u5728Fragment\u5185\u5d4c\u5957\u4e00\u4e2a\u53ef\u6eda\u52a8\u7684RecyclerView\uff0c\u5c31\u4f1a\u51fa\u73b0ViewPager\u548cRecyclerView\u7684\u6ed1\u52a8\u4e8b\u4ef6\u5c31\u4f1a\u51b2\u7a81\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u5904\u7406\u89e6\u6478\u4e8b\u4ef6\u3002<\/p>\n<p>&nbsp;\u4e00\u6b21\u5b8c\u6574\u7684\u4e8b\u4ef6\u4f20\u9012\u5206\u4e3a\u4e09\u4e2a\u9636\u6bb5\uff0c\u5206\u522b\u662f\u4e8b\u4ef6\u7684\u5206\u53d1\u3001\u62e6\u622a\u548c\u6d88\u8d39\u3002<\/p>\n<h2>\u89e6\u6478\u4e8b\u4ef6\u548c\u7c7b\u578b<\/h2>\n<p>\u89e6\u6478\u65f6\u95f4\u5bf9\u5e94\u7684\u662fMotionEvent\u7c7b, \u4e8b\u4ef6\u7684\u7c7b\u578b\u4e3b\u8981\u6709\u4e09\u79cd:<br \/>\n&#8211; ACTION_DOWN: \u7528\u6237\u7684\u6309\u4e0b\u64cd\u4f5c, \u4e00\u4e2a\u6309\u4e0b\u64cd\u4f5c\u6807\u5fd7\u7740\u4e00\u6b21\u89e6\u6478\u65f6\u95f4\u7684\u5f00\u59cb\u3002<br \/>\n&#8211; ACTION_MOVE: \u7528\u6237\u6309\u4e0b\u540e\u677e\u5f00\u524d, \u5982\u679c\u79fb\u52a8\u7684\u8ddd\u79bb\u8d85\u8fc7\u9608\u503c, \u5219\u5224\u5b9a\u4e3aACTION_MOVE\u64cd\u4f5c\u3002<br \/>\n&#8211; ACTION_UP: \u7528\u6237\u62ac\u8d77\u64cd\u4f5c, \u4e00\u4e2a\u62ac\u8d77\u64cd\u4f5c\u6807\u5fd7\u7740\u4e00\u6b21\u89e6\u6478\u4e8b\u4ef6\u7684\u7ed3\u675f\u3002<\/p>\n<p>\u5728\u4e00\u6b21\u5c4f\u5e55\u89e6\u6478\u64cd\u4f5c\u4e2d\uff0cACTION_DOWN\u548cACTION_UP\u8fd9\u4e24\u4e2a\u4e8b\u4ef6\u662f\u5fc5\u9700\u7684, \u800cACTION_MOVE\u9700\u79fb\u52a8\u89e6\u53d1\u3002<\/p>\n<h2>\u4e8b\u4ef6\u4f20\u9012\u7684\u4e09\u4e2a\u9636\u6bb5<\/h2>\n<ul>\n<li>\u5206\u53d1(Dispatch): \u4e8b\u4ef6\u7684\u5206\u53d1\u5bf9\u5e94<code>dispatchTouchEvent<\/code>\u65b9\u6cd5 , \u5728Android\u4e2d , \u6240\u6709\u7684\u89e6\u6478\u4e8b\u4ef6\u90fd\u662f\u901a\u8fc7\u8fd9\u4e2a\u65b9\u6cd5\u5206\u53d1\u7684:<\/li>\n<\/ul>\n<pre><code class=\"kotlin\">override fun dispatchTouchEvent(event: MotionEvent?): Boolean\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u56fd,\u6839\u636e\u5b9e\u9645\u7684\u5b9e\u73b0\u903b\u8f91\u6765\u51b3\u5b9a\u8fd9\u4e2a\u4e8b\u4ef6\u662f\u5426\u7ee7\u7eed\u5206\u53d1\u7ed9\u5b50\u89c6\u56fe\u5904\u7406\u3002\u5f53\u65b9\u6cd5\u8fd4\u56de<code>true<\/code>\u65f6, \u8868\u793a\u8be5\u4e8b\u4ef6\u88ab\u6d88\u8d39, \u4e0d\u518d\u5206\u53d1; \u65b9\u6cd5\u8fd4\u56de<code>super.dispatchTouchEvent<\/code>\u5219\u8868\u793a\u7ee7\u7eed\u5206\u53d1\u4e8b\u4ef6\u3002\u5982\u679c\u5f53\u524d\u89c6\u56fe\u662f<code>ViewGroup<\/code>\u53ca\u5176\u5b50\u7c7b, \u5219\u4f1a\u8c03\u7528<code>onInterceptTouchEvent<\/code>\u5224\u5b9a\u662f\u5426\u62e6\u622a\u8be5\u4e8b\u4ef6\u3002<\/p>\n<ul>\n<li>\u62e6\u622a(Intercept) : \u4e8b\u4ef6\u7684\u62e6\u622a\u5bf9\u5e94\u7740<code>onInterceptTouchEvent<\/code>, \u8fd9\u4e2a\u65b9\u6cd5\u4ec5\u5728<code>ViewGroup<\/code>\u53ca\u5176\u5b50\u7c7b\u5b58\u5728, \u5728<code>View<\/code>\u548c<code>Activity<\/code>\u4e2d\u4e0d\u5b58\u5728\u3002<\/li>\n<\/ul>\n<pre><code class=\"kotlin\">override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean\n<\/code><\/pre>\n<p>\u540c\u7406, \u6b64\u65b9\u6cd5\u4e5f\u662f\u901a\u8fc7\u8fd4\u56de\u503c\u6765\u51b3\u5b9a\u662f\u5426\u62e6\u622a\u4e8b\u4ef6 , \u5f53\u8fd4\u56de<code>true<\/code>\u65f6, \u8868\u793a\u62e6\u622a\u4e8b\u4ef6, \u4e0d\u7ee7\u7eed\u5206\u53d1\u7ed9\u5b50\u89c6\u56fe, \u540c\u65f6\u4ea4\u7531\u81ea\u8eab\u7684<code>onTouchEvent<\/code>\u65b9\u6cd5\u8fdb\u884c\u6d88\u8d39\u3002<\/p>\n<ul>\n<li>\u6d88\u8d39(Consume) : \u4e8b\u4ef6\u7684\u6d88\u8d39\u5bf9\u5e94<code>OnTouchEvent<\/code>\u65b9\u6cd5:<\/li>\n<\/ul>\n<pre><code class=\"kotlin\">override fun onTouchEvent(event: MotionEvent?): Boolean\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u8fd4\u56de<code>true<\/code>\u8868\u793a\u5f53\u524dView\u53ef\u4ee5\u5904\u7406\u6d88\u8d39\u5bf9\u5e94\u7684\u4e8b\u4ef6 , \u4e8b\u4ef6\u5c06\u4e0d\u4f1a\u5411\u4e0a\u4f20\u9012\u7ed9\u7236View; \u8fd4\u56de<code>false<\/code>\u8868\u793a\u4e0d\u6d88\u8d39\u4e8b\u4ef6, \u4e8b\u4ef6\u4f1a\u5411\u4e0a\u4f20\u9012\u7ed9\u4fef\u89c6\u56fe\u7684OnTouchEvent\u65b9\u6cd5\u3002<\/p>\n<p>\u62e5\u6709\u4e8b\u4ef6\u4f20\u9012\u5904\u7406\u80fd\u529b\u7684\u7c7b\u6709\u4e09\u4e2a:<br \/>\n&#8211; Avtivity: <code>dispatchTouchEvent<\/code>\u548c<code>onTouchEvent<\/code><br \/>\n&#8211; ViewGroup : <code>dispatchTouchEvent<\/code>\u3001<code>onInterceptTouchEvent<\/code>\u548c<code>onTouchEvent<\/code><br \/>\n&#8211; View : <code>dispatchTouchEvent<\/code>\u548c<code>onTouchEvent<\/code><\/p>\n<h2>View\u7684\u4e8b\u4ef6\u4f20\u9012\u673a\u5236<\/h2>\n<p>\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u7ee7\u627fTextView\u7684\u7c7bMyTextView (\u4e0d\u7ee7\u627fViewGroup\u53ca\u5176\u5b50\u7c7b)\u3002<\/p>\n<pre><code class=\"kotlin\">class MyTextView : TextView {\n    private val TAG=\"binbinMyTextView\"\n\n    \u2026\u2026\u2026\u2026\n\n    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {\n        var actionStr=\"\"\n        when (event?.action){\n            MotionEvent.ACTION_DOWN -&gt;{\n                actionStr=\"ACTION_DOWN\"\n            }\n            MotionEvent.ACTION_UP -&gt;{\n                actionStr=\"ACTION_UP\"\n            }\n            MotionEvent.ACTION_MOVE -&gt;{\n                actionStr=\"ACTION_MOVE\"\n            }\n            MotionEvent.ACTION_CANCEL-&gt;{\n                actionStr=\"ACTION_CANCEL\"\n            }\n            else -&gt;{\n                actionStr=\"else\"\n            }\n        }\n        e(TAG,\"dispatchTouchEvent $actionStr\")\n        return super.dispatchTouchEvent(event)\n    }\n\n    override fun onTouchEvent(event: MotionEvent?): Boolean {\n        var actionStr=\"\"\n        when (event?.action){\n            MotionEvent.ACTION_DOWN -&gt;{\n                actionStr=\"ACTION_DOWN\"\n            }\n            MotionEvent.ACTION_UP -&gt;{\n                actionStr=\"ACTION_UP\"\n            }\n            MotionEvent.ACTION_MOVE -&gt;{\n                actionStr=\"ACTION_MOVE\"\n            }\n            MotionEvent.ACTION_CANCEL-&gt;{\n                actionStr=\"ACTION_CANCEL\"\n            }\n            else -&gt;{\n                actionStr=\"else\"\n            }\n        }\n        e(TAG,\"onTouchEvent   $actionStr\")\n        return super.onTouchEvent(event)\n    }\n\n}\n<\/code><\/pre>\n<p>\u7136\u540e\u518d\u5b9a\u4e49\u4e00\u4e2aActivity\u5c55\u793aMyTextView\u5e76\u8bbe\u7f6e\u70b9\u51fb\u548c\u89e6\u6478\u76d1\u542c\u3002<\/p>\n<pre><code class=\"kotlin\">class MainActivity : AppCompatActivity() ,View.OnTouchListener,View.OnClickListener{\n    private val TAG=\"binbinMainActivity\"\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n        findViewById&lt;MyTextView&gt;(R.id.myTextView).setOnClickListener(this)\n        findViewById&lt;MyTextView&gt;(R.id.myTextView).setOnTouchListener(this)\n    }\n\n    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {\n        var actionStr=\"\"\n        when (ev?.action){\n            MotionEvent.ACTION_DOWN -&gt;{\n                actionStr=\"ACTION_DOWN\"\n            }\n            MotionEvent.ACTION_UP -&gt;{\n                actionStr=\"ACTION_UP\"\n            }\n            MotionEvent.ACTION_MOVE -&gt;{\n                actionStr=\"ACTION_MOVE\"\n            }\n            MotionEvent.ACTION_CANCEL-&gt;{\n                actionStr=\"ACTION_CANCEL\"\n            }\n            else -&gt;{\n                actionStr=\"else\"\n            }\n        }\n        e(TAG,\"dispatchTouchEvent  $actionStr\")\n        return super.dispatchTouchEvent(ev)\n    }\n\n    override fun onTouchEvent(event: MotionEvent?): Boolean {\n        var actionStr=\"\"\n        when (event?.action){\n            MotionEvent.ACTION_DOWN -&gt;{\n                actionStr=\"ACTION_DOWN\"\n            }\n            MotionEvent.ACTION_UP -&gt;{\n                actionStr=\"ACTION_UP\"\n            }\n            MotionEvent.ACTION_MOVE -&gt;{\n                actionStr=\"ACTION_MOVE\"\n            }\n            MotionEvent.ACTION_CANCEL-&gt;{\n                actionStr=\"ACTION_CANCEL\"\n            }\n            else -&gt;{\n                actionStr=\"else\"\n            }\n        }\n        e(TAG,\"onTouchEvent  $actionStr\")\n        return super.onTouchEvent(event)\n    }\n\n    override fun onTouch(v: View?, event: MotionEvent?): Boolean {\n        when(v?.id){\n            R.id.myTextView -&gt;{\n                var actionStr=\"\"\n                when (event?.action){\n                    MotionEvent.ACTION_DOWN -&gt;{\n                        actionStr=\"ACTION_DOWN\"\n                    }\n                    MotionEvent.ACTION_UP -&gt;{\n                        actionStr=\"ACTION_UP\"\n                    }\n                    MotionEvent.ACTION_MOVE -&gt;{\n                        actionStr=\"ACTION_MOVE\"\n                    }\n                    MotionEvent.ACTION_CANCEL-&gt;{\n                        actionStr=\"ACTION_CANCEL\"\n                    }\n                    else -&gt;{\n                        actionStr=\"else\"\n                    }\n                }\n                e(TAG,\"onTouch  $actionStr\")\n            }\n        }\n        return super.onTouchEvent(event)\n    }\n\n    override fun onClick(v: View?) {\n        when(v?.id){\n            R.id.myTextView -&gt;{\n                e(TAG,\"onClick\")\n            }\n        }\n    }\n\n}\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4ee3\u7801, \u70b9\u51fbMyTextView, \u5f97\u5230\u5982\u4e0b\u65e5\u5fd7:<\/p>\n<pre><code class=\"java\">com.bin.toucheventtest E\/binbinMainActivity: dispatchTouchEvent  ACTION_DOWN\ncom.bin.toucheventtest E\/binbinMyTextView: dispatchTouchEvent ACTION_DOWN\ncom.bin.toucheventtest E\/binbinMainActivity: onTouch  ACTION_DOWN\ncom.bin.toucheventtest E\/binbinMyTextView: onTouchEvent   ACTION_DOWN\ncom.bin.toucheventtest E\/binbinMainActivity: dispatchTouchEvent  ACTION_UP\ncom.bin.toucheventtest E\/binbinMyTextView: dispatchTouchEvent ACTION_UP\ncom.bin.toucheventtest E\/binbinMainActivity: onTouch  ACTION_UP\ncom.bin.toucheventtest E\/binbinMyTextView: onTouchEvent   ACTION_UP\ncom.bin.toucheventtest E\/binbinMainActivity: onClick\n<\/code><\/pre>\n<p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u770b\u51fa, dispatchTouchEvent\u3001onTouchEvent\u4e24\u4e2a\u65b9\u6cd5\u8fd4\u56de\u503c\u6709\u4e09\u79cd\u60c5\u51b5:<br \/>\n&#8211; false<br \/>\n&#8211; true<br \/>\n&#8211; super.\u540c\u540d\u65b9\u6cd5<\/p>\n<p>\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u4f7f\u4e8b\u4ef6\u4f20\u9012\u6d41\u7a0b\u8d70\u5411\u4e0d\u540c\u3002<\/p>\n<p>ACTION_DOWN\u4e8b\u4ef6\u7684\u5904\u7406\u6d41\u7a0b\u5982\u56fe:<\/p>\n<p><img decoding=\"async\" alt=\"\u56fe\u7247\u63cf\u8ff0\" src=\"http:\/\/43.138.204.135:7507\/images\/view.png\" style=\"zoom:50%;\"><\/p>\n<h2>\u672a\u5b8c\u5f85\u7eed<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u7b14\u8bb0\u3011\u3010Android\u3011\u4e8b\u4ef6\u5206\u53d1 \u7b80\u8ff0 &nbsp;\u5728Android\u5f00\u53d1\u4e2d\u4f1a\u9047\u5230\u591a\u4e2aView\u3001ViewGro [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/posts\/44"}],"collection":[{"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":1,"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":99,"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions\/99"}],"wp:attachment":[{"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.percy.homes\/index.php\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}