【发布时间】:2017-10-19 00:43:00
【问题描述】:
我有一个 LinearLayout,将 BottomSheetDialogFragment 称为 in this example
static class DemoUIView extends LinearLayout {
DateView dateView;
public DemoUIView(Context context){ // Constructor
//..
dateView.setOnClickListener(v-> {
BottomSheetDialogFragment bottomSheetDialogFragment = new BottomSheet3DialogFragment();
bottomSheetDialogFragment.show(getFragmentManager(), bottomSheetDialogFragment.getTag());
})
}
}
但我无法访问那里的 FragmentManager.. 如何从那里调用 BottomSheetDialogFragment?
编辑匕首 2.11:
public class DemoApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> activityInjector;
@Override
public void onCreate() {
super.onCreate();
DemoApplicationComponent appComponent = DaggerDemoApplicationComponent.builder()
.application(this)
.build()
}
@Override
public AndroidInjector<Activity> activityInjector() {
return activityInjector;
}
}
和 DemoApplicationComponent:
@ApplicationScope
@Component(
modules = {
ApplicationModule.class
}
)
public interface DemoApplicationComponent extends IApplicationComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
DemoApplicationComponent build();
}
}
以及对应的接口:
public interface IApplicationComponent {
Application getApplication();
}
【问题讨论】:
-
您不应该从视图中调用 BottomSheetDialogFragment 或任何其他片段;片段可以包含视图,但视图不能包含片段。尝试从活动或其他片段中显示您的 BottomSheetDialogFragment。祝你好运!
标签: android android-fragments android-context fragmentmanager