Android - Butterknife bind inside custom fragment view

I have a fragment that contains a custom view.

in the fragment I do ButterKnife.bindas follows:

    View root = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(this, root);

And I manage to bind the views.

Now the snippet contains the custom view that I created. In a custom view, MenuToggleButtonI want to bind another view and work with it.

The problem I am facing is that I do not know how to access the root view of the fragment from within the user view (which is in the fragment).

public MenuToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        final Activity activity = (Activity) context;
        ButterKnife.bind(this, // need to get the fragment root view somehow);
    }

How can I get a representation of the root fragment to order to bind it like in a fragment?

+5
1

ViewHolder , ButterKnife . findViewById. ButterKnife :

class MenuToggleButton{
    public MenuToggleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        View v = inflate(context, R.layout.menu_toggle_button, this);
        (new ViewHolder(v)).init();
    }
    class ViewHolder{
        @BindView(R.id.some_view)
        SomeView someView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        init() {
            someView.doSomething();
        }

    }

}

0

Source: https://habr.com/ru/post/1654987/


All Articles