1. 程式人生 > >Android使用RecyclerView繪製表格

Android使用RecyclerView繪製表格

一、效果圖

效果圖

二、建立步驟:

思路:其實就是將MainActivity中寫好表頭佈局,同時和RecyclerView的Item佈局保持一致,這裡面列之間使用View進行分割,邊框使用layer-list和shape實現。

1:MainActivity程式碼:

public class MainActivity extends AppCompatActivity {
    private RecyclerView rv_sheet;
    private List<entity> list;
    private SheetAdapter sheetAdapter;


    @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //資料,一般從後臺通過網路請求到 list = new ArrayList<entity>(); for (int i = 0; i < 30; i++) { list.add(new entity("Wade", "James"
, "Kobe")); } rv_sheet = (RecyclerView) findViewById(R.id.rv_sheet); //設定線性佈局 Creates a vertical LinearLayoutManager rv_sheet.setLayoutManager(new LinearLayoutManager(this)); //設定recyclerView每個item間的分割線 rv_sheet.addItemDecoration(new DividerItemDecoration(this
, DividerItemDecoration.VERTICAL_LIST)); //建立recyclerView的例項,並將資料傳輸到介面卡 sheetAdapter = new SheetAdapter(list); rv_sheet.setAdapter(sheetAdapter); } }

2、MainActivity佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<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:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/border"
        android:orientation="horizontal">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:singleLine="true"
            android:text="表頭1"
            android:textColor="#000000"
            android:textSize="15sp" />

        <View
            android:layout_width="1.5dp"
            android:layout_height="match_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:singleLine="true"
            android:text="表頭2"
            android:textColor="#000000"
            android:textSize="15sp" />

        <View
            android:layout_width="1.5dp"
            android:layout_height="match_parent"
            android:background="#000000" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dip"
            android:singleLine="true"
            android:text="表頭3"
            android:textColor="#000000"
            android:textSize="15sp" />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_sheet"
        android:name="com.example.wjm19.sheetdemo"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

3、RecyclerView介面卡程式碼


/**
 * Created by wjm on 2016/12/6.
 */
public class SheetAdapter extends RecyclerView.Adapter {
    private  List<entity> list;

    public SheetAdapter(List<entity> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.sheet_item_layout, parent, false);
        return new sheetViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        sheetViewHolder vh = (sheetViewHolder) holder;

        vh.getTv_sheetRow1().setText(list.get(position).getSheetRow1());
        vh.getTv_sheetRow2().setText(list.get(position).getSheetRow2());
        vh.getTv_sheetRow3().setText(list.get(position).getSheetRow3());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class sheetViewHolder extends RecyclerView.ViewHolder{
        public final View mView;
        public final TextView tv_sheetRow1;
        public final TextView tv_sheetRow2;
        public final TextView tv_sheetRow3;

        public sheetViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            tv_sheetRow1 = (TextView) itemView.findViewById(R.id.tv_sheetRow1);
            tv_sheetRow2 = (TextView) itemView.findViewById(R.id.tv_sheetRow2);
            tv_sheetRow3 = (TextView) itemView.findViewById(R.id.tv_sheetRow3);
        }

        public TextView getTv_sheetRow1() {
            return tv_sheetRow1;
        }

        public TextView getTv_sheetRow2() {
            return tv_sheetRow2;
        }

        public TextView getTv_sheetRow3() {
            return tv_sheetRow3;
        }
    }
}

4、RecyclerView 中的Item佈局檔案

<?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="wrap_content"
    android:background="@drawable/border">
    <TextView
        android:id="@+id/tv_sheetRow1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:text="第一列"
        android:textColor="#000000"
        android:textSize="15sp" />

    <View
        android:layout_width="1.5dp"
        android:layout_height="match_parent"
        android:background="#000000" />

    <TextView
        android:id="@+id/tv_sheetRow2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:text="第二列"
        android:textColor="#000000"
        android:textSize="15sp" />

    <View
        android:layout_width="1.5dp"
        android:layout_height="match_parent"
        android:background="#000000" />

    <TextView
        android:id="@+id/tv_sheetRow3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:paddingBottom="10dp"
        android:paddingTop="10dip"
        android:text="第三列"
        android:textColor="#000000"
        android:textSize="15sp" />
</LinearLayout>

5、實體類

/**
 * 實體類
 * Created by wjm on 2016/12/7.
 */
public class entity {
    //第一列表頭
    private String sheetRow1;
    //第二列表頭
    private String sheetRow2;
    //第三列表頭
    private String sheetRow3;

    public entity(String sheetRow1, String sheetRow2, String sheetRow3) {
        this.sheetRow1 = sheetRow1;
        this.sheetRow2 = sheetRow2;
        this.sheetRow3 = sheetRow3;
    }

    public String getSheetRow1() {
        return sheetRow1;
    }

    public void setSheetRow1(String sheetRow1) {
        this.sheetRow1 = sheetRow1;
    }

    public String getSheetRow2() {
        return sheetRow2;
    }

    public void setSheetRow2(String sheetRow2) {
        this.sheetRow2 = sheetRow2;
    }

    public String getSheetRow3() {
        return sheetRow3;
    }

    public void setSheetRow3(String sheetRow3) {
        this.sheetRow3 = sheetRow3;
    }
}

程式碼都非常簡單,對recyclerView稍微熟一點的應該很容易看懂。
原始碼地址:sheetDemo

相關推薦

Markdown學習之(2)-繪製表格和流程圖

