implement ViewStub.inflate()

This commit is contained in:
Julian Winkler
2024-02-17 15:28:29 +01:00
parent b9b28619dd
commit 0bb3d7ce15

View File

@@ -1,16 +1,37 @@
package android.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
public class ViewStub extends View {
private int layoutResource;
public ViewStub(Context context) {
super(context);
this(context, null, 0);
}
public ViewStub(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
this(context, attributeSet, 0);
}
public ViewStub(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
TypedArray a = context.obtainStyledAttributes(attributeSet, com.android.internal.R.styleable.ViewStub);
layoutResource = a.getResourceId(com.android.internal.R.styleable.ViewStub_layout, -1);
a.recycle();
}
public View inflate() {
if (layoutResource == -1)
throw new IllegalStateException("ViewStub must have a valid layoutResource");
ViewGroup parent = (ViewGroup) getParent();
View view = LayoutInflater.from(getContext()).inflate(layoutResource, parent, false);
int index = parent.indexOfChild(this);
parent.removeView(this);
parent.addView(view, index, getLayoutParams());
return view;
}
}