我是Android开发的新手,并且一直关注Android网站上提供的教程。我目前正处于视图教程部分,特别是网格视图教程:Hello, Grid View Tutorial。
我无法理解如何通过适配器进行查看。我知道您必须覆盖适配器类中的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
};
}
答案 0 :(得分:17)
通过设置适配器,您告诉GridView从适配器获取视图以填满所有网格。因此,GridView内部调用getView
来填充布局。
这就是为什么所有适配器都实现了Adapter
interface,以便任何AdapterView都可以请求适配器。
答案 1 :(得分:5)
Adapter.getView
方法中调用 ObtainView
,继承自GridView
父类AbsListView
,并在可用时传入ScrapView对象:
child = mAdapter.getView(position, scrapView, this);
然后ObtainView
直接在GridView.onMeasure
中实现,但也在几个调用GridView.layoutChildren
的回调中实现,包括处理滚动,拖动和其他触摸导航的继承onTouchEvent
方法。