getView()方法是如何使用的,它在哪里被调用?

我是Android开发新手,并一直在Android网站上提供的教程。 我目前在视图的教程部分,特别是网格视图的一个: 你好,网格视图教程 。

我无法理解如何通过适配器进行视图。 我知道你必须重写适配器类中的getView()方法,并且在这个方法中定义了如何设置Views。 我不明白的是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 }; } 

Solutions Collecting From Web of "getView()方法是如何使用的,它在哪里被调用?"

通过设置适配器,您告诉GridView从Adapter中获取视图来填充所有的网格。 所以getView被GridView内部调用来填充布局。

这就是为什么所有适配器都实现Adapter 接口,以便任何AdapterView都可以请求Adapter。

Adapter.getViewObtainView方法内部ObtainView ,inheritance于GridView父类AbsListView ,当ScrapView对象可用时传入:

 child = mAdapter.getView(position, scrapView, this); 

然后, ObtainView直接在GridView.onMeasure中实现,而且在多个调用GridView.layoutChildrencallbackObtainView中实现,包括处理滚动, onTouchEvent和其他触摸导航的inheritance的onTouchEvent方法。