下面列出了android.view.View#inflate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
H holder = null;
T item = data.get(position);
if (convertView == null) {
convertView = View.inflate(context, getLayoutId(), null);
holder = newViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (H) convertView.getTag();
}
convert(holder, item, position);
return convertView;
}
public static void showAtCenter(Context mContext, String msg) {
if (mContext == null) {
return;
}
if (msg == null) {
return;
}
View toastRoot = View.inflate(mContext, R.layout.view_toast_center, null);
TextView tv_text = (TextView) toastRoot.findViewById(R.id.tv_content);
if (tv_text != null) {
tv_text.setText(msg);
}
DToast.make(mContext)
.setView(toastRoot)
.setGravity(Gravity.CENTER, 0, 0)
.show();
}
@SuppressWarnings("unchecked")
private View getView1(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.view_ucache_getall_list_item, null);
viewHolder = new ViewHolder();
viewHolder.tvKey = ResHelper.forceCast(convertView.findViewById(R.id.tvKey));
viewHolder.tvValue = ResHelper.forceCast(convertView.findViewById(R.id.tvValue));
convertView.setTag(viewHolder);
} else {
viewHolder = ResHelper.forceCast(convertView.getTag());
}
HashMap<String, Object> data = getItem(position);
viewHolder.tvKey.setText(String.valueOf(data.get("k")));
viewHolder.tvValue.setText(String.valueOf(data.get("v")));
return convertView;
}
/**
* show a dialog to query user whether backup contacts and sms
*/
private void onBackup() {
// check sdcard
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Toast.makeText(context, R.string.tips_sdcard_not_found, Toast.LENGTH_SHORT).show();
return;
}
// create backup view
View view = View.inflate(context, R.layout.dialog_contacts_sms_backup, null);
final CheckBox cbBackupContacts = (CheckBox) view.findViewById(R.id.cb_backup_contacts);
final CheckBox cbBackupSms = (CheckBox) view.findViewById(R.id.cb_backup_sms);
new AlertDialog.Builder(context)
.setTitle(R.string.tips)
.setMessage(R.string.message_backup)
.setView(view)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startBackup(cbBackupContacts.isChecked(), cbBackupSms.isChecked());
}
})
.setNegativeButton(R.string.cancel, null)
.show();
}
@Override
public View getView(int position, View rootView, ViewGroup parent) {
ViewHolder holder;
if (rootView == null) {
holder = new ViewHolder();
rootView = View.inflate(context, R.layout.search_result_gridview_item, null);
holder.item =(RelativeLayout) rootView.findViewById(R.id.search_item);
holder.icon =(ImageView) rootView.findViewById(R.id.icon);
holder.name = (TextView) rootView.findViewById(R.id.name);
rootView.setTag(holder);
} else {
holder = (ViewHolder) rootView.getTag();
}
final FileInfo fileInfo = list.get(position);
holder.icon.setImageResource(FileType.getResId(fileInfo.getType()));
holder.name.setText(fileInfo.getName());
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v,fileInfo);
}
});
return rootView;
}
@Override
public Object instantiateItemObject(ViewGroup container, int position) {
View view = View.inflate(container.getContext(), android.R.layout.test_list_item, null);
((TextView)view.findViewById(android.R.id.text1)).setText(strings.get(position));
container.addView(view);
// set Random background
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
view.setBackgroundColor(color);
return view;
}
@Override
public View onCreateView() {
View inflate = View.inflate(mContext, R.layout.popup_bubble, null);
mLlContent = (LinearLayout) inflate.findViewById(R.id.ll_content);
mTriangleView = (TriangleView) inflate.findViewById(R.id.triangle_view);
mLlContent.addView(mWrappedView);
mLayoutParams = (RelativeLayout.LayoutParams) mLlContent.getLayoutParams();
mTriangleLayoutParams = (RelativeLayout.LayoutParams) mTriangleView.getLayoutParams();
//让mOnCreateView充满父控件,防止ViewHelper.setXY导致点击事件无效
inflate.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
return inflate;
}
private void initView(Context context, AttributeSet attributeSet) {
@SuppressLint("CustomViewStyleable")
TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.empty_layout);
View view = View.inflate(context, R.layout.empty_layout, this);
errorLayout = view.findViewById(R.id.rl_empty_layout_error);
emptyLayout= view.findViewById(R.id.rl_empty_layout_empty);
loadingLayout = view.findViewById(R.id.rl_empty_layout_loading);
loadingImage= view.findViewById(R.id.iv_empty_loading_image);
view.findViewById(R.id.rl_empty_layout_error).setOnClickListener(this);
view.findViewById(R.id.rl_empty_layout_empty).setOnClickListener(this);
typedArray.recycle();
updateViewVisible();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
SectionHolder sectionHolder = null;
switch (getItemViewType(position)) {
case VIEW_TYPE_SECTION_HEADER:
if(view == null) {
view = View.inflate(mContext, mSectionHeaderLayoutId, null);
sectionHolder = new SectionHolder();
sectionHolder.titleTextView = (TextView) view.findViewById(mSectionTitleTextViewId);
view.setTag(sectionHolder);
} else {
sectionHolder = (SectionHolder) view.getTag();
}
break;
default:
view = mListAdapter.getView(getIndexForPosition(position),
convertView, parent);
break;
}
if(sectionHolder != null) {
String sectionName = sectionTitleForPosition(position);
sectionHolder.titleTextView.setText(sectionName);
}
return view;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fragment_home, null);
ButterKnife.bind(this, view);
initData(); // 初始化页面数据
return view;
}
@Override public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder appHolder;
View view = convertView;
if (view == null) {
view = View.inflate(context, R.layout.list_generic_item, null);
appHolder = new ViewHolder();
appHolder.icon = view.findViewById(R.id.item_icon);
appHolder.name = view.findViewById(R.id.item_name);
view.setTag(appHolder);
} else {
appHolder = (ViewHolder) view.getTag();
}
appHolder.name.setText(hiddenAppsList.get(position).getAppName());
if (hiddenAppsList.get(position).isAppHidden()) {
appHolder.icon.setImageResource(R.drawable.ic_check);
appHolder.name.setTypeface(Typeface.DEFAULT_BOLD);
} else {
appHolder.icon.setImageDrawable(hiddenAppsList.get(position).getIcon());
appHolder.name.setTypeface(Typeface.DEFAULT);
}
return view;
}
@Override
public View getContentView() {
return View.inflate(context, R.layout.activity_my_photo, null);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(mContext, R.layout.item_bus_segment,
null);
holder.lineName = (TextView) convertView
.findViewById(R.id.bus_line_name);
holder.dirIcon = (ImageView) convertView
.findViewById(R.id.bus_dir_icon);
holder.dirUp = (ImageView) convertView
.findViewById(R.id.bus_dir_icon_up);
holder.dirDown = (ImageView) convertView
.findViewById(R.id.bus_dir_icon_down);
holder.splitLine = (ImageView) convertView
.findViewById(R.id.bus_seg_split_line);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final RideStep item = mItemList.get(position);
if (position == 0) {
holder.dirIcon.setImageResource(R.drawable.dir_start);
holder.lineName.setText("出发");
holder.dirUp.setVisibility(View.INVISIBLE);
holder.dirDown.setVisibility(View.VISIBLE);
holder.splitLine.setVisibility(View.INVISIBLE);
return convertView;
} else if (position == mItemList.size() - 1) {
holder.dirIcon.setImageResource(R.drawable.dir_end);
holder.lineName.setText("到达终点");
holder.dirUp.setVisibility(View.VISIBLE);
holder.dirDown.setVisibility(View.INVISIBLE);
return convertView;
} else {
holder.splitLine.setVisibility(View.VISIBLE);
holder.dirUp.setVisibility(View.VISIBLE);
holder.dirDown.setVisibility(View.VISIBLE);
String actionName = item.getAction();
int resID = AMapUtil.getWalkActionID(actionName);
holder.dirIcon.setImageResource(resID);
holder.lineName.setText(item.getInstruction());
return convertView;
}
}
private void showEditTopicText() {
final Activity activity = getActivity();
if (activity == null) {
return;
}
VxCard pub = mTopic.getPub();
final String title = pub == null ? null : pub.fn;
final PrivateType priv = mTopic.getPriv();
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final View editor = View.inflate(builder.getContext(), R.layout.dialog_edit_group, null);
builder.setView(editor).setTitle(R.string.edit_topic);
final EditText titleEditor = editor.findViewById(R.id.editTitle);
final EditText subtitleEditor = editor.findViewById(R.id.editPrivate);
if (mTopic.isOwner()) {
if (!TextUtils.isEmpty(title)) {
titleEditor.setText(title);
titleEditor.setSelection(title.length());
}
} else {
editor.findViewById(R.id.editTitleWrapper).setVisibility(View.GONE);
}
if (priv != null && !TextUtils.isEmpty(priv.getComment())) {
subtitleEditor.setText(priv.getComment());
}
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String newTitle = null;
if (mTopic.isOwner()) {
newTitle = titleEditor.getText().toString().trim();
}
String newPriv = subtitleEditor.getText().toString().trim();
UiUtils.updateTitle(activity, mTopic, newTitle, newPriv,
new UiUtils.TitleUpdateCallbackInterface() {
@Override
public void onTitleUpdated() {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
notifyContentChanged();
}
});
}
});
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
private void init(final Context context, AttributeSet attrs)
{
final View rootView = View.inflate(context, R.layout.view_expandable, this);
headerLayout = (FrameLayout) rootView.findViewById(R.id.view_expandable_headerlayout);
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableLayout);
final int headerID = typedArray.getResourceId(R.styleable.ExpandableLayout_el_headerLayout, -1);
final int contentID = typedArray.getResourceId(R.styleable.ExpandableLayout_el_contentLayout, -1);
contentLayout = (FrameLayout) rootView.findViewById(R.id.view_expandable_contentLayout);
if (headerID == -1 || contentID == -1)
throw new IllegalArgumentException("HeaderLayout and ContentLayout cannot be null!");
if (isInEditMode())
return;
duration = typedArray.getInt(R.styleable.ExpandableLayout_el_duration, getContext().getResources().getInteger(android.R.integer.config_shortAnimTime));
final View headerView = View.inflate(context, headerID, null);
headerView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
headerLayout.addView(headerView);
setTag(ExpandableLayoutItem.class.getName());
final View contentView = View.inflate(context, contentID, null);
contentView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
contentLayout.addView(contentView);
contentLayout.setVisibility(GONE);
headerLayout.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (isOpened() && event.getAction() == MotionEvent.ACTION_UP)
{
hide();
closeByUser = true;
}
return isOpened() && event.getAction() == MotionEvent.ACTION_DOWN;
}
});
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = View.inflate(parent.getContext(), android.R.layout.simple_list_item_1, null);
itemView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
return new MyViewHolder(itemView);
}
View createPresetsView() {
View contentView = View.inflate(getActivity(), R.layout.cpv_dialog_presets, null);
shadesLayout = contentView.findViewById(R.id.shades_layout);
transparencySeekBar = contentView.findViewById(R.id.transparency_seekbar);
transparencyPercText = contentView.findViewById(R.id.transparency_text);
GridView gridView = contentView.findViewById(R.id.gridView);
loadPresets();
if (showColorShades) {
createColorShades(color);
} else {
shadesLayout.setVisibility(View.GONE);
contentView.findViewById(R.id.shades_divider).setVisibility(View.GONE);
}
adapter = new ColorPaletteAdapter(new ColorPaletteAdapter.OnColorSelectedListener() {
@Override
public void onColorSelected(int newColor) {
if (color == newColor) {
ColorPickerDialogPreference.this.onColorSelected(
);
dismiss();
return;
}
color = newColor;
if (showColorShades) {
createColorShades(color);
}
}
}, presets, getSelectedItemPosition(), colorShape);
gridView.setAdapter(adapter);
if (showAlphaSlider) {
setupTransparency();
} else {
contentView.findViewById(R.id.transparency_layout).setVisibility(View.GONE);
contentView.findViewById(R.id.transparency_title).setVisibility(View.GONE);
}
return contentView;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAddParam: {
// 添加新的请求参数
View llParam = View.inflate(this, R.layout.view_custom_param, null);
llCustom.addView(llParam, 1 + llParams.size());
llParams.add(llParam);
findViewById(R.id.btnRemoveParam).setEnabled(true);
} break;
case R.id.btnRemoveParam: {
// 删除最后一个请求参数
if (llParams.size() > 0) {
llCustom.removeViewAt(llParams.size());
llParams.remove(llParams.size() - 1);
if (llParams.isEmpty()) {
findViewById(R.id.btnRemoveParam).setEnabled(false);
}
}
} break;
case R.id.btnAddFile: {
// 添加文件
if (llFile == null) {
llFile = View.inflate(this, R.layout.view_custom_file, null);
llCustom.addView(llFile, llCustom.getChildCount() - 3);
findViewById(R.id.btnRemoveFile).setEnabled(true);
findViewById(R.id.btnGet).setEnabled(false);
}
} break;
case R.id.btnRemoveFile: {
// 删除文件
if (llFile != null) {
llCustom.removeView(llFile);
llFile = null;
findViewById(R.id.btnRemoveFile).setEnabled(false);
findViewById(R.id.btnGet).setEnabled(true);
}
} break;
case R.id.btnGet: {
// 执行GET请求
requestGet();
} break;
case R.id.btnPost: {
// 执行POST请求
requestPost();
} break;
}
}
@Override
public View getContentView() {
return View.inflate(context, R.layout.dialog_alert, null);
}
/**
* Inflates layout from <code>xml</code> and encapsulates it with <code>RecyclerViewHeader</code>.
*
* @param context application context.
* @param layoutRes layout resource to be inflated.
* @return <code>RecyclerViewHeader</code> view object.
*/
public static RecyclerViewHeader fromXml(Context context, @LayoutRes int layoutRes) {
RecyclerViewHeader header = new RecyclerViewHeader(context);
View.inflate(context, layoutRes, header);
return header;
}