Skip to content

Commit 0b9725e

Browse files
committed
Update Demo
Signed-off-by: limpoxe <405977146@163.com>
1 parent 36729ce commit 0b9725e

File tree

7 files changed

+116
-156
lines changed

7 files changed

+116
-156
lines changed
-45 Bytes
Binary file not shown.

PluginMain/src/com/example/pluginmain/PluginDetailActivity.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,8 @@ private void initViews(PluginDescriptor pluginDescriptor) {
7373

7474
@Override
7575
public void onClick(View v) {
76-
// if ("vivo".equalsIgnoreCase(Build.BRAND) ||
77-
// "oppo".equalsIgnoreCase(Build.BRAND)) {
78-
// 要想步步高手机也支持fragment模式,插件需要使用独立模式,即不通过R直接或者间接使用注宿主程序的资源
79-
// 如果步步高手机也需要使用非独立插件,则要舍弃宿主主题
80-
// Toast.makeText(PluginDetailActivity.this,
81-
// "步步高手机暂时只支持Activity模式!", Toast.LENGTH_LONG).show();
82-
// return;
83-
// }
84-
// 两种fragment模式,
85-
// 一种是显示在内建的Fragment容器中,一种是可嵌入在任意支持fragment的页面
86-
if (entry.getValue().equals("com.example.plugintest.fragment.PluginNormalFragment")) {
87-
PluginDispatcher.startFragmentWithBuildInActivity(PluginDetailActivity.this, entry.getKey());
88-
} else {
89-
PluginDispatcher.startFragmentWithBuildInActivity(PluginDetailActivity.this, entry.getKey());
90-
PluginDispatcher.startFragmentWithSimpleActivity(PluginDetailActivity.this, entry.getKey());
91-
}
92-
76+
// 将插件中的fragment嵌入在宿主程序内建的Fragment容器中展示
77+
PluginDispatcher.startFragmentWithSimpleActivity(PluginDetailActivity.this, entry.getKey());
9378
}
9479
});
9580
pluginFragmentView.addView(btn);

PluginTest/AndroidManifest.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
android:name="com.example.plugintest.PluginTestApplication"
1616
android:label="包含UI的插件demo, 代表5中开发方式,UIclassName要求是唯一的,宿主程序需要根据和className来进行查找">
1717

18-
<!-- for fragment -->
19-
<meta-data android:name="fragmentTest1" android:value="com.example.plugintest.fragment.PluginNormalFragment"/>
20-
2118
<!-- for fragment -->
22-
<meta-data android:name="fragmentTest2" android:value="com.example.plugintest.fragment.PluginSpecFragment"/>
19+
<meta-data android:name="fragmentTest1" android:value="com.example.plugintest.fragment.PluginSpecFragment"/>
2320

2421
<!-- for proxy test -->
2522
<activity

PluginTest/src/com/example/plugintest/activity/PluginFragmentTestActivity.java

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
import android.widget.Toast;
1818

