在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体widget控件(如:Button,TextView等)。有很多地方可以使用:
对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.如BaseAdapter的getView中,自定义Dialog中取得view中的组件widget等等。
下面这是个简单的BaseAdapter:
首先声明LayoutInflater
private LayoutInflater inflater=null;
在getView()方法里使用
inflater=LayoutInflater.from(context);
View v=inflater.inflate(R.layout.listview_item, null);
完整代码:
public class PullXmlAdapter extends BaseAdapter {
private List<Book> list=null;
private Context context=null;
private LayoutInflater inflater=null;
public PullXmlAdapter(Context context,List<Book> list) {
// TODO Auto-generated constructor stub
this.context=context;
this.list=list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
inflater=LayoutInflater.from(context);
View v=inflater.inflate(R.layout.listview_item, null);
TextView tv=(TextView)v.findViewById(R.id.lisview_item_tv);
tv.setText(list.get(arg0).getName());
return v;
}
}
分享到:
相关推荐
本文实例讲述了Android开发中LayoutInflater用法。分享给大家供大家参考,具体如下: 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/...
LayoutInflater类在我们日常开发中经常会用到,最近在使用中就遇到了一些问题,所有下面这篇文章主要给大家总结了关于Android中使用LayoutInflater要注意的一些坑,希望通过这篇能让大家避免走一些弯路,需要的朋友...
对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? 2.加载layout文件之后,又是怎么变成供...
前言 Activity 在界面创建时需要将 XML 布局文件中的内容加载进来,正如我们在 ListView 或者 RecyclerView 中需要将 Item 的布局加载进来一样,都是使用 ...由于 Android 系统源码中关于 Content 部分采用的是装饰模
Android LayoutInflater.inflate()详解 深入理解LayoutInflater.inflate() 由于我们很容易习惯公式化的预置代码,有时我们会忽略很优雅的细节。LayoutInflater以及它在Fragment的onCreateView()中填充View的方式...
LayoutInflater.inflate源码详解 LayoutInflater的inflate方法相信大家都不陌生,在Fragment的onCreateView中或者在BaseAdapter的getView方法中我们都会经常用这个方法来实例化出我们需要的View. 假设我们有一个...
本文实例讲述了Android实现在列表List中显示半透明小窗体效果的控件用法。分享给大家供大家参考,具体如下: Android 在列表List中显示半透明...import android.view.LayoutInflater; import android.view.View; import
本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView。 系统显示列表(ListView)时,首先会实例化一个适配器,本文...
安卓开发简易电话拨号器,具体内容如下 我是基于安卓4.2.2开发的,下面是我写的MainActivity.java代码: ... import android.support.v7.app.ActionBarActivity;...import android.view.LayoutInflater; impo
// 真实环境中要赋值 int layoutId = 0; // 布局ID View contentView = LayoutInflater.from(context).inflate(layoutId, null); final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams....
Android getViewById和getLayoutInflater().inflate()的详解及比较 由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以...
Android ListView里控件添加监听方法的实例详解 关于ListView,算是android中比较常见的控件,在ListView我们通常需要一个模板,这个模板指的不是住模块,而是配置显示在ListView里面的东西,今天做项目的时候发现...
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) inflater用于绑定Fragment的布局文件,同时将该布局转换成View对象并返回;container为Fragment的UI所在的父容器。...