Android -- GridView

本文通过一个实例详细介绍了Android中GridView的使用,包括在main.xml中定义一个4列的GridView,创建cell.xml文件作为单元格模板,以及在MainActivity.java中使用SimpleAdapter填充数据并设置点击监听。当用户点击GridView中的图片时,主屏幕下方的ImageView会显示所选图片。
摘要由CSDN通过智能技术生成

下面直接通过一个例子讲解GridView的用法。

mian.xml文件:

<LinearLayout 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:orientation="vertical"
    >
    <!--定义一个4列的GridView控件  -->
    <GridView
        android:id="@+id/gridView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:numColumns="4" >
    </GridView>
    <!-- 定义一个ImageView -->
    <ImageView
        android:id="@+id/main_im"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/bomb1"
        android:layout_weight="1.5" />
</LinearLayout>

在定义一个cell.xml文件,里面只有一个ImageView。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/p_w_picpathView1"
        android:layout_width="50dip"
        android:layout_height="50dip"
        />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    /**
     * @author shao
     *使用SimpleAdapter为GridView控件添加内容
     */
    private GridView gd;
    private ImageView p_w_picpathView;
    private SimpleAdapter simpleAdapter;
    private int[] p_w_picpathId = { R.drawable.bomb1, R.drawable.bomb2,
            R.drawable.bomb3, R.drawable.bomb4, R.drawable.bomb5,
            R.drawable.bomb6, R.drawable.bomb7, R.drawable.bomb8,
            R.drawable.bomb9, R.drawable.bomb10, R.drawable.bomb11,
            R.drawable.bomb12};
    private List<Map<String, Object>> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //查找控件
        gd = (GridView) findViewById(R.id.gridView1);
        p_w_picpathView = (ImageView) findViewById(R.id.main_im);    
        //为list添加内容
        list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < p_w_picpathId.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("p_w_picpath", p_w_picpathId[i]);
            list.add(map);
        }
        simpleAdapter = new SimpleAdapter(this, list, R.layout.cell,
                new String[] { "p_w_picpath" }, new int[] { R.id.p_w_picpathView1 });
        //为GridView控件添加Adapter
        gd.setAdapter(simpleAdapter);
        //为GridView控件添加监听
        gd.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                p_w_picpathView.setImageResource(p_w_picpathId[position]);
            }
        });
    }
}

运行效果,当点击不同过得图片时,屏幕的下方将显示相应的图片

https://blog.51cto.com/shaotao/1281314 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值