activity:
/**
* 自定义适配器
*
* 1.创建一个类,继承BaseAdapter类
* 2.实现4个方法
* getCount:获取要显示的选项总数
*
* getItem :获取每一个选项,类情况而定
*
* getItemId:获取每个选项的id
*
* getView :该方法用来为每一个选项生成视图(这里为:ImageView),会多次调用
* 根据屏幕能显示多少个选项而定的,所以每次屏幕滚动时,也会调用
*
*/
public class MainActivity extends Activity {
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new MyAdapter(this));
}
static class MyAdapter extends BaseAdapter{
private Context context;
public MyAdapter(Context context){
this.context = context;
}
private int[] images = {R.mipmap.a,R.mipmap.b,
R.mipmap.a,R.mipmap.b,R.mipmap.a,R.mipmap.b,
R.mipmap.a,R.mipmap.b,R.mipmap.a,R.mipmap.b,
R.mipmap.a,R.mipmap.b,R.mipmap.a,R.mipmap.b};
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return images[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = new ImageView(context);
iv.setImageResource(images[position]);
return iv;
}
}
2.xml:
"match_parent"
android:layout_height="match_parent"
android:id="@+id/gridView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
图文混排:
/**
* 自定义适配器
*
* 1.创建一个类,继承BaseAdapter类
* 2.实现4个方法
* getCount:获取要显示的选项总数
*
* getItem :获取每一个选项,类情况而定
*
* getItemId:获取每个选项的id
*
* getView :该方法用来为每一个选项生成视图(这里为:ImageView),会多次调用
* 根据屏幕能显示多少个选项而定的,所以每次屏幕滚动时,也会调用
*
*/
public class MainActivity extends Activity {
private GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
TextView tx = (TextView) view.findViewById(R.id.textView);
Toast.makeText(MainActivity.this,tx.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
}
static class MyAdapter extends BaseAdapter{
private String[] names = {"查看","消失","金融","平安","红包","查看","消失","金融","平安","红包","查看","消失","金融","平安","红包","查看","消失","金融","平安","红包"};
private Context context;
private int[] images = {R.mipmap.a,R.mipmap.b,
R.mipmap.a,R.mipmap.b,R.mipmap.a,R.mipmap.b,
R.mipmap.a,R.mipmap.b,R.mipmap.a,R.mipmap.b,
R.mipmap.a,R.mipmap.b,R.mipmap.a,R.mipmap.b};
public MyAdapter(Context context){
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int position) {
return images[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater from = LayoutInflater.from(context);
View view = from.inflate(R.layout.item, null);
ImageView iv = (ImageView) view.findViewById(R.id.imageView);
TextView tv = (TextView) view.findViewById(R.id.textView);
iv.setImageResource(images[position]);
tv.setText(names[position]);
return view;
}
}