码迷,mamicode.com
首页 > 其他好文 > 详细

4.GridView

时间:2015-10-29 14:45:32      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:

GridView
和listview用法相似
  1. <GridView
  2. android:id="@+id/gv_home"
  3. android:layout_width="match_parent"
  4. android:layout_height="0dp"
  5. android:layout_weight="1"
  6. android:numColumns="3" //有三列
  7. android:verticalSpacing="20dp" >//垂直线间距离
  8. </GridView>
子项布局home_list_item.xml
  1. <ImageView
  2. android:id="@+id/iv_item"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:src="@drawable/home_apps" />
  6. <TextView
  7. android:id="@+id/tv_item"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_marginTop="5dp"
  11. android:textColor="@color/black"
  12. android:textSize="18sp"
  13. android:text="手机防盗" />
  1. /**
  2. * 主页面
  3. *
  4. * @author Kevin
  5. *
  6. */
  7. public class HomeActivity extends Activity {
  8. private GridView gvHome;
  9. private String[] mItems = new String[] { "手机防盗", "通讯卫士", "软件管理", "进程管理",
  10. "流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心" };
  11. private int[] mPics = new int[] { R.drawable.home_safe,
  12. R.drawable.home_callmsgsafe, R.drawable.home_apps,
  13. R.drawable.home_taskmanager, R.drawable.home_netmanager,
  14. R.drawable.home_trojan, R.drawable.home_sysoptimize,
  15. R.drawable.home_tools, R.drawable.home_settings };
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_home);
  20. gvHome = (GridView) findViewById(R.id.gv_home);
  21. gvHome.setAdapter(new HomeAdapter());
  22. }
  23. class HomeAdapter extends BaseAdapter {
  24. @Override
  25. public int getCount() {
  26. return mItems.length;//9个列表
  27. }
  28. @Override
  29. public Object getItem(int position) {
  30. return mItems[position];//返回null也没事
  31. }
  32. @Override
  33. public long getItemId(int position) {
  34. return position;
  35. }
  36. @Override
  37. public View getView(int position, View convertView, ViewGroup parent) {
  38. View view = View.inflate(HomeActivity.this,
  39. R.layout.home_list_item, null);//将字布局加载进来
  40. ImageView ivItem = (ImageView) view.findViewById(R.id.iv_item);
  41. TextView tvItem = (TextView) view.findViewById(R.id.tv_item);
  42. //getview会被调用9次
  43. tvItem.setText(mItems[position]);
  44. ivItem.setImageResource(mPics[position]);
  45. return view;
  46. }
  47. }
    1. // 设置监听
    2. gvHome.setOnItemClickListener(new OnItemClickListener() {
    3. @Override
    4. public void onItemClick(AdapterView<?> parent, View view,
    5. int position, long id) {
    6. switch (position) {
    7. case 0:
    8. // 手机防盗
    9. showPasswordDialog();
    10. break;
    11. case 8:
    12. // 设置中心
    13. startActivity(new Intent(HomeActivity.this,
    14. SettingActivity.class));
    15. break;
    16. default:
    17. break;
    18. }
    19. }
    20. });

  48. }






4.GridView

标签:

原文地址:http://www.cnblogs.com/liuyu0529/p/4920438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!