Android使用GridView實(shí)現(xiàn)表格分割線效果
使用GridView實(shí)現(xiàn)表格分割線效果,網(wǎng)格布局表格布局也是可以實(shí)現(xiàn)的。
效果如下:

1.主函數(shù)代碼:
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 表格實(shí)現(xiàn)系列
* <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 實(shí)現(xiàn)類似gridview的效果 帶分割線
* <p>
* <p>
* https://github.com/LRH1993/AutoFlowLayout 網(wǎng)格布局實(shí)現(xiàn)
* 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;
// 圖片封裝為一個(gè)數(shù)組
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 = {"通訊錄", "日歷", "照相機(jī)", "時(shí)鐘", "游戲", "短信", "鈴聲",
"設(shè)置", "語音", "天氣", "瀏覽器", "視頻"};
private String[] iconNames = {"", "籃球", "擊劍", "保齡球", "排球","臺(tái)球",
"中國(guó)", "666", "688", "999", "888","988",
"意大利", "122", "222", "112","388","321",
"法國(guó)", "322", "200", "100", "210","188",
"韓國(guó)", "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>>();
//獲取數(shù)據(jù)
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的長(zhǎng)度是相同的,這里任選其一都可以
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();
}
// 這里只是模擬,實(shí)際開發(fā)可能需要加載網(wǎng)絡(luò)圖片,可以使用ImageLoader這樣的圖片加載框架來異步加載圖片
// imageLoader.displayImage("drawable://" + mThumbIds[position], viewHolder.itemImg);
viewHolder.mText.setText(iconNames[position]);
return convertView;
}
class ViewHolder {
ImageView itemImg;
TextView mText;
}
}
}
2.主函數(shù)布局:
<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設(shè)置分割線的話設(shè)置:
android:divider
android:dividerHeight
a、設(shè)置GridView背景色。
b、設(shè)置水平和豎直方向間隔:android:horizontalSpacing和android:verticalSpacing。
c、設(shè)置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"> <!--點(diǎn)擊背景選擇器--> <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>
以上是全部代碼。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android?Spinner和GridView組件的使用示例
- Android 控件GridView使用案例講解
- Android自定義gridView仿頭條頻道拖動(dòng)管理功能
- Android開發(fā)之組件GridView簡(jiǎn)單使用方法示例
- Android中GridView插件的使用方法
- Android控件gridview實(shí)現(xiàn)單行多列橫向滾動(dòng)效果
- Android通過實(shí)現(xiàn)GridView的橫向滾動(dòng)實(shí)現(xiàn)仿京東秒殺效果
- Android使用Gridview單行橫向滾動(dòng)顯示
- Android Gridview布局出現(xiàn)滾動(dòng)條或組件沖突解決方法
相關(guān)文章
Android實(shí)現(xiàn)文字和圖片混排(文字環(huán)繞圖片)效果
這篇文章主要介紹了Android實(shí)現(xiàn)文字和圖片混排的方法,實(shí)例分析了文字環(huán)繞圖片效果的具體功能顯示及頁面布局實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android的HTTP操作庫(kù)Volley的基本使用教程
這篇文章主要介紹了Android的HTTP操作庫(kù)Volley的基本使用教程,包括JSON請(qǐng)求與圖片加載等用法的實(shí)例,需要的朋友可以參考下2016-05-05
基于Android開發(fā)支持表情的實(shí)現(xiàn)詳解
本篇文章是對(duì)在Android開發(fā)中支持表情的實(shí)現(xiàn)代碼進(jìn)行了介紹。需要的朋友參考下2013-05-05
Android studio 實(shí)現(xiàn)隨機(jī)位置畫10個(gè)隨機(jī)大小的五角星的代碼
這篇文章主要介紹了Android studio 實(shí)現(xiàn)隨機(jī)位置畫10個(gè)隨機(jī)大小的五角星,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Android Studio和阿里云數(shù)據(jù)庫(kù)實(shí)現(xiàn)一個(gè)遠(yuǎn)程聊天程序
本文主要介紹了Android Studio和阿里云數(shù)據(jù)庫(kù)實(shí)現(xiàn)一個(gè)遠(yuǎn)程聊天程序,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
解析Android開發(fā)中多點(diǎn)觸摸的實(shí)現(xiàn)方法
多點(diǎn)觸摸(MultiTouch),指的是允許計(jì)算機(jī)用戶同時(shí)通過多個(gè)手指來控制圖形界面的一種技術(shù)。與多點(diǎn)觸摸技術(shù)相對(duì)應(yīng)的就是單點(diǎn)觸摸,單點(diǎn)觸摸的設(shè)備已經(jīng)有很多年了,小尺寸的有觸摸式的手機(jī),大尺寸的最常見的就是銀行里的ATM機(jī)和排隊(duì)查詢機(jī)等等2013-05-05
monkeyrunner環(huán)境搭建及實(shí)例教程(3)
這篇文章主要為大家詳細(xì)介紹了monkeyrunner環(huán)境搭建及實(shí)例教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android使用post方式上傳圖片到服務(wù)器的方法
這篇文章主要介紹了Android使用post方式上傳圖片到服務(wù)器的方法,結(jié)合實(shí)例形式分析了Android文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2016-03-03
Android中PopuWindow實(shí)現(xiàn)下拉列表實(shí)例
本篇文章主要介紹了Android中PopuWindow實(shí)現(xiàn)下拉列表實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07

