我的应用程序称为MyNiceApp。MyNiceApp 主要只是一个核心,它加载coreView
在 MainActivity 中调用的视图onCreate
。coreView
由用户根据需要下载的其他插件的视图填充。我在核心视图上定义了可以由插件通过 MyNiceApp 中的接口填充的各个区域。如何从插件加载视图并将其传递到coreView
?
有人告诉我RemoteViews是一个不错的选择,但我不知道如何实现它。还有哪些其他选择?
RemoteViews 是最好的方法吗?我愿意尝试任何可行的方法,即使不是最好的方法。一个黑客就可以了。目前,任何可以为该功能提供服务的东西就足够了。以后可以进行改进。
谢谢大家。
更新
我正在考虑将它们托管在我的私人服务器上。它们将被下载到一个名为/data/app/com.myniceapp.plugins的专用文件夹
我认为如果我在 /data/app/com.myniceapp./plugins 下创建一个文件夹,然后让 DexClassLoader 抓取 /data/app/com.myniceapp/plugins 以获取下载的插件,那会更好地组织起来,然后我可以调用我的类实现,并在运行时将插件视图动态加载到核心视图。
临时更新
嗨@lelloman,以及其他所有人。我一直在努力使您的解决方案发挥作用,但到目前为止我一直没有成功。
我创建了一个名为Test View的新项目。它有一个 XML 布局,我尝试将其膨胀并发送到核心视图,如下所示:
package rev.ca.testview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import ca.rev.libs.core.MyViewCreator;
public class TestView implements MyViewCreator {
@Override
public View createMyView(Context context) {
LayoutInflater revInfl = LayoutInflater.from(context);
View toolBarItemsLL = revInfl.inflate(R.layout.layout, null, false);
Button button = (Button) toolBarItemsLL.findViewById(R.id.testButton);
return button;
}
}
但是,这不起作用。这是其余的:
在 MainActivity 视图中,它应该从插件中获取视图:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
String dexPath = "/data/app/rev.ca.testview";
String optimizedDirectory = this.getCacheDir().getAbsolutePath();
String libraryPath = null;
DexClassLoader dexClassLoader = new DexClassLoader(dexPath, optimizedDirectory, null, ClassLoader.getSystemClassLoader());
DexFile dexFile = null;
try {
dexFile = DexFile.loadDex(dexPath, File.createTempFile("opt", "dex", this.getCacheDir()).getPath(), 0);
for (Enumeration<String> classNames = dexFile.entries(); classNames.hasMoreElements(); ) {
String className = classNames.nextElement();
Class myClass = dexClassLoader.loadClass(className);
if (myClass.isAssignableFrom(MyViewCreator.class)) {
MyViewCreator creator = (MyViewCreator) myClass.getConstructor().newInstance();
View myView = creator.createMyView(this);
// add myView wherever you want
navigationView.addView(myView);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
抽屉布局实施问题更新
嗨@lelloman 再次。我一直在尝试将您的解决方案实施到具有抽屉布局的主要项目中。它在final View toolBarItemsLL = revInfl.inflate(R.layout.activity_main, null, false);
.
为什么它不适用于抽屉布局。如果您将Navigation Drawer Activity ( android-plugins/MyNiceApps/app/src/main/
: New -> Activity -> Navigation Drawer Activity)添加到其中,那么它就会崩溃。希望你能帮忙。
这是堆栈跟踪:
08-14 21:44:27.564 13390-13390/rev.ca.revcore W/ResourceType: For resource 0x7f0b005e, entry index(94) is beyond type entryCount(9)
08-14 21:44:27.564 13390-13390/rev.ca.revcore W/ResourceType: Failure getting entry for 0x7f0b005e (t=10 e=94) (error -75)
08-14 21:44:27.565 13390-13390/rev.ca.revcore W/ResourceType: For resource 0x7f0a002c, entry index(44) is beyond type entryCount(5)
08-14 21:44:27.565 13390-13390/rev.ca.revcore W/ResourceType: Failure getting entry for 0x7f0a002c (t=9 e=44) (error -75)
08-14 21:44:27.565 13390-13390/rev.ca.revcore W/ResourceType: For resource 0x7f060022, entry index(34) is beyond type entryCount(1)
08-14 21:44:27.565 13390-13390/rev.ca.revcore W/ResourceType: Failure getting entry for 0x7f060022 (t=5 e=34) (error -75)
08-14 21:44:27.565 13390-13390/rev.ca.revcore D/AndroidRuntime: Shutting down VM
08-14 21:44:27.566 13390-13390/rev.ca.revcore E/AndroidRuntime: FATAL EXCEPTION: main
Process: rev.ca.revcore, PID: 13390
android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Can't convert to ComplexColor: type=0x1
at android.content.res.ResourcesImpl.loadComplexColorForCookie(ResourcesImpl.java:879)
at android.content.res.ResourcesImpl.loadComplexColorFromName(ResourcesImpl.java:756)
at android.content.res.ResourcesImpl.loadColorStateList(ResourcesImpl.java:835)
at android.content.res.Resources.loadColorStateList(Resources.java:1002)
at android.content.res.TypedArray.getColorStateList(TypedArray.java:531)
at android.widget.TextView.<init>(TextView.java:1076)
at android.widget.TextView.<init>(TextView.java:704)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at rev.ca.revbags.MyViewCreator.createView(MyViewCreator.java:24)
at rev.ca.revcore.rev_plugin_loader.RevPluginLoader.revLoadView(RevPluginLoader.java:36)
at rev.ca.revcore.RevCoreMainActivity$1.handleMessage(RevCoreMainActivity.java:21)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)