GridView布局及适配器优化

时间:2021-08-26 16:58:34

1.布局样式

<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DCDCDC"
android:gravity="center"
android:horizontalSpacing="1dp"
android:listSelector="@null"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="1dp" />

2.适配器对象

import android.view.View;

/**
* Author:JsonLu
* DateTime:2016/6/3 14:47
* Email:jsonlu@qq.com
* Desc:
**/
public class GridViewItem {
private int resId;
private String name;
private View.OnClickListener clickListener; public int getResId() {
return resId;
} public void setResId(int resId) {
this.resId = resId;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public View.OnClickListener getClickListener() {
return clickListener;
} public void setClickListener(View.OnClickListener clickListener) {
this.clickListener = clickListener;
}
}

3.填充子节点布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="@android:color/white"
android:gravity="center"
android:orientation="vertical"> <ImageView
android:id="@+id/grid_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/grid_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

4.适配器

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; import java.util.List; /**
* Author:JsonLu
* DateTime:2016/6/3 14:33
* Email:jsonlu@qq.com
* Desc:
**/
public abstract class GridAdapter extends BaseAdapter { public abstract List<GridViewItem> getData();
private List<GridViewItem> list;
private GridViewItem item;
private LayoutInflater layoutInflater; public GridAdapter(Context context) {
list = getData();
layoutInflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.girdview_item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.grid_icon);
TextView textView = (TextView) view.findViewById(R.id.grid_name);
item = list.get(position);
imageView.setImageDrawable(view.getContext().getResources().getDrawable(item.getResId()));
textView.setText(item.getName());
view.setOnClickListener(item.getClickListener()); }
return view;
}
}

5.使用

GridView gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new GridAdapter(this) {
@Override
public List<GridViewItem> getData() {
List<GridViewItem> list = new ArrayList<GridViewItem>();
for (int i = 0; i < 9; i++) {
GridViewItem item = new GridViewItem();
final int index = i;
item.setName("Title"+i);
item.setResId(R.drawable.ic_launcher);
item.setClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getBaseContext(), "点击"+index, Toast.LENGTH_SHORT).show();
}
});
list.add(item);
}
return list;
}
});