2015-05-26 20 views
6

Tôi đang cố gắng thích nghi với ứng dụng của mình và gặp phải vấn đề, vì vậy tôi không thể làm cho PageAdapter hoạt động với Màn hình, thay vì Phân đoạn.Vữa và lưu trữ thư viện so với ViewPager

Có ai quản lý đúng không?

tôi đã không thành công nhưng, có lẽ ai đó có thể hướng dẫn cho tôi từ thời điểm này:

Các Dagger ban đầu đăng ký:

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

MainScreen, mà View được lưu trữ 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); 
} 
..... 

Bây giờ, phần chính, SubScreen (3 màn hình tương tự, mà chỉ khác bởi các thông số chúng ta đang đi vào chúng => họ nên điều chỉnh quan điểm theo các thông số)

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

và nó nhìn public class subview kéo dài 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() {....} 
...... 
} 

Tuỳ chỉnh 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(); 
    } 
} 

Báo cáo sự cố: sự cố xảy ra, vì lớp SubView chưa được thêm vào danh sách Tiêm tại "Layouts.createView (childContext, screen);" thời điểm trong Bộ điều hợp và tôi không thể thêm nó theo mặc định, bởi vì tôi muốn có một @provider dữ liệu từ SubScreen đến SubScreen.Presenter. (Tôi đang sử dụng biến cục bộ

Nếu tôi thêm SubView.class vào danh sách phép tiêm và chuyển đổi biến cục bộ thành tĩnh, thì tôi sẽ có 3 trang giống hệt nhau trong ViewPager (logic, như mọi cuộc gọi tiếp theo của các nhà xây dựng -.? ghi ​​đè cũ biến tĩnh)

Bất kỳ sự giúp đỡ/ý tưởng Nhờ sự giúp đỡ của bạn, Konstantin

+1

Hãy giải thích, hoàn toàn và chính xác, những gì "chưa được quản lý để làm cho nó hoạt động" có nghĩa là. – CommonsWare

+0

@CommonsWare hi, điều đó có nghĩa là đoạn trích từ phía trên đã lỗi thời và không hoạt động với các phiên bản hiện tại của Flow & Mortar, thật không may là –

+0

tức là bằng cách có Màn hình để có Chế độ xem cho màn hình này. Tôi nghĩ rằng đó là vấn đề lớn nhất của tôi bây giờ. –

Trả lời

4

Ok, tôi đã tìm ra

Trước hết, thêm subview vào. danh sách toàn cầu lớp tiêm Sau đó sửa đổi SubScreen lớp:

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

và sau đó trong bộ chuyển đổi Tuỳ chỉnh làm thay đổi này:

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

Tức là giải pháp là giữ các biến tĩnh địa phương AND tĩnh trong Màn hình nếu cùng một màn hình sẽ được sử dụng lại. Và khi chúng ta thổi phồng khung nhìn nó - chỉ cần đặt giá trị đúng cho giá trị tĩnh (sẽ được sử dụng trong Trình bày).

Tôi không chắc chắn, đó là giải pháp tốt nhất có thể, nhưng nó hoạt động. Thật tuyệt khi nghe, nếu nó có thể được cải thiện.

Các vấn đề liên quan