Android使用RecyclerView绘制表格
来源:互联网 发布:软件测试原佩腾 编辑:程序博客网 时间:2024/03/29 23:15
一、效果图
二、创建步骤:
思路:其实就是将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
0 0
- Android使用RecyclerView绘制表格
- android 使用TableLayout 绘制表格
- Android中使用ListView绘制自定义表格
- Android中使用 listView 绘制表格
- Android 绘制表格
- Android中使用ListView绘制自定义表格(2)
- Android中使用ListView绘制自定义表格(3)
- 绘制RecyclerView表格布局的分割线的类
- Android界面绘制简单表格
- Android 5.0 RecyclerView 使用
- Android RecyclerView使用
- Android RecyclerView 使用详解
- Android -- RecyclerView的使用
- Android RecyclerView 使用解析
- Android之RecyclerView使用
- Android RecyclerView使用介绍
- Android RecyclerView的使用
- Android开发--RecyclerView使用
- 兰州骑行:153公里骑行刘家峡
- canvas代码段
- yocto 各种技巧 imx6
- Angular-1.5.8文档翻译之$compile
- Vue组件理解
- Android使用RecyclerView绘制表格
- TransitionDrawable的使用
- promise 特点总结
- java运算符记录
- 如何从Eclipse 迁移到Android Studio 且保持Eclipse项目结构
- React常用插件介绍:React中我们为什么要用 redux-thunk,它能做什么?
- 矩阵的最小路径和 [DP]
- Android高效率编码-细节,控件,架包,功能,工具,开源汇总
- Java编译器的行为------第一节:方法