1919
import com.example.plugintest.R;
20-
20+
import com.example.plugintest.fragment.PluginNormalFragment;
21+
2122
/**
2223
*
2324
* @author cailiming
24-
* 放弃proxy方式
2525
*/
26-
@Deprecated
2726
public class PluginFragmentTestActivity extends FragmentActivity {
2827

2928
private static final String LOG_TAG = PluginFragmentTestActivity.class.getSimpleName();
@@ -36,32 +35,31 @@ public void onCreate(Bundle savedInstanceState) {
3635
setContentView(root, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
3736
root.setId(android.R.id.primary);
3837

39-
Fragment fragment = new TestFragment();
38+
Fragment fragment = new PluginNormalFragment();
4039
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
4140
ft.replace(android.R.id.primary, fragment).commit();
4241
}
4342

4443
@Override
4544
public boolean onCreateOptionsMenu(Menu menu) {
46-
menu.add(0,0, 0, "test plugin menu");
45+
menu.add(0, 0, 0, "test plugin menu");
4746
return super.onCreateOptionsMenu(menu);
4847
}
49-
48+
5049
@Override
5150
public boolean onOptionsItemSelected(MenuItem item) {
5251
Toast.makeText(this, "test plugin menu", Toast.LENGTH_LONG).show();
5352
Log.e(LOG_TAG, "" + item.getTitle());
5453
return super.onOptionsItemSelected(item);
5554
}
56-
55+
5756
public static class TestFragment extends Fragment implements OnClickListener {
5857

5958
private ViewGroup mRoot;
6059
private LayoutInflater mInflater;
6160

6261
@Override
63-
public View onCreateView(LayoutInflater inflater, ViewGroup container,
64-
Bundle savedInstanceState) {
62+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
6563
mInflater = getActivity().getLayoutInflater();
6664
View scrollview = mInflater.inflate(R.layout.plugin_layout, null);
6765

@@ -85,33 +83,30 @@ public void initViews() {
8583

8684
Button btn4 = (Button) mRoot.findViewById(R.id.plugin_test_btn4);
8785
btn4.setOnClickListener(this);
88-
86+
8987
}
9088

9189
@Override
9290
public void onClick(View v) {
9391
Log.v("v.click 111", "" + v.getId());
9492
if (v.getId() == R.id.plugin_test_btn1) {
95-
View view = mInflater.inflate(R.layout.plugin_layout, null,
96-
false);
93+
View view = mInflater.inflate(R.layout.plugin_layout, null, false);
9794
mRoot.addView(view);
9895
Toast.makeText(this.getActivity(), getString(R.string.hello_world1), Toast.LENGTH_LONG).show();
9996
} else if (v.getId() == R.id.plugin_test_btn2) {
100-
101-
View view = mInflater
102-
.inflate(com.example.pluginsharelib.R.layout.share_main,
103-
null, false);
97+
98+
View view = mInflater.inflate(com.example.pluginsharelib.R.layout.share_main, null, false);
10499
mRoot.addView(view);
105-
Toast.makeText(this.getActivity(), getString(com.example.pluginsharelib.R.string.share_string_1), Toast.LENGTH_LONG).show();
100+
Toast.makeText(this.getActivity(), getString(com.example.pluginsharelib.R.string.share_string_1),
101+
Toast.LENGTH_LONG).show();
106102
} else if (v.getId() == R.id.plugin_test_btn3) {
107-
View view = LayoutInflater.from(getActivity())
108-
.inflate(com.example.pluginsharelib.R.layout.share_main,
109-
null, false);
103+
View view = LayoutInflater.from(getActivity()).inflate(com.example.pluginsharelib.R.layout.share_main,
104+
null, false);
110105
mRoot.addView(view);
111106
} else if (v.getId() == R.id.plugin_test_btn4) {
112107
((Button) v).setText(com.example.pluginsharelib.R.string.share_string_2);
113108
}
114109
}
115110
}
116-
111+
117112
}

PluginTest/src/com/example/plugintest/activity/PluginTestActivity.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
* 而是将依赖库的R文件作为编译时的classpath引用 反编译插件可以看到,插件资源id都替换成了常量,二非插件id还是保留R.id的引用形式
2121
* 这正是我们想要的结果
2222
*
23-
*不携带插件相关代码的activity。完全就是一个普通的activity
24-
*
25-
*
23+
* 不携带插件相关代码的activity。完全就是一个普通的activity
24+
*
25+
*
2626
* @author cailiming
2727
*
28-
* 放弃proxy方式
2928
*/
3029
@Deprecated
3130
public class PluginTestActivity extends Activity implements OnClickListener {
@@ -36,7 +35,7 @@ public class PluginTestActivity extends Activity implements OnClickListener {
3635
@Override
3736
public void onCreate(Bundle savedInstanceState) {
3837
super.onCreate(savedInstanceState);
39-
38+
4039
setTitle("测试插件中的Activity");
4140
Log.d("xx", getIntent().toUri(0));
4241
mInflater = getLayoutInflater();
@@ -51,17 +50,17 @@ public void onCreate(Bundle savedInstanceState) {
5150

5251
@Override
5352
public boolean onCreateOptionsMenu(Menu menu) {
54-
menu.add(0,0, 0, "test plugin menu");
53+
menu.add(0, 0, 0, "test plugin menu");
5554
return super.onCreateOptionsMenu(menu);
5655
}
57-
56+
5857
@Override
5958
public boolean onOptionsItemSelected(MenuItem item) {
6059
Toast.makeText(this, "test plugin menu", Toast.LENGTH_LONG).show();
6160
Log.e("xx", "" + item.getTitle());
6261
return super.onOptionsItemSelected(item);
6362
}
64-
63+
6564
public void initViews() {
6665

6766
Button btn1 = (Button) mRoot.findViewById(R.id.plugin_test_btn1);
@@ -75,7 +74,7 @@ public void initViews() {
7574

7675
Button btn4 = (Button) mRoot.findViewById(R.id.plugin_test_btn4);
7776
btn4.setOnClickListener(this);
78-
77+
7978
}
8079

8180
@Override
@@ -88,13 +87,14 @@ public void onClick(View v) {
8887
} else if (v.getId() == R.id.plugin_test_btn2) {
8988
View view = mInflater.inflate(com.example.pluginsharelib.R.layout.share_main, null, false);
9089
mRoot.addView(view);
91-
Toast.makeText(this, getString(com.example.pluginsharelib.R.string.share_string_1), Toast.LENGTH_LONG).show();
90+
Toast.makeText(this, getString(com.example.pluginsharelib.R.string.share_string_1), Toast.LENGTH_LONG)
91+
.show();
9292
} else if (v.getId() == R.id.plugin_test_btn3) {
9393
View view = LayoutInflater.from(this).inflate(com.example.pluginsharelib.R.layout.share_main, null, false);
9494
mRoot.addView(view);
9595
} else if (v.getId() == R.id.plugin_test_btn4) {
9696
((Button) v).setText(com.example.pluginsharelib.R.string.share_string_2);
9797
}
9898
}
99-
99+
100100
}
Lines changed: 79 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,79 @@
1-
package com.example.plugintest.fragment;
2-
3-
import com.example.plugintest.R;
4-
5-
import android.os.Bundle;
6-
import android.support.v4.app.Fragment;
7-
import android.util.Log;
8-
import android.view.LayoutInflater;
9-
import android.view.View;
10-
import android.view.View.OnClickListener;
11-
import android.view.ViewGroup;
12-
import android.widget.Button;
13-
import android.widget.Toast;
14-
15-
/**
16-
* 插件中引用主程序资源文件需要显式的指定引用的R 因为主程序的资源id每次编译时都会变化 所以使用主程序资源id的时候必须使用引用
17-
* 而不是id的const常量, 因此在插件工程code-gen时没有合并依赖库的R,(合并后会获得依赖库R的id常量)
18-
* 而是将依赖库的R文件作为编译时的classpath引用 反编译插件可以看到,插件资源id都替换成了常量,二非插件id还是保留R.id的引用形式
19-
* 这正是我们想要的结果
20-
*
21-
*
22-
* 不携带任何插件相关的代理 完全就是一个普通的fragment
23-
* @author cailiming
24-
*
25-
*/
26-
public class PluginNormalFragment extends Fragment implements OnClickListener {
27-
28-
private ViewGroup mRoot;
29-
private LayoutInflater mInflater;
30-
31-
@Override
32-
public void onCreate(Bundle savedInstanceState) {
33-
super.onCreate(savedInstanceState);
34-
}
35-
36-
@Override
37-
public View onCreateView(LayoutInflater inflater, ViewGroup container,
38-
Bundle savedInstanceState) {
39-
40-
getActivity().setTitle("测试插件中非自由模式的Fragment");
41-
42-
mInflater = inflater;
43-
View scrollview = mInflater.inflate(R.layout.plugin_layout, null);
44-
45-
mRoot = (ViewGroup) scrollview.findViewById(R.id.content);
46-
47-
initViews();
48-
49-
return scrollview;
50-
}
51-
52-
public void initViews() {
53-
54-
Button btn1 = (Button) mRoot.findViewById(R.id.plugin_test_btn1);
55-
btn1.setOnClickListener(this);
56-
57-
Button btn2 = (Button) mRoot.findViewById(R.id.plugin_test_btn2);
58-
btn2.setOnClickListener(this);
59-
60-
Button btn3 = (Button) mRoot.findViewById(R.id.plugin_test_btn3);
61-
btn3.setOnClickListener(this);
62-
63-
Button btn4 = (Button) mRoot.findViewById(R.id.plugin_test_btn4);
64-
btn4.setOnClickListener(this);
65-
66-
}
67-
68-
@Override
69-
public void onClick(View v) {
70-
Log.v("v.click 111", "" + v.getId());
71-
if (v.getId() == R.id.plugin_test_btn1) {
72-
View view = mInflater.inflate(R.layout.plugin_layout, null,
73-
false);
74-
mRoot.addView(view);
75-
Toast.makeText(this.getActivity(), getString(R.string.hello_world1), Toast.LENGTH_LONG).show();
76-
} else if (v.getId() == R.id.plugin_test_btn2) {
77-
View view = mInflater
78-
.inflate(com.example.pluginsharelib.R.layout.share_main,
79-
null, false);
80-
mRoot.addView(view);
81-
Toast.makeText(this.getActivity(), getString(com.example.pluginsharelib.R.string.share_string_1), Toast.LENGTH_LONG).show();
82-
} else if (v.getId() == R.id.plugin_test_btn3) {
83-
View view = LayoutInflater.from(getActivity())
84-
.inflate(com.example.pluginsharelib.R.layout.share_main,
85-
null, false);
86-
mRoot.addView(view);
87-
} else if (v.getId() == R.id.plugin_test_btn4) {
88-
((Button) v).setText(com.example.pluginsharelib.R.string.share_string_2);
89-
}
90-
}
91-
}
1+
package com.example.plugintest.fragment;
2+
3+
import com.example.plugintest.R;
4+
5+
import android.os.Bundle;
6+
import android.support.v4.app.Fragment;
7+
import android.util.Log;
8+
import android.view.LayoutInflater;
9+
import android.view.View;
10+
import android.view.View.OnClickListener;
11+
import android.view.ViewGroup;
12+
import android.widget.Button;
13+
import android.widget.Toast;
14+
15+
/**
16+
* 这个fragment会被嵌在 插件 提供的Activity中展示
17+
*/
18+
public class PluginNormalFragment extends Fragment implements OnClickListener {
19+
20+
private ViewGroup mRoot;
21+
private LayoutInflater mInflater;
22+
23+
@Override
24+
public void onCreate(Bundle savedInstanceState) {
25+
super.onCreate(savedInstanceState);
26+
}
27+
28+
@Override
29+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
30+
31+
getActivity().setTitle("测试插件中非自由模式的Fragment");
32+
33+
mInflater = inflater;
34+
View scrollview = mInflater.inflate(R.layout.plugin_layout, null);
35+
36+
mRoot = (ViewGroup) scrollview.findViewById(R.id.content);
37+
38+
initViews();
39+
40+
return scrollview;
41+
}
42+
43+
public void initViews() {
44+
45+
Button btn1 = (Button) mRoot.findViewById(R.id.plugin_test_btn1);
46+
btn1.setOnClickListener(this);
47+
48+
Button btn2 = (Button) mRoot.findViewById(R.id.plugin_test_btn2);
49+
btn2.setOnClickListener(this);
50+
51+
Button btn3 = (Button) mRoot.findViewById(R.id.plugin_test_btn3);
52+
btn3.setOnClickListener(this);
53+
54+
Button btn4 = (Button) mRoot.findViewById(R.id.plugin_test_btn4);
55+
btn4.setOnClickListener(this);
56+
57+
}
58+
59+
@Override
60+
public void onClick(View v) {
61+
Log.v("v.click 111", "" + v.getId());
62+
if (v.getId() == R.id.plugin_test_btn1) {
63+
View view = mInflater.inflate(R.layout.plugin_layout, null, false);
64+
mRoot.addView(view);
65+
Toast.makeText(this.getActivity(), getString(R.string.hello_world1), Toast.LENGTH_LONG).show();
66+
} else if (v.getId() == R.id.plugin_test_btn2) {
67+
View view = mInflater.inflate(com.example.pluginsharelib.R.layout.share_main, null, false);
68+
mRoot.addView(view);
69+
Toast.makeText(this.getActivity(), getString(com.example.pluginsharelib.R.string.share_string_1),
70+
Toast.LENGTH_LONG).show();
71+
} else if (v.getId() == R.id.plugin_test_btn3) {
72+
View view = LayoutInflater.from(getActivity()).inflate(com.example.pluginsharelib.R.layout.share_main,
73+
null, false);
74+
mRoot.addView(view);
75+
} else if (v.getId() == R.id.plugin_test_btn4) {
76+
((Button) v).setText(com.example.pluginsharelib.R.string.share_string_2);
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)