Adapters与LayoutInflater与LayoutInflater.Factory

待续。//LayoutInflater.Factory未加入。

external source: a list that code supplies / query from db

file:///home/ivan/%E8%BD%AF%E4%BB%B6/eclipse/android-sdk/docs/guide/topics/ui/binding.html

Class Overview:

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

//--------------------------

LayoutInflater:

This class is used to instantiate layout XML file into its corresponding View objects. 

To create a new LayoutInflater with an additional LayoutInflater.Factory for your own views, you can use cloneInContext(Context) to clone an existing ViewFactory, and then call setFactory(LayoutInflater.Factory) on it to include your Factory.

Adapters与LayoutInflater与LayoutInflater.Factory

Adapters与LayoutInflater与LayoutInflater.Factory

ListView常用,

listview加载adapter过程是这样的.

1 先判断adapter 有多少数据项,根据这个数据确定有多少item. 
2 确定每个item里加载哪个View. 
3 把View里加载要显示的数据.

使用时常与LayoutInflater一起使用,

Adapters负责将数据与single listView item绑定,

但之前需要layoutInflater将相关listView item的XML文件inflate进入context,

所以常见格式如下:

public class MyAdapter extends BaseAdapter{   // ArrayAdapter[T]

Private LayoutInflater inflater;

private String array[] = {"","", ""};



Private Context mContext;



public MyAdapter(Context context){



  this.mContext = context;



  inflater = LayoutInflater.from(context);



}



.......

@Override

public View getView(int position, View convertView, ViewGroup parent){

       View v;

       if(convert == null){

            inflater.inflate(R.id.list_item, parent, false);

        } else {

        v = convertView;

        }

//此时该view被inflate,之后可以使用里面的其他子view了

      TextView  text =  (TextView)v.findViewById(R.id.list_item_textview);

       text.setText(this.Array[position];



       return v;

} 



}



自此MyAdapter就写好了,下面会有使用例子。

            

在使用的时候,一般是放在onCreateView或者其他onCreate里面(生命周期应该在刚创建的时候)

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, 

               Bundle savedInstanceState){

//思路简单,

//先把view给inflate进来,然后找到listview,再之后进行listview.setAdapter

        View v = inflater.inflate(R.id.list_manage_layout, container, false);

        ListView list = (ListView)v.findViewById(R.id.manage_list);

        list.setListAdapter(new MyAdapter(getActivity()));



       //do stuff here

      

      return v;

}

你可能感兴趣的:(LayoutInflater)