Android控件gridview实现单行多列横向滚动效果
作者:Xurns 时间:2022-09-11 10:53:49
本文实例为大家分享了安卓实现单行多列横向滚动,供大家参考,具体内容如下
<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;
}
来源:https://blog.csdn.net/Xurns/article/details/51535377
标签:Android,gridview,滚动
0
投稿
猜你喜欢
C# NAudio 库的各种常见使用方式之播放 录制 转码 音频可视化
2023-06-20 04:14:16
阿里面试Nacos配置中心交互模型是push还是pull原理解析
2023-04-09 06:40:54
android实现通过NFC读取卡号
2023-06-24 08:45:54
C#私有构造函数使用示例
2021-12-31 02:14:53
c#中设置快捷键
2021-05-31 20:14:29
C#实现图书管理系统
2023-03-24 04:30:35
一文搞懂Java中的注解和反射
2022-04-04 20:55:31
C#基础入门之值类型和引用类型的区别详析
2022-02-22 00:14:04
spring security获取用户信息的实现代码
2022-09-29 01:59:03
C# wpf Grid中实现控件拖动调整大小的示例代码
2023-05-15 17:12:03
Android WorkManager浅谈
2023-03-24 11:26:46
C语言二叉树常见操作详解【前序,中序,后序,层次遍历及非递归查找,统计个数,比较,求深度】
2021-09-30 08:41:23
springboot整合quartz项目使用案例
2023-02-13 19:57:12
MyBatis中XML 映射文件中常见的标签说明
2023-01-07 08:02:59
C#制作简单的多人在线即时交流聊天室
2023-02-23 12:12:23
解决Android使用Handler造成内存泄露问题
2022-11-10 09:07:13
android使用ExpandableListView控件实现小说目录效果的例子
2023-04-14 22:10:27
java组件SmartUpload和FileUpload实现文件上传功能
2022-02-02 17:08:17
java实现导出Excel的功能
2022-07-17 15:57:28
Android PopupWindow增加半透明蒙层
2021-12-30 04:12:09