{"id":1216,"date":"2023-03-18T06:58:04","date_gmt":"2023-03-17T22:58:04","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1216"},"modified":"2023-04-29T09:40:02","modified_gmt":"2023-04-29T01:40:02","slug":"interface-method-for-communication-between-atlas-plugin-bundle-and-host","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/interface-method-for-communication-between-atlas-plugin-bundle-and-host\/","title":{"rendered":"Atlas\u63d2\u4ef6bundle\u4e0ehost\u901a\u4fe1\u4e4b\u63a5\u53e3\u65b9\u5f0f"},"content":{"rendered":"<h2>\u5728\u516c\u5171library\u4e2d\u5b9a\u4e49\u4e2d\u95f4\u4ef6\u63a5\u53e3<\/h2>\n<pre><code class=\"language-java\">package cn.appblog.example_common.middleware;\n\npublic interface ISwitchMainTabListener {\n    void onSwitchTab(int position);\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u5355\u4f8b\u5c01\u88c5\u63a5\u53e3\u76d1\u542c\u5668\u7684Getter\u4e0eSetter<\/h2>\n<pre><code class=\"language-java\">package cn.appblog.example_common.middleware;\n\nimport android.content.Context;\n\npublic class MiddlewareHelper {\n    private static MiddlewareHelper mMiddlewareHelper;\n    private Context mContext;\n    private ISwitchMainTabListener mSwitchMainTabListener;\n\n    private MiddlewareHelper(Context context) {\n        mContext = context;\n    }\n\n    public static MiddlewareHelper getInstance(Context context) {\n        if (mMiddlewareHelper == null) {\n            synchronized (MiddlewareHelper.class) {\n                if (mMiddlewareHelper == null) {\n                    mMiddlewareHelper = new MiddlewareHelper(context.getApplicationContext());\n                }\n            }\n        }\n        return mMiddlewareHelper;\n    }\n\n    public ISwitchMainTabListener getSwitchMainTabListener() {\n        return mSwitchMainTabListener;\n    }\n\n    public void setSwitchMainTabListener(ISwitchMainTabListener listener) {\n        this.mSwitchMainTabListener = listener;\n    }\n}<\/code><\/pre>\n<h2>\u5728host\u4e2d\u8bbe\u7f6e\u76d1\u542c<\/h2>\n<pre><code class=\"language-java\">public class MainActivity extends AppCompatActivity implements ISwitchMainTabListener {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n\n        MiddlewareHelper.getInstance(this).setSwitchMainTabListener(this);\n    }\n\n    @Override\n    public void onSwitchTab(int position) {\n        mViewPager.setCurrentItem(position, false);\n    }   \n}<\/code><\/pre>\n<h2>\u5728bundle\u4e2d\u53d1\u9001\u6d88\u606f<\/h2>\n<pre><code class=\"language-java\">MiddlewareHelper.getInstance(context).getSwitchMainTabListener().onSwitchTab(0);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u516c\u5171library\u4e2d\u5b9a\u4e49\u4e2d\u95f4\u4ef6\u63a5\u53e3 package cn.appblog.example_common.mi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[],"class_list":["post-1216","post","type-post","status-publish","format-standard","hentry","category-atlas"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1216","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=1216"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1216\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}