|
1 |
| -package com.example.plugintest.fragment; |
2 |
| - |
3 |
| -import android.content.Context; |
4 |
| -import android.os.Bundle; |
5 |
| -import android.support.v4.app.Fragment; |
6 |
| -import android.util.Log; |
7 |
| -import android.view.LayoutInflater; |
8 |
| -import android.view.View; |
9 |
| -import android.view.View.OnClickListener; |
10 |
| -import android.view.ViewGroup; |
11 |
| -import android.widget.Button; |
12 |
| -import android.widget.Toast; |
13 |
| - |
14 |
| -import com.example.plugintest.R; |
15 |
| -import com.example.plugintest.R.id; |
16 |
| -import com.example.plugintest.R.layout; |
17 |
| -import com.example.plugintest.R.string; |
18 |
| -import com.plugin.core.PluginLoader; |
19 |
| - |
20 |
| -/** |
21 |
| - * 插件中引用主程序资源文件需要显式的指定引用的R 因为主程序的资源id每次编译时都会变化 所以使用主程序资源id的时候必须使用引用 |
22 |
| - * 而不是id的const常量, 因此在插件工程code-gen时没有合并依赖库的R,(合并后会获得依赖库R的id常量) |
23 |
| - * 而是将依赖库的R文件作为编译时的classpath引用 反编译插件可以看到,插件资源id都替换成了常量,二非插件id还是保留R.id的引用形式 |
24 |
| - * 这正是我们想要的结果 |
25 |
| - * |
26 |
| - * 携带插件相关的代码所有使用context的地方,需要使用PluginLoader.getPluginContext()获取的Context |
27 |
| - * |
28 |
| - * @author cailiming |
29 |
| - * |
30 |
| - */ |
31 |
| -public class PluginSpecFragment extends Fragment implements OnClickListener { |
32 |
| - |
33 |
| - private ViewGroup mRoot; |
34 |
| - private Context pluginContext; |
35 |
| - private LayoutInflater pluginInflater; |
36 |
| - |
37 |
| - @Override |
38 |
| - public void onCreate(Bundle savedInstanceState) { |
39 |
| - super.onCreate(savedInstanceState); |
40 |
| - |
41 |
| - getActivity().setTitle("测试插件自由模式的Fragment"); |
42 |
| - |
43 |
| - pluginContext = PluginLoader.getPluginContext(PluginSpecFragment.class); |
44 |
| - |
45 |
| - //设置主题为宿主程序主题 |
46 |
| - //pluginContext.setTheme(getActivity().getApplicationInfo().theme); |
47 |
| - //设置主题为插件程序主题 |
48 |
| - pluginContext.setTheme(R.style.AppTheme); |
49 |
| - |
50 |
| - pluginInflater = (LayoutInflater) pluginContext |
51 |
| - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
52 |
| - } |
53 |
| - |
54 |
| - @Override |
55 |
| - public View onCreateView(LayoutInflater inflater, ViewGroup container, |
56 |
| - Bundle savedInstanceState) { |
57 |
| - |
58 |
| - View scrollview = pluginInflater.inflate(R.layout.plugin_layout, |
59 |
| - null); |
60 |
| - |
61 |
| - mRoot = (ViewGroup) scrollview.findViewById(R.id.content); |
62 |
| - |
63 |
| - initViews(); |
64 |
| - |
65 |
| - return scrollview; |
66 |
| - } |
67 |
| - |
68 |
| - public void initViews() { |
69 |
| - |
70 |
| - Button btn1 = (Button) mRoot.findViewById(R.id.plugin_test_btn1); |
71 |
| - btn1.setOnClickListener(this); |
72 |
| - |
73 |
| - Button btn2 = (Button) mRoot.findViewById(R.id.plugin_test_btn2); |
74 |
| - btn2.setOnClickListener(this); |
75 |
| - |
76 |
| - Button btn3 = (Button) mRoot.findViewById(R.id.plugin_test_btn3); |
77 |
| - btn3.setOnClickListener(this); |
78 |
| - |
79 |
| - Button btn4 = (Button) mRoot.findViewById(R.id.plugin_test_btn4); |
80 |
| - btn4.setOnClickListener(this); |
81 |
| - |
82 |
| - } |
83 |
| - |
84 |
| - @Override |
85 |
| - public void onClick(View v) { |
86 |
| - Log.v("v.click MainFragment", "" + v.getId()); |
87 |
| - if (v.getId() == R.id.plugin_test_btn1) { |
88 |
| - View view = pluginInflater.inflate(R.layout.plugin_layout, null, |
89 |
| - false); |
90 |
| - mRoot.addView(view); |
91 |
| - Toast.makeText(this.getActivity(), getString(R.string.hello_world1), Toast.LENGTH_LONG).show(); |
92 |
| - } else if (v.getId() == R.id.plugin_test_btn2) { |
93 |
| - View view = pluginInflater |
94 |
| - .inflate(com.example.pluginsharelib.R.layout.share_main, |
95 |
| - null, false); |
96 |
| - mRoot.addView(view); |
97 |
| - Toast.makeText(this.getActivity(), getString(com.example.pluginsharelib.R.string.share_string_1), Toast.LENGTH_LONG).show(); |
98 |
| - } else if (v.getId() == R.id.plugin_test_btn3) { |
99 |
| - View view = LayoutInflater.from(getActivity()) |
100 |
| - .inflate(com.example.pluginsharelib.R.layout.share_main, |
101 |
| - null, false); |
102 |
| - mRoot.addView(view); |
103 |
| - } else if (v.getId() == R.id.plugin_test_btn4) { |
104 |
| - ((Button) v).setText(com.example.pluginsharelib.R.string.share_string_2); |
105 |
| - } |
106 |
| - } |
107 |
| -} |
| 1 | +package com.example.plugintest.fragment; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.v4.app.Fragment; |
| 6 | +import android.util.Log; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.view.View.OnClickListener; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.Button; |
| 12 | +import android.widget.Toast; |
| 13 | + |
| 14 | +import com.example.plugintest.R; |
| 15 | +import com.example.plugintest.R.id; |
| 16 | +import com.example.plugintest.R.layout; |
| 17 | +import com.example.plugintest.R.string; |
| 18 | +import com.plugin.core.PluginLoader; |
| 19 | + |
| 20 | +/** |
| 21 | + * 插件中引用主程序资源文件需要显式的指定引用的R 因为主程序的资源id每次编译时都会变化 所以使用主程序资源id的时候必须使用引用 |
| 22 | + * 而不是id的const常量, 因此在插件工程code-gen时没有合并依赖库的R,(合并后会获得依赖库R的id常量) |
| 23 | + * 而是将依赖库的R文件作为编译时的classpath引用 反编译插件可以看到,插件资源id都替换成了常量,二非插件id还是保留R.id的引用形式 |
| 24 | + * 这正是我们想要的结果 |
| 25 | + * |
| 26 | + * 携带插件相关的代码所有使用context的地方,需要使用PluginLoader.getPluginContext()获取的Context |
| 27 | + * |
| 28 | + * @author cailiming |
| 29 | + * |
| 30 | + */ |
| 31 | +public class PluginSpecFragment extends Fragment implements OnClickListener { |
| 32 | + |
| 33 | + private ViewGroup mRoot; |
| 34 | + private Context pluginContext; |
| 35 | + private LayoutInflater pluginInflater; |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onCreate(Bundle savedInstanceState) { |
| 39 | + super.onCreate(savedInstanceState); |
| 40 | + |
| 41 | + getActivity().setTitle("测试插件自由模式的Fragment"); |
| 42 | + |
| 43 | + pluginContext = PluginLoader.getPluginContext(PluginSpecFragment.class); |
| 44 | + |
| 45 | + // 设置主题为宿主程序主题 |
| 46 | + // pluginContext.setTheme(getActivity().getApplicationInfo().theme); |
| 47 | + // 设置主题为插件程序主题 |
| 48 | + pluginContext.setTheme(R.style.PluginTheme); |
| 49 | + |
| 50 | + pluginInflater = (LayoutInflater) pluginContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 55 | + |
| 56 | + View scrollview = pluginInflater.inflate(R.layout.plugin_layout, null); |
| 57 | + |
| 58 | + mRoot = (ViewGroup) scrollview.findViewById(R.id.content); |
| 59 | + |
| 60 | + initViews(); |
| 61 | + |
| 62 | + return scrollview; |
| 63 | + } |
| 64 | + |
| 65 | + public void initViews() { |
| 66 | + |
| 67 | + Button btn1 = (Button) mRoot.findViewById(R.id.plugin_test_btn1); |
| 68 | + btn1.setOnClickListener(this); |
| 69 | + |
| 70 | + Button btn2 = (Button) mRoot.findViewById(R.id.plugin_test_btn2); |
| 71 | + btn2.setOnClickListener(this); |
| 72 | + |
| 73 | + Button btn3 = (Button) mRoot.findViewById(R.id.plugin_test_btn3); |
| 74 | + btn3.setOnClickListener(this); |
| 75 | + |
| 76 | + Button btn4 = (Button) mRoot.findViewById(R.id.plugin_test_btn4); |
| 77 | + btn4.setOnClickListener(this); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void onClick(View v) { |
| 83 | + Log.v("v.click MainFragment", "" + v.getId()); |
| 84 | + if (v.getId() == R.id.plugin_test_btn1) { |
| 85 | + View view = pluginInflater.inflate(R.layout.plugin_layout, null, false); |
| 86 | + mRoot.addView(view); |
| 87 | + Toast.makeText(this.getActivity(), getString(R.string.hello_world1), Toast.LENGTH_LONG).show(); |
| 88 | + } else if (v.getId() == R.id.plugin_test_btn2) { |
| 89 | + View view = pluginInflater.inflate(com.example.pluginsharelib.R.layout.share_main, null, false); |
| 90 | + mRoot.addView(view); |
| 91 | + Toast.makeText(this.getActivity(), getString(com.example.pluginsharelib.R.string.share_string_1), |
| 92 | + Toast.LENGTH_LONG).show(); |
| 93 | + } else if (v.getId() == R.id.plugin_test_btn3) { |
| 94 | + View view = LayoutInflater.from(getActivity()).inflate(com.example.pluginsharelib.R.layout.share_main, |
| 95 | + null, false); |
| 96 | + mRoot.addView(view); |
| 97 | + } else if (v.getId() == R.id.plugin_test_btn4) { |
| 98 | + ((Button) v).setText(com.example.pluginsharelib.R.string.share_string_2); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments