关于适配器的getView 函数

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

               
getView是在 Adapter中申明
public abstract View  getView (int position, View convertView, ViewGroup parent)
Since: API Level 1
Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root.
Parameters
position     The position of the item within the adapter's data set of the item whose view we want.
convertView     The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
parent     The parent that this view will eventually be attached to
    //什么时候需要把新生成的View连接到parent?
Returns
    * A View corresponding to the data at the specified position. 
我写了个测试程序(见附件1)测试ArrayAdapter,最终得到如下结论
1、 该函数在绘画的时候才调用。
2、 ArrayAdapter中每一项数据就对应一个视图convertView。
如果ArrayAdapter有7项数据那么对应的View也就有7个
3、 第一次画某项时convertView为null,要创建View。下次在画该项是convertView就不为空了。他就是上次的创建 View。就可以利用该View而不用再创建。
但是对ViewGroup parent的还是很困惑
什么时候需要把新生成的View连接到parent?
    public View          getView( int position, View  convertView, ViewGroup  parent)
     {
         TextView v =null;
            if (convertView==null){
               LayoutInflater vi = null;
               vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                //这里只是把它的LayoutParams参数给新生成的View用,并没把生成的View连接到root
               //v =(TextView)vi.inflate(R.layout.list_row, parent, false);
               
               v =(TextView)vi.inflate(R.layout.list_row,null);
   /*什么时候需要把新生成的View连接到parent?
    反正在ArrayAdapter是不支持。会抛异常
    java.lang.UnsupportedOperationException: addView(View, LayoutParams) is 
    not supported in AdapterView
    */
     //v =(TextView)vi.inflate(R.layout.list_row,parent);
           }
            else {
               v = (TextView)convertView;
           }
               v.setText((String)this.getItem(position));
           return v;
     }
附件1:
public class  HelloList extends ListActivity{    
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main3);        
        List<String> items = fillArray();        
        ArrayAdapter<String> adapter = new MyArrayAdapter<String>(this,R.layout.list_row,items);              
        this.setListAdapter(adapter);
        mHandler = new Handler();
        mHandler.postDelayed(mRunnable,speed);
    }
    private List<String> fillArray(){
        List<String> items = new ArrayList<String>();
        items.add("1");
        items.add("2");
        items.add("3");
        items.add("4");
        items.add("5");
        items.add("6");
        items.add("7");
        return items;
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        TextView txt = (TextView)this.findViewById(R.id.text);
        txt.setText("selected"+l.getSelectedItem().toString()+"!");
    }
    private Handler mHandler;
    public int speed=1000;
    public int cnt=0;
    private final Runnable mRunnable = new Runnable() {
        public void run() {
            cnt++;
            int k=HelloList.this.getListView().getChildCount();
            HelloList.this.getListView().invalidateViews();
            Log.i("hubin",k+"run"+cnt);
            
                mHandler.postDelayed(this, speed);
        }
    };    
}
class  MyArrayAdapter<T> extends ArrayAdapter<T>
{
    MyArrayAdapter(Context  context, int textViewResourceId)
    {
        super(context, textViewResourceId);
    }
    MyArrayAdapter(Context  context, int textViewResourceId,List<T>  objects)
    {
        super(context, textViewResourceId,objects);
    }
     public View        getView(int position, View  convertView, ViewGroup  parent)
     {
         View v=super.getView(position, convertView, parent);
         Log.i("hubin","v:"+v+"cv:"+convertView);
         return v;
     }
}
测试结果如下:
v:android.widget.TextView@43d0d488 cv:null
v:android.widget.TextView@43d0dec8 cv:null
v:android.widget.TextView@43d0e6e0 cv:null
v:android.widget.TextView@43d0eef8 cv:null
v:android.widget.TextView@43d0f710 cv:null
v:android.widget.TextView@43d0ff28 cv:null
v:android.widget.TextView@43d10740 cv:null
7run1
v:android.widget.TextView@43d10740 cv:android.widget.TextView@43d10740
v:android.widget.TextView@43d0ff28 cv:android.widget.TextView@43d0ff28
v:android.widget.TextView@43d0f710 cv:android.widget.TextView@43d0f710
v:android.widget.TextView@43d0eef8 cv:android.widget.TextView@43d0eef8
v:android.widget.TextView@43d0e6e0 cv:android.widget.TextView@43d0e6e0
v:android.widget.TextView@43d0dec8 cv:android.widget.TextView@43d0dec8
v:android.widget.TextView@43d0d488 cv:android.widget.TextView@43d0d488
7run2
v:android.widget.TextView@43d0d488 cv:android.widget.TextView@43d0d488
v:android.widget.TextView@43d0dec8 cv:android.widget.TextView@43d0dec8
v:android.widget.TextView@43d0e6e0 cv:android.widget.TextView@43d0e6e0
v:android.widget.TextView@43d0eef8 cv:android.widget.TextView@43d0eef8
v:android.widget.TextView@43d0f710 cv:android.widget.TextView@43d0f710
v:android.widget.TextView@43d0ff28 cv:android.widget.TextView@43d0ff28
v:android.widget.TextView@43d10740 cv:android.widget.TextView@43d10740
           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值