【发布时间】:2014-07-15 20:31:22
【问题描述】:
我有一个问题:
我创建片段,然后使用 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 中,则会执行所有视图注入!我有点失望。是bug还是我做错了?
org.roboguice:roboguice:3.0-alpha-2
android sdk 19
【问题讨论】:
标签: android android-fragments roboguice