我正在尝试在我的应用中调整Mortar& Flow并遇到一个问题,我无法使PageAdapter与屏幕一起使用,而不是碎片。
有人设法做到了吗?
我没有成功但是,可能有人可以从这一点指导我:
最初的Dagger注册:
@Module(
injects = {
MainActivity.class,
},
library = true,
complete = false
)
public class DaggerConfig {
@SuppressWarnings("unused")
@Provides @Singleton Gson provideGson() {
return new GsonBuilder().create();
}
}
MainScreen,其View托管ViewPager:
@Layout(R.layout.screen_main) @WithModule(MainScreen.Module.class)
public class MainScreen extends Path {
@dagger.Module(injects = MainView.class, addsTo = DaggerConfig.class)
public static class Module {}
@Singleton
public static class Presenter extends ViewPresenter<MainView> {
@Inject
public Presenter() {}
}
}
的MainView:
...........
@Inject
MainScreen.Presenter presenter;
...........
@Override protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.inject(this);
final Path[] screens = {
new SubScreen("1"),
new SubScreen("2"),
new SubScreen("3"),
};
CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(getContext(), screens );
customPagerAdapter .setAdapter(firstRunPagerAdapter);
}
.....
现在,主要部分,SubScreen(3个类似的屏幕,只有我们传入的参数不同=&gt;他们应根据这些参数调整视图)
@Layout(R.layout.screen_subscreen) @WithModule(SubScreen.Module.class)
public class SubScreen extends Path {
private final String title;
public SubScreen(String titleParam) {
title = titleParam;
}
@dagger.Module(injects = SubView.class, addsTo = DaggerConfig.class)
public class Module {
@Provides
SubViewMetadata provideSubViewMetadata() {
return new SubViewMetadata(backgroundColor, title);
}
}
@Singleton
public static class Presenter extends ViewPresenter<SubView> {
private String title;
@Inject
public Presenter(String title) {
this.title= title;
}
@Override
protected void onLoad(Bundle savedInstanceState) {
super.onLoad(savedInstanceState);
if (!hasView()) {
return;
}
getView().setTitle(subViewMetadata.title);
}
}
}
它的观点 公共类SubView扩展了FrameLayout {
@InjectView(R.id.subViewTitleTextView)
TextView subViewTitleTextView;
@Inject
SubScreen.Presenter presenter;
public SubView(Context context, AttributeSet attrs) {
super(context, attrs);
ObjectGraphService.inject(context, this);
}
public void setTitle(String title) {
subViewTitleTextView.setText(title);
}
@Override protected void onAttachedToWindow() {....}
@Override protected void onDetachedFromWindow() {....}
......
}
自定义寻呼机适配器:
public class CustomPagerAdapter extends PagerAdapter {
private final Context context;
private final Path[] screens;
public CustomPagerAdapter(Context context, Path[] screens) {
this.context = context;
this.screens = screens;
}
@Override
public int getCount() {
return (screens == null)? 0 : screens.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view.equals(o);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Path screen = screens[position];
MortarScope originalScope = MortarScope.getScope(context);
MortarScope newChildScope = originalScope.buildChild().build("tutorialpage" + position);
Context childContext = newChildScope.createContext(context);
View newChild = Layouts.createView(childContext, screen);
container.addView(newChild);
return newChild;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = ((View) object);
container.removeView(view);
MortarScope.getScope(view.getContext()).destroy();
}
}
问题陈述:它正在崩溃,因为SubView类尚未被添加到“Layouts.createView(childContext,screen);”中的Injections列表中;在适配器中的时刻,我默认不能添加它,因为我想从SubScreen到SubScreen.Presenter有一个@provider数据。 (我正在使用局部变量。
如果我将SubView.class添加到注入列表中并将本地Screen的变量转换为静态,那么我将在ViewPager中有3个相同的页面(这是合乎逻辑的,因为每次下一次调用构造函数 - 覆盖旧的静态变量。)
任何帮助/想法? 谢谢你的帮助, 康斯坦丁
答案 0 :(得分:4)
好的,我想通了。
首先,将SubView添加到全局注入的类列表中 然后修改SubScreen类:
@Layout(R.layout.screen_subscreen)
public class SubScreen extends Path {
private static String titleStatic; // Introducing static variable
private final String title;
public SubScreen(String titleParam) {
title = titleParam;
}
public void refreshPresenter() {
titleStatic = title;
}
@Singleton
public static class Presenter extends ViewPresenter<SubView> {
private String title;
@Inject
public Presenter() {
}
@Override
protected void onLoad(Bundle savedInstanceState) {
super.onLoad(savedInstanceState);
if (!hasView()) {
return;
}
getView().setTitle(titleStatic);
}
}
}
然后在自定义适配器中执行此更改:
public class CustomPagerAdapter extends PagerAdapter {
private final Context context;
private final SubScreen[] screens;
public CustomPagerAdapter(Context context, SubScreen[] screens) {
this.context = context;
this.screens = screens;
}
......
@Override
public Object instantiateItem(ViewGroup container, int position) {
SubScreen screen = screens[position];
MortarScope originalScope = MortarScope.getScope(context);
MortarScope newChildScope = originalScope.buildChild().build("tutorialpage" + position);
Context childContext = newChildScope.createContext(context);
screen.refreshPresenter(); // updating the static var with local one!
View newChild = Layouts.createView(childContext, screen);
container.addView(newChild);
return newChild;
}
....
}
即。解决方案是在屏幕中保留本地 AND 静态变量,如果要重用相同的屏幕。当我们给视图充气时 - 只需将正确的值设置为静态值(将在Presenter中使用)。
我不确定,这是最好的解决方案,但它确实有效。如果可以改进的话,听听会很高兴。