经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android学习-网格视图GridView
来源:cnblogs  作者:YouChaoMin  时间:2018/11/29 9:19:33  对本文有异议

一、简介:

GridView是一个以表格形式显示多张图片等组件。它是按照行列的方式来显示内容的,比如实现九宫格图,用GridView是首选。

二、代码块:

看过我上一篇博客的同学应该知道,一步一步全部步骤写出来是很费时间的,大概流程就那样,所以这次网格视图就直接上代码块了,步骤差不多。

在activity_main.xml中添加一个按钮:

  1. <Button
  2. android:id="@+id/btn_gridview"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:text="GridView"
  6. android:textAllCaps="false"/>

MainActivity在原有基础上添加:

先声明控件:

  1. private Button mBtnGridView;

在onCreate:

  1. mBtnGridView=findViewById(R.id.btn_gridview);

在setListeners:

  1. mBtnGridView.setOnClickListener(onClick);

在OnClick:

  1. case R.id.btn_gridview:
  2. //跳转到GridView演示页面
  3. intent = new Intent(MainActivity.this, GridViewActivity.class);
  4. break;

在activity_grid_view.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:padding="15dp">
  6. <GridView
  7. android:id="@+id/gv"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:numColumns="3"
  11. android:horizontalSpacing="10dp"
  12. android:verticalSpacing="10dp"/>
  13. </LinearLayout>

在GridViewActivity:

先声明控件:

  1. private GridView mGv;

在onCreate:

  1. mGv=findViewById(R.id.gv);

在之前新建MyGridViewAdapter:

  1. public class MyGridViewAdapter extends BaseAdapter {
  2. @Override
  3. public int getCount() {
  4. return 10;
  5. }
  6. @Override
  7. public Object getItem(int i) {
  8. return null;
  9. }
  10. @Override
  11. public long getItemId(int i) {
  12. return 0;
  13. }
  14. @Override
  15. public View getView(int i, View view, ViewGroup viewGroup) {
  16. return null;
  17. }
  18. }

新建layout_grid_item.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center_horizontal">
  6. <ImageView
  7. android:id="@+id/iv_grid"
  8. android:layout_width="match_parent"
  9. android:layout_height="100dp"
  10. android:scaleType="fitCenter"
  11. android:background="@color/colorPrimaryDark"/>
  12. <TextView
  13. android:id="@+id/tv_title"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:text="Hello"
  17. android:textColor="@color/colorAccent"
  18. android:gravity="center"
  19. android:layout_marginTop="10dp"
  20. />
  21. </LinearLayout>

在MyGridViewAdapter中修改:

  1. public class MyGridViewAdapter extends BaseAdapter {
  2. private Context mContext;
  3. private LayoutInflater mLayoutInflater;
  4. public MyGridViewAdapter(Context context){
  5. this.mContext=context;
  6. mLayoutInflater=LayoutInflater.from(context);
  7. }
  8. @Override
  9. public int getCount() {
  10. return 10;
  11. }
  12. @Override
  13. public Object getItem(int i) {
  14. return null;
  15. }
  16. @Override
  17. public long getItemId(int i) {
  18. return 0;
  19. }
  20. static class ViewHolder{
  21. public ImageView imageView;
  22. public TextView textView;
  23. }
  24. @Override
  25. public View getView(int i, View view, ViewGroup viewGroup) {
  26. ViewHolder holder=null;
  27. if(view==null){
  28. view=mLayoutInflater.inflate(R.layout.layout_grid_item,null);
  29. holder=new ViewHolder();
  30. holder.imageView=view.findViewById(R.id.iv_grid);
  31. holder.textView=view.findViewById(R.id.tv_title);
  32. view.setTag(holder);
  33. }else {
  34. holder=(ViewHolder)view.getTag();
  35. }
  36. //赋值
  37. holder.textView.setText("ycm");
  38. Glide.with(mContext).load("http://i1.bvimg.com/670191/a72f2a8c0f289d48s.png").into(holder.imageView);
  39. return view;
  40. }
  41. }

在GridViewActivity中添加:

  1. mGv.setAdapter(new MyGridViewAdapter(GridViewActivity.this));

三、运行截图:

GridView
设置点击和长按事件之前的ListView中介绍过了,就不介绍了。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号