Android使用GridView实现表格分割线效果
作者:冰糖葫芦三剑客 时间:2022-05-12 06:59:54
使用GridView实现表格分割线效果,网格布局表格布局也是可以实现的。
效果如下:
1.主函数代码:
package com.example.qd.douyinwu;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 自定义GridLayout 表格实现系列
* <p>
* https://github.com/li504799868/GridLayoutView
* https://github.com/Eason90/GridBuilder
* <p>
* https://github.com/wimsonevel/AndroidGridLayout
* <p>
* https://blog.csdn.net/swust_chenpeng/article/details/37873215
* tableLayout 实现类似gridview的效果 带分割线
* <p>
* <p>
* https://github.com/LRH1993/AutoFlowLayout 网格布局实现
* https://github.com/dolphinwang/GridLayout
* https://blog.csdn.net/aminy123/article/details/69053339 频道管理
*/
public class SGridViewAcivity extends Activity {
private View view = null;
private GridView gridView;
private List<Map<String, Object>> data_list;
private SimpleAdapter sim_adapter;
private Context mContext;
// 图片封装为一个数组
private int[] icon = {R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
R.drawable.jz_backward_icon, R.drawable.jz_backward_icon, R.drawable.ic_launcher_background,
R.drawable.jz_backward_icon, R.drawable.jz_backward_icon, R.drawable.ic_launcher_background,
R.drawable.jz_backward_icon, R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
R.drawable.jz_backward_icon};
private String[] iconName = {"通讯录", "日历", "照相机", "时钟", "游戏", "短信", "铃声",
"设置", "语音", "天气", "浏览器", "视频"};
private String[] iconNames = {"", "篮球", "击剑", "保龄球", "排球","台球",
"中国", "666", "688", "999", "888","988",
"意大利", "122", "222", "112","388","321",
"法国", "322", "200", "100", "210","188",
"韩国", "101", "120", "142", "234", "532"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gv);
gridView = (GridView) findViewById(R.id.gridView2);
//新建List
data_list = new ArrayList<Map<String, Object>>();
//获取数据
getData();
//新建适配器
String[] from = {"image", "text"};
int[] to = {R.id.image, R.id.text};
gridView.setAdapter(new ImageAdapter(SGridViewAcivity.this));
// sim_adapter = new SimpleAdapter(this, data_list, R.layout.item, from, to);
//配置适配器
// gridView.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;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.jz_backward_icon, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume,
R.drawable.jz_add_volume, R.drawable.jz_add_volume
};
private class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
this.mContext = context;
}
@Override
public int getCount() {
return iconNames.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
viewHolder = new ViewHolder();
// viewHolder.itemImg = (ImageView) convertView.findViewById(R.id.iv_head);
viewHolder.mText = (TextView) convertView.findViewById(R.id.text);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// 这里只是模拟,实际开发可能需要加载网络图片,可以使用ImageLoader这样的图片加载框架来异步加载图片
// imageLoader.displayImage("drawable://" + mThumbIds[position], viewHolder.itemImg);
viewHolder.mText.setText(iconNames[position]);
return convertView;
}
class ViewHolder {
ImageView itemImg;
TextView mText;
}
}
}
2.主函数布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
<GridView
android:id="@+id/gridView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="#999"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
android:padding="1dp"
android:numColumns="6" />
</LinearLayout>
ListView设置分割线的话设置:
android:divider
android:dividerHeight
a、设置GridView背景色。
b、设置水平和竖直方向间隔:android:horizontalSpacing和android:verticalSpacing。
c、设置GridView的item的背景色及其选中后的颜色。
3.适配器布局:
<?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:orientation="vertical"
android:background="@drawable/gv_one_selector"
>
<TextView
android:id="@+id/text"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="39dp"
android:textSize="13sp"
android:padding="0dp"
android:text="TextView" />
</LinearLayout>
4.背景选择器gv_one_selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--点击背景选择器-->
<item
android:state_selected="true" >
<shape android:shape="rectangle">
<solid
android:color="#CCCCCC"
/>
</shape>
</item>
<item
android:state_pressed="true" >
<shape android:shape="rectangle">
<solid
android:color="#CCCCCC"
/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid
android:color="#FFFFFF"
/>
</shape>
</item>
</selector>
参考选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--表格分割线背景效果-->
<item
android:state_selected="true" >
<shape android:shape="rectangle">
<solid
android:color="#CCCCCC"
/>
<stroke android:width="1.0px" android:color="#999999" />
</shape>
</item>
<item
android:state_pressed="true" >
<shape android:shape="rectangle">
<solid
android:color="#CCCCCC"
/>
<stroke android:width="1.0px" android:color="#999999" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<stroke android:width="1.0px" android:color="#999999" />
</shape>
</item>
</selector>
以上是全部代码。
来源:https://blog.csdn.net/shenggaofei/article/details/105775324
标签:Android,GridView,分割线
0
投稿
猜你喜欢
WPF模拟实现Gitee泡泡菜单的示例代码
2023-09-19 00:53:16
SpringBoot读取资源目录中JSON文件的方法实例
2023-04-26 02:00:42
SpringBoot 整合mybatis+mybatis-plus的详细步骤
2022-09-29 21:06:10
解决Weblogic部署war找不到spring配置文件的问题
2022-12-29 07:03:08
SpringBoot下的值注入(推荐)
2023-04-01 04:45:01
深入理解Java设计模式之抽象工厂模式
2023-11-28 12:11:34
Spring Data JPA调用存储过程实例代码
2023-11-25 00:13:15
Java中final修饰的方法是否可以被重写示例详解
2022-02-25 13:35:40
详解C#正则表达式Regex常用匹配
2022-07-08 08:04:45
深入理解C#中new、override、virtual关键字的区别
2021-08-29 05:42:37
springMVC+ajax实现文件上传且带进度条实例
2022-01-15 16:42:36
SpringBoot创建WebService方法详解
2022-02-08 10:29:31
Java实现堆排序(大根堆)的示例代码
2023-09-13 15:24:01
Android利用LitePal操作数据库存取图片
2021-06-14 14:57:45
20个非常实用的Java程序代码片段
2022-02-08 10:35:44
深入理解C#窗体关闭事件
2023-06-01 14:38:56
java实现上传网络图片到微信临时素材
2022-09-21 08:43:23
JAVA SPI特性及简单应用代码实例
2021-11-11 14:54:54
Java裁剪压缩PNG图片,透明背景色变黑的解决方案
2023-11-25 13:21:27
mybatis水平分表实现动态表名的项目实例
2023-04-02 22:11:42