Android 在custiom视图中使用Roboguice ViewInjection。
我有一个问题: 我创建了片段,然后用TranactionManager.replace()将其添加到活动中 A该点创建所有ok FragmentView,并注入所有视图 但接下来:Android 在custiom视图中使用Roboguice ViewInjection。,android,android-fragments,roboguice,Android,Android Fragments,Roboguice,我有一个问题: 我创建了片段,然后用TranactionManager.replace()将其添加到活动中 A该点创建所有ok FragmentView,并注入所有视图 但接下来: public class ViewWithViews extends LinearLayout implements View.OnClickListener { @InjectView(R.id.child_text) TextView textView; private View butt
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中没有注入视图
但是,如果将片段直接放入MainActivity R.layout.activity\u main,则会执行所有视图注入!我有点失望。是虫子,还是我做错了
org.roboguice:roboguice:3.0-alpha-2
android sdk 19在onfinishflate调用中:
RoboGuice.getInjector(context).injectViewMembers(this);
injectMembers只调用guice注入的对象,而不是android视图。getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment,new ReaderFragment(),“fragment”).commit()
RoboGuice.getInjector(context).injectViewMembers(this);