gridview是一个以表格形式显示多张图片等组件。它是按照行列的方式来显示内容的,比如实现九宫格图,用gridview是首选。
看过我上一篇博客的同学应该知道,一步一步全部步骤写出来是很费时间的,大概流程就那样,所以这次网格视图就直接上代码块了,步骤差不多。
<button android:id="@+id/btn_gridview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="gridview" android:textallcaps="false"/>
private button mbtngridview;
mbtngridview=findviewbyid(r.id.btn_gridview);
mbtngridview.setonclicklistener(onclick);
case r.id.btn_gridview: //跳转到gridview演示页面 intent = new intent(mainactivity.this, gridviewactivity.class); break;
<?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:padding="15dp"> <gridview android:id="@+id/gv" android:layout_width="match_parent" android:layout_height="wrap_content" android:numcolumns="3" android:horizontalspacing="10dp" android:verticalspacing="10dp"/> </linearlayout>
private gridview mgv;
mgv=findviewbyid(r.id.gv);
public class mygridviewadapter extends baseadapter { @override public int getcount() { return 10; } @override public object getitem(int i) { return null; } @override public long getitemid(int i) { return 0; } @override public view getview(int i, view view, viewgroup viewgroup) { return null; } }
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <imageview android:id="@+id/iv_grid" android:layout_width="match_parent" android:layout_height="100dp" android:scaletype="fitcenter" android:background="@color/colorprimarydark"/> <textview android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="hello" android:textcolor="@color/coloraccent" android:gravity="center" android:layout_margintop="10dp" /> </linearlayout>
public class mygridviewadapter extends baseadapter { private context mcontext; private layoutinflater mlayoutinflater; public mygridviewadapter(context context){ this.mcontext=context; mlayoutinflater=layoutinflater.from(context); } @override public int getcount() { return 10; } @override public object getitem(int i) { return null; } @override public long getitemid(int i) { return 0; } static class viewholder{ public imageview imageview; public textview textview; } @override public view getview(int i, view view, viewgroup viewgroup) { viewholder holder=null; if(view==null){ view=mlayoutinflater.inflate(r.layout.layout_grid_item,null); holder=new viewholder(); holder.imageview=view.findviewbyid(r.id.iv_grid); holder.textview=view.findviewbyid(r.id.tv_title); view.settag(holder); }else { holder=(viewholder)view.gettag(); } //赋值 holder.textview.settext("ycm"); glide.with(mcontext).load("http://i1.bvimg.com/670191/a72f2a8c0f289d48s.png").into(holder.imageview); return view; } }
mgv.setadapter(new mygridviewadapter(gridviewactivity.this));
设置点击和长按事件之前的listview中介绍过了,就不介绍了。
如对本文有疑问, 点击进行留言回复!!
荐 面试官:kafka日志段如何读写都不知道,好意思说精通Kafka吗?
荐 如何写出一份“有理有据使人信服”的Android项目设计文档
荐 灵魂一问,Android中有代替HashMap的方法吗?
intent.setFlag的参数 FLAG_ACTIVITY_CLEAR_TASK 说明
网友评论