Adapter类型控件之GirdView(网格布局)

(一)概述

(二)相关属性
这里写图片描述
Adapter类型控件之GirdView(网格布局)_第1张图片
(三)使用案例
这里写图片描述
实现效果图:
Adapter类型控件之GirdView(网格布局)_第2张图片
代码实现:
GirdView中items.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp">

    <ImageView  android:id="@+id/img_icon" android:layout_width="64dp" android:layout_height="64dp" android:layout_centerInParent="true" android:src="@drawable/address_book" />

    <TextView  android:id="@+id/txt_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/img_icon" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:text="哈哈" android:textSize="18sp" />


</RelativeLayout>

这里写图片描述

public class Icon {
    private int iId;
    private String iName;

    public Icon() {
    }

    public Icon(int iId, String iName) {
        this.iId = iId;
        this.iName = iName;
    }

    public int getiId() {
        return iId;
    }

    public String getiName() {
        return iName;
    }

    public void setiId(int iId) {
        this.iId = iId;
    }

    public void setiName(String iName) {
        this.iName = iName;
    }
}

MainActivity的布局文件跟java代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" tools:context=".MainActivity">

    <!--numColumns设置每行显示多少个-->
    <GridView  android:id="@+id/grid_photo" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="3" />

</RelativeLayout>
public class MainActivity extends Activity {

    private Context mContext;
    private GridView grid_photo;
    private BaseAdapter mAdapter = null;
    private ArrayList<Icon> mData = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        grid_photo = (GridView) findViewById(R.id.grid_photo);

        mData = new ArrayList<Icon>();
        mData.add(new Icon(R.drawable.calendar, "图标1"));
        mData.add(new Icon(R.drawable.camera, "图标2"));
        mData.add(new Icon(R.drawable.clock, "图标3"));
        mData.add(new Icon(R.drawable.games_control, "图标4"));
        mData.add(new Icon(R.drawable.messenger, "图标5"));
        mData.add(new Icon(R.drawable.ringtone, "图标6"));
        mData.add(new Icon(R.drawable.settings, "图标7"));

        mAdapter = new MyAdapter<Icon>(mData, R.layout.item_grid_icon) {
            @Override
            public void bindView(ViewHolder holder, Icon obj) {
                holder.setImageResource(R.id.img_icon, obj.getiId());
                holder.setText(R.id.txt_icon, obj.getiName());
            }
        };

        grid_photo.setAdapter(mAdapter);

        grid_photo
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        Toast.makeText(mContext, "你点击了~" + position + "~项",
                                Toast.LENGTH_SHORT).show();
                    }
                });

    }

}

这里写图片描述
这里写图片描述
<完>

你可能感兴趣的:(Adapter,布局,网格,GirdView)