getView<wbr style="line-height:22px">是在<span style="line-height:22px"><wbr style="line-height:22px">Adapter</wbr></span><wbr style="line-height:22px">中申明<br style="line-height:22px"><span style="color:#993300; line-height:22px">publicabstractView</span><span style="color:#ff6600; line-height:22px">getView</span>(intposition,ViewconvertView,ViewGroupparent)<br style="line-height:22px">
Since:APILevel1<br style="line-height:22px"><span style="color:#003366; line-height:22px">GetaViewthatdisplaysthedataatthespecifiedpositioninthedataset.YoucaneithercreateaViewmanuallyorinflateitfromanXMLlayoutfile.WhentheViewisinflated,theparentView(GridView,ListView...)willapplydefaultlayoutparametersunlessyouuseinflate(int,android.view.ViewGroup,boolean)tospecifyarootviewandtopreventattachmenttotheroot.</span><br style="line-height:22px">
Parameters<br style="line-height:22px"><span style="color:#993300; line-height:22px">position</span>Thepositionoftheitemwithintheadapter'sdatasetoftheitemwhoseviewwewant.<br style="line-height:22px"><span style="color:#993300; line-height:22px">convertView</span>Theoldviewtoreuse,ifpossible.Note:Youshouldcheckthatthisviewisnon-nullandofanappropriatetypebeforeusing.Ifitisnotpossibletoconvertthisviewtodisplaythecorrectdata,thismethodcancreateanewview.<br style="line-height:22px"><span style="color:#993300; line-height:22px">parent</span>Theparentthatthisviewwilleventuallybeattachedto<br style="line-height:22px">
//什么时候需要把新生成的View连接到parent?<br style="line-height:22px">
Returns<br style="line-height:22px">
*AViewcorrespondingtothedataatthespecifiedposition.<br style="line-height:22px">
我写了个测试程序(见附件1)测试ArrayAdapter,最终得到如下结论<br style="line-height:22px"><span style="color:#000080; line-height:22px">1、 该函数在绘画的时候才调用。<br style="line-height:22px">
2、 ArrayAdapter中每一项数据就对应一个视图convertView。</span><br style="line-height:22px"><span style="color:#000080; line-height:22px">如果ArrayAdapter有7项数据那么对应的View也就有7个</span><br style="line-height:22px"><span style="color:#000080; line-height:22px">3、 第一次画某项时convertView为null,要创建View。下次在画该项是convertView就不为空了。他就是上次的创建View。就可以利用该View而不用再创建。</span><br style="line-height:22px">
但是对ViewGroupparent的还是很困惑<br style="line-height:22px">
什么时候需要把新生成的View连接到parent?<br style="line-height:22px"><span style="color:#3366ff; line-height:22px"> </span><span style="color:#993300; line-height:22px">publicView</span><span style="color:#3366ff; line-height:22px"></span><span style="color:#ff6600; line-height:22px">getView(</span><span style="color:#3366ff; line-height:22px">intposition,ViewconvertView,ViewGroupparent)<br style="line-height:22px">
{<br style="line-height:22px">
TextViewv=null;<br style="line-height:22px"></span><span style="color:#993300; line-height:22px">if</span><span style="color:#3366ff; line-height:22px">(convertView==null){<br style="line-height:22px">
LayoutInflatervi=null;<br style="line-height:22px">
vi=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);<br style="line-height:22px"></span><span style="color:#808080; line-height:22px">//这里只是把它的LayoutParams参数给新生成的View用,并没把生成的View连接到root</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px">//v=(TextView)vi.inflate(R.layout.list_row,parent,false);</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px"></span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px">v=(TextView)vi.inflate(R.layout.list_row,null);</span><br style="line-height:22px"><span style="color:#808080; line-height:22px"> /*什么时候需要把新生成的View连接到parent?<br style="line-height:22px">
反正在ArrayAdapter是不支持。会抛异常<br style="line-height:22px">
java.lang.UnsupportedOperationException:addView(View,LayoutParams)is<br style="line-height:22px">
notsupportedinAdapterView<br style="line-height:22px">
*/</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px"></span><span style="color:#808080; line-height:22px">//v=(TextView)vi.inflate(R.layout.list_row,parent);</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px">}</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px"></span><span style="color:#993300; line-height:22px">else</span><span style="color:#3366ff; line-height:22px">{</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px">v=(TextView)convertView;</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px">}</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px">v.setText((String)this.getItem(position));</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px">returnv;</span><br style="line-height:22px"><span style="color:#3366ff; line-height:22px">}</span><br style="line-height:22px">
附件1:<br style="line-height:22px"><span style="color:#993300; line-height:22px">publicclass</span><span style="color:#ff6600; line-height:22px">HelloList</span>extendsListActivity{<br style="line-height:22px">
publicvoidonCreate(Bundleicicle){<br style="line-height:22px">
super.onCreate(icicle);<br style="line-height:22px">
setContentView(R.layout.main3);<br style="line-height:22px">
List<String>items=fillArray();<br style="line-height:22px">
ArrayAdapter<String>adapter=newMyArrayAdapter<String>(this,R.layout.list_row,items);<br style="line-height:22px">
this.setListAdapter(adapter);<br style="line-height:22px">
mHandler=newHandler();<br style="line-height:22px">
mHandler.postDelayed(mRunnable,speed);<br style="line-height:22px">
}<br style="line-height:22px">
privateList<String>fillArray(){<br style="line-height:22px">
List<String>items=newArrayList<String>();<br style="line-height:22px">
items.add("1");<br style="line-height:22px">
items.add("2");<br style="line-height:22px">
items.add("3");<br style="line-height:22px">
items.add("4");<br style="line-height:22px">
items.add("5");<br style="line-height:22px">
items.add("6");<br style="line-height:22px">
items.add("7");<br style="line-height:22px">
returnitems;<br style="line-height:22px">
}<br style="line-height:22px">
@Override<br style="line-height:22px">
protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid)<br style="line-height:22px">
{<br style="line-height:22px">
TextViewtxt=(TextView)this.findViewById(R.id.text);<br style="line-height:22px">
txt.setText("selected"+l.getSelectedItem().toString()+"!");<br style="line-height:22px">
}<br style="line-height:22px">
privateHandlermHandler;<br style="line-height:22px">
publicintspeed=1000;<br style="line-height:22px">
publicintcnt=0;<br style="line-height:22px">
privatefinalRunnablemRunnable=newRunnable(){<br style="line-height:22px">
publicvoidrun(){<br style="line-height:22px">
cnt++;<br style="line-height:22px">
intk=HelloList.this.getListView().getChildCount();<br style="line-height:22px">
HelloList.this.getListView().invalidateViews();<br style="line-height:22px">
Log.i("hubin",k+"run"+cnt);<br style="line-height:22px"><br style="line-height:22px">
mHandler.postDelayed(this,speed);<br style="line-height:22px">
}<br style="line-height:22px">
};<br style="line-height:22px">
}<br style="line-height:22px"><span style="color:#993300; line-height:22px">class</span><span style="color:#ff6600; line-height:22px">MyArrayAdapter<T></span>extendsArrayAdapter<T><br style="line-height:22px">
{<br style="line-height:22px">
MyArrayAdapter(Contextcontext,inttextViewResourceId)<br style="line-height:22px">
{<br style="line-height:22px">
super(context,textViewResourceId);<br style="line-height:22px">
}<br style="line-height:22px">
MyArrayAdapter(Contextcontext,inttextViewResourceId,List<T>objects)<br style="line-height:22px">
{<br style="line-height:22px">
super(context,textViewResourceId,objects);<br style="line-height:22px">
}<br style="line-height:22px">
publicViewgetView(intposition,ViewconvertView,ViewGroupparent)<br style="line-height:22px">
{<br style="line-height:22px">
Viewv=super.getView(position,convertView,parent);<br style="line-height:22px">
Log.i("hubin","v:"+v+"cv:"+convertView);<br style="line-height:22px">
returnv;<br style="line-height:22px">
}<br style="line-height:22px">
}<br style="line-height:22px">
测试结果如下:<br style="line-height:22px"><span style="color:#3366ff; line-height:22px">v:android.widget.TextView@43d0d488cv:null<br style="line-height:22px">
v:android.widget.TextView@43d0dec8cv:null<br style="line-height:22px">
v:android.widget.TextView@43d0e6e0cv:null<br style="line-height:22px">
v:android.widget.TextView@43d0eef8cv:null<br style="line-height:22px">
v:android.widget.TextView@43d0f710cv:null<br style="line-height:22px">
v:android.widget.TextView@43d0ff28cv:null<br style="line-height:22px">
v:android.widget.TextView@43d10740cv:null<br style="line-height:22px">
7run1<br style="line-height:22px">
v:android.widget.TextView@43d10740cv:android.widget.TextView@43d10740<br style="line-height:22px">
v:android.widget.TextView@43d0ff28cv:android.widget.TextView@43d0ff28<br style="line-height:22px">
v:android.widget.TextView@43d0f710cv:android.widget.TextView@43d0f710<br style="line-height:22px">
v:android.widget.TextView@43d0eef8cv:android.widget.TextView@43d0eef8<br style="line-height:22px">
v:android.widget.TextView@43d0e6e0cv:android.widget.TextView@43d0e6e0<br style="line-height:22px">
v:android.widget.TextView@43d0dec8cv:android.widget.TextView@43d0dec8<br style="line-height:22px">
v:android.widget.TextView@43d0d488cv:android.widget.TextView@43d0d488<br style="line-height:22px">
7run2<br style="line-height:22px">
v:android.widget.TextView@43d0d488cv:android.widget.TextView@43d0d488<br style="line-height:22px">
v:android.widget.TextView@43d0dec8cv:android.widget.TextView@43d0dec8<br style="line-height:22px">
v:android.widget.TextView@43d0e6e0cv:android.widget.TextView@43d0e6e0<br style="line-height:22px">
v:android.widget.TextView@43d0eef8cv:android.widget.TextView@43d0eef8<br style="line-height:22px">
v:android.widget.TextView@43d0f710cv:android.widget.TextView@43d0f710<br style="line-height:22px">
v:android.widget.TextView@43d0ff28cv:android.widget.TextView@43d0ff28<br style="line-height:22px">
v:android.widget.TextView@43d10740cv:android.widget.TextView@43d10740</span></wbr></wbr>
关于适配器的getView()函数
最新推荐文章于 2025-08-08 23:51:41 发布