Android控件gridview实现单行多列横向滚动效果
程序员文章站
2022-05-16 12:30:37
本文实例为大家分享了安卓实现单行多列横向滚动,供大家参考,具体内容如下
本文实例为大家分享了安卓实现单行多列横向滚动,供大家参考,具体内容如下
<gridlayout android:layout_width="match_parent" android:layout_height="match_parent" android:columncount="1" > <horizontalscrollview android:layout_width="match_parent" android:layout_height="88dp" android:layout_column="0" android:layout_gravity="left|bottom" android:layout_row="0" > <linearlayout android:id="@+id/linearlayout_gridtablelayout" android:layout_width="1000dp" android:layout_height="79dp" android:orientation="horizontal" > <gridview android:id="@+id/gridview1" android:layout_width="600dp" android:layout_height="match_parent" android:background="#ffffff" android:columnwidth="100dp" android:gravity="center" android:horizontalspacing="1dp" android:numcolumns="6" android:scrollbaralwaysdrawhorizontaltrack="true" android:scrollbaralwaysdrawverticaltrack="true" android:scrollbars="horizontal|vertical" android:verticalspacing="1dp" /> </linearlayout> </horizontalscrollview> </gridlayout>
指定其中linearlayout的宽度就能够实现你gridview的长宽变化,如果它的长超过屏幕,则自动添加水平滚动条。
private int[] icon = { r.drawable.camera, r.drawable.wifi, r.drawable.temperature, r.drawable.lamp, r.drawable.wechat, r.drawable.mic, }; private string[] iconname = { "摄像头", "网络", "温湿度", "电器", "微信", "语音" }; private gridview gview; private list<map<string, object>> data_list; private simpleadapter sim_adapter; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //gridview数据适配器 gview = (gridview) findviewbyid(r.id.gridview1); //新建list data_list = new arraylist<map<string, object>>(); //获取数据 getdata(); //新建适配器 string [] from ={"image","text"}; int [] to = {r.id.image,r.id.title}; sim_adapter = new simpleadapter(this, data_list, r.layout.items, from, to); //配置适配器 gview.setadapter(sim_adapter); } public list<map<string, object>> getdata(){ //cion和iconname的长度是相同的,这里任选其一都可以 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]); data_list.add(map); } return data_list; }
fragment里:
oncreate里边初始化数据,oncreateview里边初始化gridview和绑定适配器
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // todo auto-generated method stub view view = inflater.inflate(r.layout.gridview,container, false); gridview = (gridview) view.findviewbyid(r.id.gridview1); //新建list data_list = new arraylist<map<string, object>>(); //http://www.cnblogs.com/xiaobo-linux/ qq463431476 //获取数据 getdata(); //新建适配器 string [] from ={"image","text"}; int [] to = {r.id.image,r.id.title}; sim_adapter = new simpleadapter(getactivity(), data_list, r.layout.items, from, to); //配置适配器 gridview.setadapter(sim_adapter); return view; }
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } // http://www.cnblogs.com/xiaobo-linux/ qq463431476 public list<map<string, object>> getdata(){ //cion和iconname的长度是相同的,这里任选其一都可以 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]); data_list.add(map); } return data_list; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。