Android getView() 方法是如何使用的,它在哪里被调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3614779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is the getView() method used and where is it getting called?
提问by amandion
I am new to Android development and have been following the tutorials available on the Android website. I am currently on the section of tutorials for Views, specifically the one for Grid Views: Hello, Grid View Tutorial.
我是 Android 开发新手,一直在关注 Android 网站上提供的教程。我目前在视图教程部分,特别是网格视图教程:你好,网格视图教程。
I am having trouble understanding how views are made through an adapter. I understand that you must override the getView() method in your adapter class and in this method is where you define how your Views are set up. What I don't understand is where does getView() actually get called? Perhaps I've got the wrong kind of mentality here, but in the code below (the Grid View tutorial) I don't see any calls to getView() (or any other things used in the adapter class such as getCount()).
我无法理解如何通过适配器生成视图。我知道您必须覆盖适配器类中的 getView() 方法,并且在此方法中您可以定义视图的设置方式。我不明白的是 getView() 实际上在哪里被调用?也许我在这里有一种错误的心态,但是在下面的代码(网格视图教程)中,我没有看到对 getView() 的任何调用(或适配器类中使用的任何其他东西,例如 getCount()) .
Main.xml
主文件
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
Start.java
启动文件
package com.examples.hellogridlayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Start extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView)findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent,View v, int position, long id){
Toast.makeText(Start.this, "" + position,Toast.LENGTH_SHORT).show();
}
});
}
}
ImageAdapter.java
图像适配器
package com.examples.hellogridlayout;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
采纳答案by bhups
By setting Adapter, you are telling GridView to fetch views from the Adapter to fill up all the grids. So getView
is internally called by GridView to fill up the layout.
通过设置 Adapter,您告诉 GridView 从 Adapter 获取视图以填充所有网格。SogetView
由 GridView 内部调用以填充布局。
Thats why all the adapters implements Adapter
interfaceso that any AdapterView can request the Adapter.
这就是为什么所有的适配器都实现了Adapter
接口,以便任何 AdapterView 都可以请求适配器。
回答by Trent Steele
Adapter.getView
is called inside ObtainView
method, inherited from GridView
parent class AbsListView
, with ScrapView objects passed in when available:
Adapter.getView
在ObtainView
方法内部调用,继承自GridView
父类AbsListView
,当可用时传入 ScrapView 对象:
child = mAdapter.getView(position, scrapView, this);
Then ObtainView
is implemented in GridView.onMeasure
directly but also in several more callbacks that invoke GridView.layoutChildren
, including inherited onTouchEvent
method which handles scroll, fling and other touch navigation.
然后ObtainView
在实现GridView.onMeasure
直接而且在几个回调来调用GridView.layoutChildren
,包括继承的onTouchEvent
方法,其手柄滚动,甩和其他触摸导航。