rjsvieira / floatingMenu

An orbit-like action button
Apache License 2.0
305 stars 42 forks source link

when use databinding, it goes wrong #7

Closed Nstd closed 7 years ago

Nstd commented 7 years ago

I override setContent() method, to dynamic add FloatingMenuButton such as:

    @Override
    public void setContentView(int layoutResID) {
            super.setContentView(layoutResID);
            FrameLayout fLayout = new FrameLayout(this);
            ViewGroup contentContainer = (ViewGroup) this.findViewById(android.R.id.content);
            ViewGroup parent = (ViewGroup) contentContainer.getParent();
            parent.removeView(contentContainer);
            fLayout.addView(contentContainer);
            parent.addView(fLayout);
            floatingMenu = (FloatingMenuButton) LayoutInflater.from(this).inflate(R.layout.menu, parent, false);
            fLayout.addView(floatingMenu);
    }

then DataBindingUtil.setContentView(this, R.layout.my_layout) return a null object

I found that you add FloatingSubButton inner android.R.id.content too, which cause following problem.

When DataBindingUtil invoke bindToAddedViews()

private static <T extends ViewDataBinding> T bindToAddedViews(DataBindingComponent component, ViewGroup parent, int startChildren, int layoutId) {
    final int endChildren = parent.getChildCount(); // parent is R.id.content, count is subButton size + 1
    final int childrenAdded = endChildren - startChildren;
    if (childrenAdded == 1) {
        final View childView = parent.getChildAt(endChildren - 1);
        return bind(count, childView, layoutId);
    } else {
        final View[] children = new View[childrenAdded];
        for (int i = 0; i < childrenAdded; i++) {
           children[i] = parent.getChildAt(i + startChildren);
       }
       return bind(component, children, layoutId);
    }

The bind() method in else invoke return (T) sMapper.getDataBinder(bindingComponent, roots, layoutId); which return null, while the 'bind()' method in if can return correct binding object.

rjsvieira commented 7 years ago

Hello @Nstd ,

Not sure if I got the issue. Yes it uses R.android.id.content inside the getActivityContentView() method. Are you using the the addFloatingSubButton method ? Or is this happening only on your specific overridden method?

Regards,

Nstd commented 7 years ago

Hi @rjsvieira ,

I just use xml, and I write a simple demo here

rjsvieira commented 7 years ago

Hello @Nstd Could not go much deeper into the issue but I have downloaded the project and ran it and the FloatingMenuButton seems nice. The code you're using to build the FloatingMenu inside the ScalpelHelper seems correct and its usage is good. You stated that

then DataBindingUtil.setContentView(this, R.layout.my_layout) return a null object

I found that you add FloatingSubButton inner android.R.id.content too, which cause following problem.

When DataBindingUtil invoke bindToAddedViews()

How precisely can this be fixed? The android.R.id.content allows the library to get the hierarchy where the button is going to be added.

Regards,

PS : (Jake Wharton's library is nice and with that FloatingMenu, is even cooler :), congratulations)

Nstd commented 7 years ago

Hi @rjsvieira Please use the branch of feature-databinding. When we launch the app, it shows a Toast mBinding is null = true, which means DataBindingUtil find binding object failed.

I think maybe the mechanism of showing sub menu just in conflict with the databinding mechanism.

PS: I think so too, LOL

Nstd commented 7 years ago

Hi @rjsvieira I found a simple solution.

mBinding = DataBindingUtil..setContentView(this, R.layout.my_view);

...

activity.findViewById(android.R.id.content).post(new Runnalbe() {
    @Override
    public void run() {
        //to dynamic add FloatingMenu and Scalpel
    }
});

Thanks~