GridView依然是BaseAdapter和SimpleAdapter两种实现方法
BaseAdapter的实现类
package com.phone.hty.myapplication;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/** * 作者:司马啸尘 * 创建日期:2016/3/25 * 描述: */
public class MyAdapter extends BaseAdapter {
private Context context;
private int[] images;
private String[] desc;
public MyAdapter(Context context, int[] images, String[] desc) {
this.context = context;
this.images = images;
this.desc = desc;
}
@Override
public int getCount() {
return desc.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.image, null);
viewHolder = new ViewHolder();
viewHolder.mTextView = (TextView) convertView.findViewById(R.id.textViewShow);
viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.ImageViewShow);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
System.out.println(images[position]);
viewHolder.mImageView.setImageResource(images[position]);
viewHolder.mTextView.setText(desc[position]);
return convertView;
}
}
final class ViewHolder {
public ImageView mImageView;
public TextView mTextView;
}
Activity中GridView的创建
package com.phone.hty.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] images = new int[]{
R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12, R.drawable.bomb13,
R.drawable.bomb14, R.drawable.bomb15, R.drawable.bomb16, R.drawable.bomb9,
R.drawable.bomb5, R.drawable.bomb6, R.drawable.bomb7, R.drawable.bomb8,
};
GridView mGridView = (GridView) findViewById(R.id.gv);
MyAdapter myAdapter = new MyAdapter(this,images,
getResources().getStringArray(R.array.desc));
mGridView.setAdapter(myAdapter);
}
}
xml文件中字符串数组的定义
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="desc">
<item>@string/desc1</item>
<item>@string/desc2</item>
<item>@string/desc3</item>
<item>@string/desc4</item>
<item>@string/desc5</item>
<item>@string/desc6</item>
<item>@string/desc7</item>
<item>@string/desc8</item>
<item>@string/desc9</item>
<item>@string/desc10</item>
<item>@string/desc11</item>
<item>@string/desc12</item>
</array>
</resources>
实现结果图
更新Activity中的代码
package com.phone.hty.myapplication;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int[] images = new int[]{
R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12, R.drawable.bomb13,
R.drawable.bomb14, R.drawable.bomb15, R.drawable.bomb16, R.drawable.bomb9,
R.drawable.bomb5, R.drawable.bomb6, R.drawable.bomb7, R.drawable.bomb8,
};
TypedArray typedArray = getResources().obtainTypedArray(R.array.image);
int[] image = new int[typedArray.length()];
for (int i = 0;i<typedArray.length();i++){
image[i] = typedArray.getResourceId(i,0);
}
GridView mGridView = (GridView) findViewById(R.id.gv);
MyAdapter myAdapter = new MyAdapter(this,image,
getResources().getStringArray(R.array.desc));
mGridView.setAdapter(myAdapter);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
System.out.println(position);
break;
case 1:
System.out.println(position);
break;
}
}
});
}
}
其中访问drawable数组的方法代码是
TypedArray typedArray = getResources().obtainTypedArray(R.array.image); int[] image = new int[typedArray.length()];
for (int i = 0;i<typedArray.length();i++){
image[i] = typedArray.getResourceId(i,0);
}