Atlas插件bundle与host通信之接口方式

在公共library中定义中间件接口

1
2
3
4
5
package cn.appblog.example_common.middleware;

public interface ISwitchMainTabListener {
void onSwitchTab(int position);
}

单例封装接口监听器的Getter与Setter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package cn.appblog.example_common.middleware;

import android.content.Context;

public class MiddlewareHelper {
private static MiddlewareHelper mMiddlewareHelper;
private Context mContext;
private ISwitchMainTabListener mSwitchMainTabListener;

private MiddlewareHelper(Context context) {
mContext = context;
}

public static MiddlewareHelper getInstance(Context context) {
if (mMiddlewareHelper == null) {
synchronized (MiddlewareHelper.class) {
if (mMiddlewareHelper == null) {
mMiddlewareHelper = new MiddlewareHelper(context.getApplicationContext());
}
}
}
return mMiddlewareHelper;
}

public ISwitchMainTabListener getSwitchMainTabListener() {
return mSwitchMainTabListener;
}

public void setSwitchMainTabListener(ISwitchMainTabListener listener) {
this.mSwitchMainTabListener = listener;
}
}

在host中设置监听

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MainActivity extends AppCompatActivity implements ISwitchMainTabListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MiddlewareHelper.getInstance(this).setSwitchMainTabListener(this);
}

@Override
public void onSwitchTab(int position) {
mViewPager.setCurrentItem(position, false);
}
}

在bundle中发送消息

1
MiddlewareHelper.getInstance(context).getSwitchMainTabListener().onSwitchTab(0);

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :