Libraries of solutions and streams against ViewPager

I am trying to adapt Mortar & Flow in my application and have encountered the problem that I cannot make the PageAdapter work with screens instead of fragments.

Has anyone managed to fix this?

I did not succeed, but probably someone can guide me from now on:

Initial registration of the dagger:

@Module( injects = { MainActivity.class, }, library = true, complete = false ) public class DaggerConfig { @SuppressWarnings("unused") @Provides @Singleton Gson provideGson() { return new GsonBuilder().create(); } } 

MainScreen, in the view of which is 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); } ..... 

Now the main part , SubScreen (3 similar screens that differ only in the parameters that we pass to them => they must configure the views according to these parameters)

 @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); } } } 

and it scans the Open SubView class extends 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() {....} ...... } 

Custom Pager Adapter:

 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(); } } 

Problem statement : it crashes because the SubView class was not added to the Injection list on "Layouts.createView (childContext, screen)"; moment in the adapter, and I cannot add it by default, because I want to have @provider data from SubScreen in SubScreen.Presenter. (I use a local variable.

If I add SubView.class to the injection list and convert the local screen variables to static, then I will have 3 identical pages inside the ViewPager (which is logical, since every next call to the constructor will override the old static variables).

Any help / ideas? Thank you for your help, Konstantin

+6
source share
1 answer

OK I understood.

First of all, add SubView to the list of classes that are globally implemented. Then change the SubScreen class:

 @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); } } } 

and then in the user adapter this will change:

 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; } .... } 

those. the solution is to keep local static variables AND on the screen if the same screen is reused. And when we inflate the view, we simply set the correct value to static (which will be used in Presenter).

I am not sure if this is the best solution, but it works. It would be nice to hear if this can be improved.

+4
source

Source: https://habr.com/ru/post/987942/


All Articles