我遇到了问题:
我创建片段,然后使用TranactionManager.replace()
将其添加到活动中public class Main extends RoboActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction().replace(R.id.root, new BaseFragment()).commit();
}
}
public class BaseFragment extends RoboFragment {
@InjectView(R.id.text)
TextView textView;
@InjectView(R.id.viewWithViews)
ViewWithViews viewWithViews;
public BaseFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_base, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//text view and viewWithViews injected there
}
}
这一点都是好的FragmentView创建并注入了所有视图。
但接下来:
public class ViewWithViews extends LinearLayout implements View.OnClickListener {
@InjectView(R.id.child_text)
TextView textView;
private View button;
@Inject
private SharedPreferences sharedPreferences;
public ViewWithViews(Context context) {
super(context);
init();
onFinishInflate();
}
private void init() {
inflate(getContext(), R.layout.child_view, this);
}
public ViewWithViews(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ViewWithViews(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
@Override
protected void onFinishInflate() {
super.onFinishInflate(); // sharedPreferences are injected!
this.button = findViewById(R.id.button);
this.button.setOnClickListener(this);
RoboGuice.injectMembers(getContext(), this); //force injection
//still no views injected there
}
@Override
public void onClick(View view) {
System.out.println(view); // all possible events are passed, manual click. "textView" is not injected
}
}
ViewWithViews中没有注入任何视图!
但是,如果将Fragment直接放在MainActivity R.layout.activity_main中,则会执行所有视图注入!我有点失望。这是一个错误,还是我做错了?
org.roboguice:roboguice:3.0-α-2
android sdk 19
答案 0 :(得分:0)
在onFinishInflate电话中:
RoboGuice.getInjector(context).injectViewMembers(this);
injectMembers只调用guice Injected对象,而不是android视图。
答案 1 :(得分:0)
getSupportFragmentManager ()。beginTransaction()。replace(R.id.main_fragment,new ReaderFragment()," fragment")。commit();