下面列出了android.view.ViewStub#inflate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected View createGraphObjectView(T graphObject) {
View result = inflater.inflate(getGraphObjectRowLayoutId(graphObject), null);
ViewStub checkboxStub = (ViewStub) result.findViewById(R.id.com_facebook_picker_checkbox_stub);
if (checkboxStub != null) {
if (!getShowCheckbox()) {
checkboxStub.setVisibility(View.GONE);
} else {
CheckBox checkBox = (CheckBox) checkboxStub.inflate();
updateCheckboxState(checkBox, false);
}
}
ViewStub profilePicStub = (ViewStub) result.findViewById(R.id.com_facebook_picker_profile_pic_stub);
if (!getShowPicture()) {
profilePicStub.setVisibility(View.GONE);
} else {
ImageView imageView = (ImageView) profilePicStub.inflate();
imageView.setVisibility(View.VISIBLE);
}
return result;
}
protected void showEmptyView(String text) {
// 需要这样处理,否则二次显示会失败
ViewStub viewStub = getView(R.id.vs_empty);
if (viewStub != null) {
emptyView = viewStub.inflate();
((TextView) emptyView.findViewById(R.id.tv_tip_empty)).setText(text);
}
if (emptyView != null) {
emptyView.setVisibility(View.VISIBLE);
}
if (loadingView != null && loadingView.getVisibility() != View.GONE) {
loadingView.setVisibility(View.GONE);
}
// 停止动画
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
}
if (errorView != null) {
errorView.setVisibility(View.GONE);
}
if (bindingView != null && bindingView.getRoot().getVisibility() != View.GONE) {
bindingView.getRoot().setVisibility(View.GONE);
}
}
/**
* @param parentView
* @param viewStubId
* @param inflatedViewId
* @param inflateLayoutResId
* @return
*/
@SuppressLint("ResourceType")
public static View findViewFromViewStub(View parentView, int viewStubId, int inflatedViewId, int inflateLayoutResId) {
if (null == parentView) {
return null;
}
View view = parentView.findViewById(inflatedViewId);
if (null == view) {
ViewStub vs = (ViewStub) parentView.findViewById(viewStubId);
if (null == vs) {
return null;
}
if (vs.getLayoutResource() < 1 && inflateLayoutResId > 0) {
vs.setLayoutResource(inflateLayoutResId);
}
view = vs.inflate();
if (null != view) {
view = view.findViewById(inflatedViewId);
}
}
return view;
}
/**
* 把 ViewStub inflate 之后在其中根据 id 找 View
*
* @param parentView 包含 ViewStub 的 View
* @param viewStubId 要从哪个 ViewStub 来 inflate
* @param inflatedViewId 最终要找到的 View 的 id
* @return id 为 inflatedViewId 的 View
*/
public static View findViewFromViewStub(View parentView, int viewStubId, int inflatedViewId) {
if (null == parentView) {
return null;
}
View view = parentView.findViewById(inflatedViewId);
if (null == view) {
ViewStub vs = (ViewStub) parentView.findViewById(viewStubId);
if (null == vs) {
return null;
}
view = vs.inflate();
if (null != view) {
view = view.findViewById(inflatedViewId);
}
}
return view;
}
protected View createGraphObjectView(T graphObject, View convertView) {
View result = inflater.inflate(getGraphObjectRowLayoutId(graphObject), null);
ViewStub checkboxStub = (ViewStub) result.findViewById(R.id.com_facebook_picker_checkbox_stub);
if (checkboxStub != null) {
if (!getShowCheckbox()) {
checkboxStub.setVisibility(View.GONE);
} else {
CheckBox checkBox = (CheckBox) checkboxStub.inflate();
updateCheckboxState(checkBox, false);
}
}
ViewStub profilePicStub = (ViewStub) result.findViewById(R.id.com_facebook_picker_profile_pic_stub);
if (!getShowPicture()) {
profilePicStub.setVisibility(View.GONE);
} else {
ImageView imageView = (ImageView) profilePicStub.inflate();
imageView.setVisibility(View.VISIBLE);
}
return result;
}
protected View createGraphObjectView(T graphObject) {
View result = inflater.inflate(getGraphObjectRowLayoutId(graphObject), null);
ViewStub checkboxStub = (ViewStub) result.findViewById(R.id.com_facebook_picker_checkbox_stub);
if (checkboxStub != null) {
if (!getShowCheckbox()) {
checkboxStub.setVisibility(View.GONE);
} else {
CheckBox checkBox = (CheckBox) checkboxStub.inflate();
updateCheckboxState(checkBox, false);
}
}
ViewStub profilePicStub = (ViewStub) result.findViewById(R.id.com_facebook_picker_profile_pic_stub);
if (!getShowPicture()) {
profilePicStub.setVisibility(View.GONE);
} else {
ImageView imageView = (ImageView) profilePicStub.inflate();
imageView.setVisibility(View.VISIBLE);
}
return result;
}
protected View createGraphObjectView(T graphObject) {
View result = inflater.inflate(getGraphObjectRowLayoutId(graphObject), null);
ViewStub checkboxStub = (ViewStub) result.findViewById(R.id.com_facebook_picker_checkbox_stub);
if (checkboxStub != null) {
if (!getShowCheckbox()) {
checkboxStub.setVisibility(View.GONE);
} else {
CheckBox checkBox = (CheckBox) checkboxStub.inflate();
updateCheckboxState(checkBox, false);
}
}
ViewStub profilePicStub = (ViewStub) result.findViewById(R.id.com_facebook_picker_profile_pic_stub);
if (!getShowPicture()) {
profilePicStub.setVisibility(View.GONE);
} else {
ImageView imageView = (ImageView) profilePicStub.inflate();
imageView.setVisibility(View.VISIBLE);
}
return result;
}
/**
* 加载失败点击重新加载的状态
*/
protected void showError() {
ViewStub viewStub = getView(R.id.vs_error_refresh);
if (viewStub != null) {
errorView = viewStub.inflate();
// 点击加载失败布局
errorView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showLoading();
onRefresh();
}
});
}
if (errorView != null) {
errorView.setVisibility(View.VISIBLE);
}
if (loadingView != null && loadingView.getVisibility() != View.GONE) {
loadingView.setVisibility(View.GONE);
}
// 停止动画
if (mAnimationDrawable != null && mAnimationDrawable.isRunning()) {
mAnimationDrawable.stop();
}
if (bindingView.getRoot().getVisibility() != View.GONE) {
bindingView.getRoot().setVisibility(View.GONE);
}
if (emptyView != null && emptyView.getVisibility() != View.GONE) {
emptyView.setVisibility(View.GONE);
}
}
private void setUpView(Activity context) {
shareView = View.inflate(context, R.layout.layout_share_view, null);
fullMaskView = shareView.findViewById(R.id.full_mask);
mViewStub = (ViewStub) shareView.findViewById(R.id.view_stub);
mViewStub.inflate();
llContent = (LinearLayout) shareView.findViewById(R.id.ll_content);
mGirdView = (GridView) shareView.findViewById(R.id.gv_share);
mTvClose = (TextView) shareView.findViewById(R.id.tv_close);
// add shareView to activity's root view
((ViewGroup) context.getWindow().getDecorView()).addView(shareView);
initListener();
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initToolbar();
ViewStub viewStub = (ViewStub) findViewById(R.id.view_stub);
viewStub.setLayoutResource(R.layout.fragment_view_stub);
viewStub.inflate();
MDFirstFragment fragment = (MDFirstFragment) getSupportFragmentManager().findFragmentById(R.id.md_fragment);
Log.e("TestActivity", "fragment = " + fragment);
}
private View inflateViewStub(@IdRes int stubId) {
ViewStub viewStub = (ViewStub) mView.findViewById(stubId);
if (viewStub != null && viewStub.getLayoutResource() != 0) {
return viewStub.inflate();
}
return null;
}
public LinkShareViewHolder(View itemView) {
super(itemView);
ViewStub viewStub = (ViewStub) itemView.findViewById(R.id.vs_share_fragment_item_main);
viewStub.setLayoutResource(R.layout.share_fragment_item_link_layout);
viewStub.inflate();
// display = (LinearLayout) viewStub.inflate().findViewById(R.id.ll_share_fragment_item_container);
}
private void showRecentlyViewed(){
if(recentlyViewedView == null){
ViewStub viewStub = findViewById(R.id.view_stub_recently_viewed);
recentlyViewedView = (RecentlyViewedView) viewStub.inflate();
}
recentlyViewedView.show();
}
private void inflateDataUiLayout(){
ViewStub viewStub = (ViewStub) findViewById(R.id.vs_data_ui_layout);
if(viewStub == null){
//already inflate
return;
}
View dataUiRoot = viewStub.inflate();
//初始化数据UI相关
mViewGpsPower = dataUiRoot.findViewById(R.id.view_gps_power);
mBtnChangeMapUi = (Button) dataUiRoot.findViewById(R.id.btn_change_to_map_ui);
mTvDataMoveDistance = (TextView) dataUiRoot.findViewById(R.id.tv_data_ui_distance);
mTvDataMoveTime = (TextView) dataUiRoot.findViewById(R.id.tv_data_ui_time);
mBtnChangeMapUi.setOnClickListener(this);
}
/**
* This function builds the {@link CompositorViewHolder}. Subclasses *must* call
* super.setContentView() before using {@link #getTabModelSelector()} or
* {@link #getCompositorViewHolder()}.
*/
@Override
protected final void setContentView() {
final long begin = SystemClock.elapsedRealtime();
TraceEvent.begin("onCreate->setContentView()");
enableHardwareAcceleration();
setLowEndTheme();
int controlContainerLayoutId = getControlContainerLayoutId();
WarmupManager warmupManager = WarmupManager.getInstance();
if (warmupManager.hasBuiltOrClearViewHierarchyWithToolbar(controlContainerLayoutId)) {
View placeHolderView = new View(this);
setContentView(placeHolderView);
ViewGroup contentParent = (ViewGroup) placeHolderView.getParent();
WarmupManager.getInstance().transferViewHierarchyTo(contentParent);
contentParent.removeView(placeHolderView);
} else {
setContentView(R.layout.main);
if (controlContainerLayoutId != NO_CONTROL_CONTAINER) {
ViewStub toolbarContainerStub =
((ViewStub) findViewById(R.id.control_container_stub));
toolbarContainerStub.setLayoutResource(controlContainerLayoutId);
toolbarContainerStub.inflate();
}
}
TraceEvent.end("onCreate->setContentView()");
mInflateInitialLayoutDurationMs = SystemClock.elapsedRealtime() - begin;
// Set the status bar color to black by default. This is an optimization for
// Chrome not to draw under status and navigation bars when we use the default
// black status bar
ApiCompatibilityUtils.setStatusBarColor(getWindow(), Color.BLACK);
ViewGroup rootView = (ViewGroup) getWindow().getDecorView().getRootView();
mCompositorViewHolder = (CompositorViewHolder) findViewById(R.id.compositor_view_holder);
mCompositorViewHolder.setRootView(rootView);
// Setting fitsSystemWindows to false ensures that the root view doesn't consume the insets.
rootView.setFitsSystemWindows(false);
// Add a custom view right after the root view that stores the insets to access later.
// ContentViewCore needs the insets to determine the portion of the screen obscured by
// non-content displaying things such as the OSK.
mInsetObserverView = InsetObserverView.create(this);
rootView.addView(mInsetObserverView, 0);
}
private void inflateTitleBar(ViewGroup view) {
ViewStub stub = (ViewStub) view.findViewById(R.id.com_facebook_picker_title_bar_stub);
if (stub != null) {
View titleBar = stub.inflate();
final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.BELOW, R.id.com_facebook_picker_title_bar);
listView.setLayoutParams(layoutParams);
if (titleBarBackground != null) {
titleBar.setBackgroundDrawable(titleBarBackground);
}
doneButton = (Button) view.findViewById(R.id.com_facebook_picker_done_button);
if (doneButton != null) {
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logAppEvents(true);
appEventsLogged = true;
if (onDoneButtonClickedListener != null) {
onDoneButtonClickedListener.onDoneButtonClicked(PickerFragment.this);
}
}
});
if (getDoneButtonText() != null) {
doneButton.setText(getDoneButtonText());
}
if (doneButtonBackground != null) {
doneButton.setBackgroundDrawable(doneButtonBackground);
}
}
titleTextView = (TextView) view.findViewById(R.id.com_facebook_picker_title);
if (titleTextView != null) {
if (getTitleText() != null) {
titleTextView.setText(getTitleText());
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.auth_activity_title);
if (! settings.getScreenshotsEnabled())
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
setContentView(R.layout.activity_container);
Toolbar toolbar = findViewById(R.id.container_toolbar);
toolbar.setNavigationIcon(null);
setSupportActionBar(toolbar);
ViewStub stub = findViewById(R.id.container_stub);
stub.setLayoutResource(R.layout.content_authenticate);
View v = stub.inflate();
Intent callingIntent = getIntent();
int labelMsg = callingIntent.getIntExtra(Constants.EXTRA_AUTH_MESSAGE, R.string.auth_msg_authenticate);
newEncryption = callingIntent.getStringExtra(Constants.EXTRA_AUTH_NEW_ENCRYPTION);
TextView passwordLabel = v.findViewById(R.id.passwordLabel);
TextInputLayout passwordLayout = v.findViewById(R.id.passwordLayout);
passwordInput = v.findViewById(R.id.passwordEdit);
if (settings.getBlockAccessibility())
passwordLayout.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && settings.getBlockAutofill())
passwordLayout.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
passwordLabel.setText(labelMsg);
authMethod = settings.getAuthMethod();
password = settings.getAuthCredentials();
if (password.isEmpty()) {
password = settings.getOldCredentials(authMethod);
oldPassword = true;
}
if (authMethod == AuthMethod.PASSWORD) {
if (password.isEmpty()) {
Toast.makeText(this, R.string.auth_toast_password_missing, Toast.LENGTH_LONG).show();
finishWithResult(true, null);
} else {
passwordLayout.setHint(getString(R.string.auth_hint_password));
passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
} else if (authMethod == AuthMethod.PIN) {
if (password.isEmpty()) {
Toast.makeText(this, R.string.auth_toast_pin_missing, Toast.LENGTH_LONG).show();
finishWithResult(true, null);
} else {
passwordLayout.setHint(getString(R.string.auth_hint_pin));
passwordInput.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
}
} else {
finishWithResult(true, null);
}
passwordInput.setTransformationMethod(new PasswordTransformationMethod());
passwordInput.setOnEditorActionListener(this);
Button unlockButton = v.findViewById(R.id.buttonUnlock);
unlockButton.setOnClickListener(this);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
private void inflateTitleBar(ViewGroup view) {
ViewStub stub = (ViewStub) view.findViewById(R.id.com_facebook_picker_title_bar_stub);
if (stub != null) {
View titleBar = stub.inflate();
final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.addRule(RelativeLayout.BELOW, R.id.com_facebook_picker_title_bar);
listView.setLayoutParams(layoutParams);
if (titleBarBackground != null) {
titleBar.setBackgroundDrawable(titleBarBackground);
}
doneButton = (Button) view.findViewById(R.id.com_facebook_picker_done_button);
if (doneButton != null) {
doneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
logAppEvents(true);
appEventsLogged = true;
if (onDoneButtonClickedListener != null) {
onDoneButtonClickedListener.onDoneButtonClicked(PickerFragment.this);
}
}
});
if (getDoneButtonText() != null) {
doneButton.setText(getDoneButtonText());
}
if (doneButtonBackground != null) {
doneButton.setBackgroundDrawable(doneButtonBackground);
}
}
titleTextView = (TextView) view.findViewById(R.id.com_facebook_picker_title);
if (titleTextView != null) {
if (getTitleText() != null) {
titleTextView.setText(getTitleText());
}
}
}
}
public SendTextHolder(View itemView) {
super(itemView);
ViewStub viewStub = (ViewStub) getView(R.id.vs_item_activity_chat_send_view_stub);
viewStub.setLayoutResource(R.layout.item_activity_chat_send_text);
viewStub.inflate();
}
private void init() {
layoutSelectorView = activity.getLayoutInflater().inflate(R.layout.stream_layout_preview, null);
final RadioGroup rg = (RadioGroup) layoutSelectorView.findViewById(R.id.layouts_radiogroup);
final FrameLayout previewWrapper = (FrameLayout) layoutSelectorView.findViewById(R.id.preview_wrapper);
if (previewMaxHeightRes != -1) {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
(int) activity.getResources().getDimension(previewMaxHeightRes)
);
previewWrapper.setLayoutParams(lp);
//previewWrapper.setMinimumHeight((int) activity.getResources().getDimension(previewMaxHeightRes));
}
ViewStub preview = (ViewStub) layoutSelectorView.findViewById(R.id.layout_stub);
preview.setLayoutResource(previewLayout);
final View inflated = preview.inflate();
for (int i = 0; i < layoutTitles.length; i++) {
final String layoutTitle = layoutTitles[i];
final AppCompatRadioButton radioButton = new AppCompatRadioButton(activity);
radioButton.setText(layoutTitle);
final int finalI = i;
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectCallback.onSelected(layoutTitle, finalI, inflated);
}
});
if (textColor != -1) {
radioButton.setTextColor(Service.getColorAttribute(textColor, R.color.black_text, activity));
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
Color.GRAY, //Disabled
Service.getColorAttribute(R.attr.colorAccent, R.color.accent, activity), //Enabled
}
);
radioButton.setSupportButtonTintList(colorStateList);
}
radioButton.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, // Width
(int) activity.getResources().getDimension(R.dimen.layout_selector_height) // Height
));
rg.addView(radioButton, i);
if ((selectedLayoutIndex != -1 && selectedLayoutIndex == i) || (selectedLayoutTitle != null && selectedLayoutTitle.equals(layoutTitle))) {
radioButton.performClick();
}
}
}