Android程序:GridView的应用(仿桌面图标排列并设置点击事件)

实现效果:
GridView的应用(仿桌面图标排列并设置点击事件)

MainActivity :

public class MainActivity extends Activity implements OnItemClickListener {

    private GridView gridView;
    private List<Map<String, Object>> dataList;
    private int[] icon = { R.drawable.address_book, R.drawable.calendar,
            R.drawable.camera, R.drawable.clock, R.drawable.games_control,
            R.drawable.messenger, R.drawable.ringtone, R.drawable.settings,
            R.drawable.speech_balloon, R.drawable.weather, R.drawable.world,
            R.drawable.youtube };
    private String[] iconName = { "通讯录", "日历", "相机", "时钟", "游戏", "短信", "铃声",
            "设置", "语音", "天气", "浏览器", "视频" };
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gridView = (GridView) findViewById(R.id.gridView);
        // 1.准备数据源

        dataList = new ArrayList<Map<String, Object>>();
        // 2.新建适配器(SimpleAdapter)
        // SimpleAdapter(context, data, resource, from, to)
        // SimpleAdapter(上下文(this), 数据源, 布局文件, 自己取的名字要与后面的一一对应, 对应布局中的id)
        simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item,
                new String[] { "image", "text" }, new int[] { R.id.imageView,
                        R.id.textView });
        // 3.GridView加载适配器
        gridView.setAdapter(simpleAdapter);

        // 4.GridView配置事件监听器(OnItemClickListener)
        gridView.setOnItemClickListener(this);


    }

    private List<Map<String, Object>> getData() {
        //填充数据源
        for(int i =0;i<icon.length;i++){
            Map<String, Object>map=new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            dataList.add(map);
        }

        return dataList;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        //String s=gridView.getItemAtPosition(position)+"";
        Toast.makeText(this, "我是"+iconName[position],Toast.LENGTH_SHORT).show();
    }

}

main.xml:

<?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" >

    <!--
        android:numColumns="3"  每行显示几列
        android:horizontalSpacing="" 两列之间的间距
        android:verticalSpacing="" 两行之间的间距

    -->

    <GridView
        android:layout_marginTop="50dp"
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="10dp"
        android:numColumns="3"
        android:verticalSpacing="10dp" >
    </GridView>

</LinearLayout>

item.xml:

<?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"
    android:gravity="center"
    android:background="#000000" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="60dp"
        android:layout_height="60dp" 
        android:src="@drawable/address_book"/>

    <TextView
        android:layout_marginTop="5dp"
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textColor="#ffffff"
        android:text="hellp"/>

</LinearLayout>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值