
android的BaseAdapter重写getView()方法,View第一个参数输出两次,为什么? 20
程序输出会出现两次text1的值,第一次为NULL,第二次为TextView,之后输出的全部都是相同TextView,代码:publicViewgetView(intpo...
程序输出会出现两次text1的值,第一次为NULL,第二次为TextView,之后输出的全部都是相同TextView,代码:
public View getView(int position, View text1, ViewGroup parent) { //TextView text=(TextView)text1; Log.d("and4g", "text1="+text1+",position="+position); Log.d("and4g", "parent"+parent+",position="+position); TextView text = new TextView(TestGridViewActivity.this); Resources res = TestGridViewActivity.this.getResources(); text.setWidth((int) res.getDimension(R.dimen.cell_width)); text.setHeight((int) res.getDimension(R.dimen.cell_height)); text.setText(textIds[position]); text.setBackgroundResource(colorIds[position]); //text.setTextSize(20); text.setTextSize(getResources().getInteger(R.integer.font_size)); return text; }
布局文件:
<GridView android:id="@+id/grid01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:horizontalSpacing="@dimen/spacing" android:verticalSpacing="@dimen/spacing" android:numColumns="3" android:gravity="center"></GridView>
结果:
我的图片并
没有移出屏幕应该全是NULL啊? 展开
public View getView(int position, View text1, ViewGroup parent) { //TextView text=(TextView)text1; Log.d("and4g", "text1="+text1+",position="+position); Log.d("and4g", "parent"+parent+",position="+position); TextView text = new TextView(TestGridViewActivity.this); Resources res = TestGridViewActivity.this.getResources(); text.setWidth((int) res.getDimension(R.dimen.cell_width)); text.setHeight((int) res.getDimension(R.dimen.cell_height)); text.setText(textIds[position]); text.setBackgroundResource(colorIds[position]); //text.setTextSize(20); text.setTextSize(getResources().getInteger(R.integer.font_size)); return text; }
布局文件:
<GridView android:id="@+id/grid01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:horizontalSpacing="@dimen/spacing" android:verticalSpacing="@dimen/spacing" android:numColumns="3" android:gravity="center"></GridView>
结果:
我的图片并
没有移出屏幕应该全是NULL啊? 展开
展开全部
你上面的代码是打印,getview里面的缓存的text1,第一次加载的时候加载一屏数据,这时候是第一次加载所以是null,当第一条移出屏幕,下面一条显示的时候,下面刚出来的view要复用第一条item的内存,所以你这时候打印的是text1的“内存地址”哈希编码
追问
那为什么在最后那里又输出了一次position=0的text1为null呢?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询