2023-09-01 12:55:04 +02:00
|
|
|
package android.view;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2024-02-17 15:28:29 +01:00
|
|
|
import android.content.res.TypedArray;
|
2023-09-01 12:55:04 +02:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
|
|
|
|
|
public class ViewStub extends View {
|
|
|
|
|
|
2024-02-17 15:28:29 +01:00
|
|
|
private int layoutResource;
|
|
|
|
|
|
2023-09-01 12:55:04 +02:00
|
|
|
public ViewStub(Context context) {
|
2024-02-17 15:28:29 +01:00
|
|
|
this(context, null, 0);
|
2023-09-01 12:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ViewStub(Context context, AttributeSet attributeSet) {
|
2024-02-17 15:28:29 +01:00
|
|
|
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;
|
2023-09-01 12:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|