| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
package android.databinding; |
| 17 |
|
| 18 |
import android.view.View; |
| 19 |
import android.view.ViewStub; |
| 20 |
import android.view.ViewStub.OnInflateListener; |
| 21 |
|
| 22 |
/** |
| 23 |
* This class represents a ViewStub before and after inflation. Before inflation, |
| 24 |
* the ViewStub is accessible. After inflation, the root View of the inflated layout |
| 25 |
* will be available. If the inflated layout has data binding, the ViewDataBinding for the inflated |
| 26 |
* View is accessible. |
| 27 |
*/ |
| 28 |
public class ViewStubProxy { |
| 29 |
private ViewStub mViewStub; |
| 30 |
private ViewDataBinding mViewDataBinding; |
| 31 |
private View mRoot; |
| 32 |
private OnInflateListener mOnInflateListener; |
| 33 |
private ViewDataBinding mContainingBinding; |
| 34 |
|
| 35 |
private OnInflateListener mProxyListener = new OnInflateListener() { |
| 36 |
@Override |
| 37 |
public void onInflate(ViewStub stub, View inflated) { |
| 38 |
mRoot = inflated; |
| 39 |
mViewDataBinding = DataBindingUtil.bind(mContainingBinding.mBindingComponent, |
| 40 |
inflated, stub.getLayoutResource()); |
| 41 |
mViewStub = null; |
| 42 |
|
| 43 |
if (mOnInflateListener != null) { |
| 44 |
mOnInflateListener.onInflate(stub, inflated); |
| 45 |
mOnInflateListener = null; |
| 46 |
} |
| 47 |
mContainingBinding.invalidateAll(); |
| 48 |
mContainingBinding.forceExecuteBindings(); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
public ViewStubProxy(ViewStub viewStub) { |
| 53 |
mViewStub = viewStub; |
| 54 |
mViewStub.setOnInflateListener(mProxyListener); |
| 55 |
} |
| 56 |
|
| 57 |
public void setContainingBinding(ViewDataBinding containingBinding) { |
| 58 |
mContainingBinding = containingBinding; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Returns <code>true</code> if the ViewStub has replaced itself with the inflated layout |
| 63 |
* or <code>false</code> if not. |
| 64 |
* |
| 65 |
* @return <code>true</code> if the ViewStub has replaced itself with the inflated layout |
| 66 |
* or <code>false</code> if not |
| 67 |
*/ |
| 68 |
public boolean isInflated() { |
| 69 |
return mRoot != null; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Returns the root View of the layout replacing the ViewStub once it has been inflated. |
| 74 |
* <code>null</code> is returned prior to inflation. |
| 75 |
* |
| 76 |
* @return the root View of the layout replacing the ViewStub once it has been inflated. |
| 77 |
* <code>null</code> is returned prior to inflation |
| 78 |
*/ |
| 79 |
public View getRoot() { |
| 80 |
return mRoot; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the data binding associated with the inflated layout once it has been inflated. |
| 85 |
* <code>null</code> prior to inflation or if there is no binding associated with the layout. |
| 86 |
* |
| 87 |
* @return the data binding associated with the inflated layout once it has been inflated. |
| 88 |
* <code>null</code> prior to inflation or if there is no binding associated with the layout |
| 89 |
*/ |
| 90 |
public ViewDataBinding getBinding() { |
| 91 |
return mViewDataBinding; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns the ViewStub in the layout or <code>null</code> if the ViewStub has been inflated. |
| 96 |
* |
| 97 |
* @return the ViewStub in the layout or <code>null</code> if the ViewStub has been inflated. |
| 98 |
*/ |
| 99 |
public ViewStub getViewStub() { |
| 100 |
return mViewStub; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Sets the {@link OnInflateListener} to be called when the ViewStub inflates. The proxy must |
| 105 |
* have an OnInflateListener, so <code>listener</code> will be called immediately after |
| 106 |
* the proxy's listener is called. |
| 107 |
* |
| 108 |
* @param listener The OnInflateListener to notify of successful inflation |
| 109 |
*/ |
| 110 |
public void setOnInflateListener(OnInflateListener listener) { |
| 111 |
if (mViewStub != null) { |
| 112 |
mOnInflateListener = listener; |
| 113 |
} |
| 114 |
} |
| 115 |
} |