MarkDown表格 + 流程圖繪製 表格繪製 在Markdown上寫一個表格真是讓人頭疼的事情,寫的不流暢還要擔心格式。我為大家總結了以下三種方法,前兩種大家或許司空見慣了,第三種是神器。。。 一、md原生 | 水果 | 價格

Android使用RecyclerView繪製表格

一、效果圖 二、建立步驟: 思路:其實就是將MainActivity中寫好表頭佈局,同時和RecyclerView的Item佈局保持一致,這裡面列之間使用View進行分割,邊框使用layer-list和shape實現。 1:MainActivity程式

符號大全 繪製表格符號

* 繪表符號:─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛ 表情符號大全 心形符號大全 QQ 起暱稱專用特效字元 ︻︼︽︾〒↑↓☉⊙●〇◎¤★☆■▓「」『』◆◇▲△▼▽◣◥◢◣◤ ◥№↑↓→←↘↙Ψ※㊣∑⌒∩【】〖〗@ξζω□∮〓※》∏卐√

用latex multirow包 繪製表格

介紹一下不規則表格的一種畫法,使用multirow 這個包, 在latex檔案最前面用這個包\usepackage{multirow} 在正文中用以下程式碼:這個是模版,根據需要調整。 \begin{table}[htbp] \centering \caption{

重構一段基於原生JavaScript的表格繪製程式碼

為了在CardSimulate專案中方便的顯示技能和效果列表,決定重構以前編寫的一段JavaScript程式碼——att表格繪製庫,這段程式碼的作用是將特定的JavaScript資料物件轉化為表格,支援精細的樣式設定和一些複雜報表功能並且提供了自由的擴充套件性。可以用較新的Chrome瀏覽器訪問https:/

C# 繪製PDF巢狀表格

巢狀表格,即在一張表格中的特定單元格中再插入一個或者多個表格,使用巢狀表格的優點在於能夠讓內容的佈局更加合理,同時也方便程式套用。下面的示例中,將介紹如何通過C#程式設計來演示如何插入巢狀表格到PDF文件。要點概括: 插入巢狀表格 插入文字到巢狀表格 插入圖片到巢狀表格 使用工具

使用QTableWidget繪製表格

簡述 QTableWidget是qt自帶的表格控制元件,可以方便的繪製表格。實現效果如下所示: 程式碼之路 標頭檔案包含宣告 private: QTableWidget* m_tableWidget; QLabel* m_label; QVBoxLayout* m_vl

繪製一個帶邊框的表格 table

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="te

mpandroidchart 繪製K線圖 難點解決-高亮 連線 與 表格對齊

mpandroidchart 繪製K線圖 最少要處理高亮和對齊問題 先來個圖片看下效果 1、對齊 直接說重點,設定表格對齊的屬性,靠右顯示y軸就不說了 YAxis myYAxis = combinedChart.getAxisRight();

python繪製excel表格曲線

import os,sys,math from openpyx1.workbook import Workbook from openpyx1.writer.excel import save_workbook from openpyx1.chart import Scat

如何繪製一張簡單的html表格

效果: 表1 外觀檢查記錄表 試驗內容 XXX外觀檢查 序號 檢查部位 檢查內容 備註 表面平整 光滑 無裂縫 無毛刺 無磕碰傷痕 1 手輪操作面 2 液晶顯示器屏 3 支撐座 4 定位塊 5 鎖緊塊 檢察人員

Java圖表繪製和操作excel表格

今天學習了Java中使用JFreeChart,POI,JXL繪製圖表,操作excel表格問題,自己動手了寫了些小demo,不足之處,請大家指正。謝謝。 ^_^ 完整的類檔案在附件中。並附上一張截圖 ====================================

HTML——使用表格進行頁面布局

灰色 align doc name cin ans spa org log <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/

修復Extjs5.1.4表格設置enableTextSelection: true之後,文本仍然不能選擇的BUG

over del ble nav targe find ret lec parent 如果您發現其他版本也有此BUG,可參照此方式進行修復,源代碼中多了一句攔截mousedown事件的代碼mousedownEvent.preventDefault()造成的。 Ext.def

js實現動態刪除表格的行或者列-------Day57

back _id ron easy 技術 註意 次循環 ont 時序 昨天記錄了動態加入表格的一行,當然這個一行是指一行數據,也就是說一行多少列也是加上的,而且第幾列的內容都能夠加入上,先來回想下它的實現的關鍵點: 1、var row=table.i

selenium處理table表格

處理 輸入 table 所有 指定 固定 pat 你會 driver 在UI自動化測試中經常會遇到表格的處理,下面是一點心得。 假設網頁頁面有一個表格,如何獲取這個table的指定cell的值?你會說我們可以根據xpath定位到這個cell的行列,然後getText(),不

通過使用jsoup解析html,繪畫表格生成execl文件

num group wid 字符 for format 格式 colspan tables 1.獲取文件或者字符設置繪畫表格字符編碼 //得到Document並且設置編碼格式 public static Document getDoc(String fileNam

DOM操作表格

div 重寫 ble 修改 復雜 tle 核心 主體 tab 前面的話   表格table元素是HTML中最復雜的結構之一。要想創建表格,一般都必須涉及表示表格行、單元格、表頭等方面的標簽。由於涉及的標簽多,因而使用核心DOM方法創建和修改表格往往都免不了要編寫大量的代碼。

bootstrap-表格-響應式表格

bootstrap-表格-響應式表格1.運行效果如圖所示2.實現代碼如下<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" co

html(2)有序,無序,定義列表,表格

描述 列表 標簽 背景顏色 itl colspan cal ble 列表項 1.無序列表<ul>.............聲明無序列表<li></li>列表項</ul> 例如: <html> <head>