InjectView in customview in fragments

42 views
Skip to first unread message

Vladimir Abramov

unread,
Jul 15, 2014, 4:22:47 PM7/15/14
to robo...@googlegroups.com
I have a problem:

I create fragment and than add it to activity with 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
    }
}

A this point all ok FragmentView created and all views are injected.

But next:
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

    }

    @Override
    public void onClick(View view) {
        System.out.println(view); // all possible events are passed, manual click. "textView" is not injected
    }


No views are injected in ViewWithViews!


However if put Fragment directly in MainActivity R.layout.activity_main all view injections are performed! I'm bit dissapointed. Is it a bug, or I'm doing it wrong?
Reply all
Reply to author
Forward
0 new messages