1.创建Web类

public class Web {
private String name;
private String url;
private int resource;

public Web(String name, String url, int resource) {

this.name = name;
this.url = url;
this.resource = resource;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setUrl(String url) {
this.url = url;
}

public String getUrl() {
return url;
}

public void setResource(int resource) {
this.resource = resource;
}

public int getResource() {
return resource;
}

}

2.创建itemView继承RelativeLayout ,名称为ResourcePanelWebItemView.java

public class ResourcePanelWebItemView extends RelativeLayout {

public ResourcePanelWebItemView(Context context) {
super(context);
}

public ResourcePanelWebItemView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public ResourcePanelWebItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onFinishInflate() {
super.onFinishInflate();

}

}

3.创建ArrayDaapter, 名称为ResourcePanelWebListAdapter.java,内容如下:

public class ResourcePanelWebListAdapter extends ArrayAdapter<Web> {
Context mContext;
LayoutInflater infalter;
private ResourcePanelWebItemView view = null;
public ResourcePanelWebListAdapter(Context context, List<Web> weblist) {
super(context, 0, weblist);
mContext = context;
infalter = LayoutInflater.from(mContext);
System.gc();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Web model =  getItem(position);
if (convertView == null || convertView.getTag() != model) {
final ResourcePanelWebItemView view1 = (ResourcePanelWebItemView) infalter.inflate(
R.layout.gridviewitem, null);
view = view1;
//view1.setBackgroundColor(Color.WHITE);
view1.setBackgroundResource(R.drawable.overview_item_normal);
ImageView mImageView= (ImageView)view1.findViewById(R.id.GridViewImage);
mImageView.setBackgroundColor(Color.WHITE);
//mImageView.setImageResource(model.getResource());
TextView mTextView= (TextView)view1.findViewById(R.id.webTitle);
mTextView.setText(model.getName());
//1.得到图片
//2.得到文字
} else {
view = (ResourcePanelWebItemView) convertView;
}
view.setTag(model);

return view;
}

}

4.PanelGridView.java

public class PanelGridView extends GridView implements OnItemClickListener {

public PanelGridView(Context context) {
super(context);
init();
}

public PanelGridView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public PanelGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init(){
//this.setNumColumns(1);
}
private Listener mListener;

public void onItemClick(AdapterView parent, View v, int position, long id) {
if (mListener != null) {
mListener.onClick(position);
}
}

public void setListener(Listener l) {
mListener = l;
}

public interface Listener {
void onClick(int position);
}
boolean cancelIntercept;
public boolean isCancelIntercept() {
return cancelIntercept;
}

public void setCancelIntercept(boolean cancelIntercept) {
this.cancelIntercept = cancelIntercept;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean b = super.onInterceptTouchEvent(ev);
b&=!isCancelIntercept();
return b;
}
public int columnNum = 1;

@Override
public void setNumColumns(int numColumns) {
super.setNumColumns(numColumns);
columnNum = numColumns;
}
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(mOnConfigurationChangedListner!=null){
mOnConfigurationChangedListner.setColumn();
}
}
OnConfigurationChangedListner mOnConfigurationChangedListner;
public void setOnConfigurationChangedListner(OnConfigurationChangedListner l){
mOnConfigurationChangedListner = l;
}
public static interface OnConfigurationChangedListner{
void setColumn();
}

}

 

5.gridviewpanel.xml布局,使用自定义的控件

<com.jieless.xnote.panel.PanelGridView
android:id=”@+id/list” android:layout_gravity=”center”
android:layout_alignParentTop=”true” android:padding=”0px”
android:gravity=”center_vertical” android:layout_centerVertical=”true”
android:layout_width=”match_parent” android:layout_height=”match_parent”>
</com.jieless.xnote.panel.PanelGridView>

6.gridviewitem.xml

<ImageView android:id=”@+id/GridViewImage”
android:layout_centerInParent=”true” android:layout_width=”100px”
android:layout_height=”100px” android:layout_marginLeft=”0px”
android:layout_marginRight=”0px” android:layout_marginTop=”0px”
android:layout_marginBottom=”0px”>
</ImageView>

<TextView android:id=”@+id/webTitle” android:layout_width=”fill_parent”
android:text=”hi” android:textColor=”@android:color/black”
android:textSize=”18dip” android:textStyle=”bold”
android:layout_height=”wrap_content” android:gravity=”center”
android:layout_gravity=”center” android:layout_centerInParent=”true”>

</TextView>

7.调用使用方法

mGrid = (PanelGridView) findViewById(R.id.list);
if (mGrid != null) {
mGrid.setNumColumns(GridView.AUTO_FIT);
mGrid.setColumnWidth(100);
mGrid.setGravity(Gravity.CENTER);
mGrid.setVerticalSpacing(10);
mGrid.setHorizontalSpacing(10);
getList();
mResourcePanelWebListAdapter = new ResourcePanelWebListAdapter(
getContext(), weblist);
mGrid.setAdapter(mResourcePanelWebListAdapter);

mGrid
.setOnConfigurationChangedListner(new OnConfigurationChangedListner() {
@Override
public void setColumn() {
resetColumn();
}

});

 

}

8. 附加一个resetColumn方法

public void resetColumn() {
if (mGrid == null)
return;

int columnNum = getColumNum(XNoteWebObject.MIN_WIDTH);
if (columnNum != mGrid.columnNum) {
mGrid.setNumColumns(columnNum);
mGrid.requestLayout();
}
}

9.getList()代码

public static List<Web> getList() {
List<Web> webs = new ArrayList<Web>();

Web web1 = new Web(“输入网址开始浏览”, “”, R.drawable.handwriting_background_1);
webs.add(web1);
web1 = null;

Web web2 = new Web(“百度搜索”, “http://baidu.com”,
R.drawable.handwriting_background_1);
webs.add(web2);
web2 = null;

Web web3 = new Web(“Google搜索”, “http://google.com”,
R.drawable.handwriting_background_1);
webs.add(web3);
web3 = null;

Web web4 = new Web(“维基百科”, “http://www.wikipedia.org”,
R.drawable.handwriting_background_1);
webs.add(web4);
web4 = null;

Web web5 = new Web(“百度百科”, “http://baike.baidu.com”,
R.drawable.handwriting_background_1);
webs.add(web5);
web5 = null;

Web web6 = new Web(“搜搜百科”, “http://baike.soso.com”,
R.drawable.handwriting_background_1);
webs.add(web6);
web6 = null;
return webs;
}

10.最终的效果

gridview