MainActivity.java
package com.yqy.yqy_gridviewtest;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView gridview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridView1);
initGridView();
}
private void initGridView() {
// 生成动态数组,并且转入数据
ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 9; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(“tv”, i+1);// 按序号做img
lstImageItem.add(map);
}
SimpleAdapter sa = new SimpleAdapter(this, lstImageItem,
R.layout.item_gridview, new String[] { “tv” },
new int[] { R.id.tv
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享
});
gridview.setAdapter(sa);
gridview.setOnItemClickListener(new ItemClickListener());
}
class ItemClickListener implements OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
@SuppressWarnings(“unchecked”)
HashMap<String, Object> item = (HashMap<String, Object>) arg0
.getItemAtPosition(arg2);
int index = (Integer) item.get(“tv”);
Toast.makeText(MainActivity.this, “点击:”+index, Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
item_gridview.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” >
<TextView
最后
给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了
最后
给大家送上我成功跳槽复习中所整理的资料,由于文章篇幅有限,所以只是把题目列出来了
[外链图片转存中…(img-xnzusznF-1640663847716)]
[外链图片转存中…(img-QvTfQJS4-1640663847735)]
[外链图片转存中…(img-qYKW8wd2-1640663847736)]