iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android中使用ListView绘制自定义表格技巧分享
  • 532
分享到

Android中使用ListView绘制自定义表格技巧分享

自定义表格技巧listviewAndroid 2022-06-06 10:06:42 532人浏览 泡泡鱼
摘要

先上一下可以实现的效果图  要实现的效果有几方面 1、列不固定:可以根据数据源的不同生成不同的列数 2、表格内容可以根据数据源的定义合并列 3、要填写的单元格可以选择

先上一下可以实现的效果图
 
要实现的效果有几方面
1、列不固定:可以根据数据源的不同生成不同的列数
2、表格内容可以根据数据源的定义合并列
3、要填写的单元格可以选择自定义键盘还是系统键盘
奔着这三点,做了个简单的实现,把源码贴一下(因为该点是主界面中的一部分,不便于放整个Demo)
自定义适配器,CallBackInterface是自定义的回调接口,这里定义回调是因为数据输入时需要及时保存
代码如下:
public class SiteDetailViewAdapter extends BaseAdapter implements CallBackInterface{
private Context context;
private LayoutInflater inflater;
private ArrayList<HashMap<String,Object>> lists;
private KeyBoard keyBoard = null;//自定义键盘
private ListView listView = null;
private boolean isReadOnly = false;//是否是浏览状态
private String[] arrCellType = null;
private int[] arrHeadWidth = null;//每列宽度
public SiteDetailViewAdapter(Context context, ArrayList<HashMap<String,Object>> lists
,KeyBoard keyBoard,ListView listView,boolean isReadOnly
,int[] arrHeadWidth) {
super();
this.context = context;
this.lists = lists;
inflater = LayoutInflater.from(context);
this.keyBoard = keyBoard;
this.listView = listView;
this.isReadOnly = isReadOnly;
this.arrHeadWidth = arrHeadWidth;
this.listView.setAdapter(this);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return lists.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int index, View view, ViewGroup arg2) {
HashMap map = lists.get(index);
String type = (String)map.get("rowtype");
ArrayList<ItemCell> itemCells = new ArrayList();
//String cellValue,String cellKey,long cellType,int cellInRow,int cellSpan
ItemCell itemCellXuHao = new ItemCell((index+1)+"","-1",1,-1,1);
itemCells.add(itemCellXuHao);
for(int i=0;i<map.size()-1;i++){
ItemCell itemCell = (ItemCell)map.get(i+"");
itemCells.add(itemCell);
}
//性能优化后需要放开注释danielinbiti
if(view == null||view!=null&&!((ListItemCustom)view.getTag()).getType().equals(type)){
view = inflater.inflate(R.layout.customel_list_item, null);
ListItemCustom itemCustom = (ListItemCustom)view.findViewById(R.id.custome_item);
itemCustom.buildItem(type, context, isReadOnly,arrHeadWidth,itemCells,index
,this.listView,this.keyBoard,this);
view.setTag(itemCustom);
}else{
ListItemCustom itemCustom = (ListItemCustom)view.getTag();
itemCustom.refreshData(itemCells,index);
}
if(index%2 == 0){
view.setBackgroundColor(Color.argb(250 , 255 , 255 , 255 ));
}else{
view.setBackgroundColor(Color.argb(250 , 224 , 243 , 250 ));
}
return view;
}
@Override
public boolean exectueMethod(Object params) {
String[] arr = (String[])params;
HashMap map = lists.get(Integer.parseInt(arr[0]));
ItemCell itemCell = (ItemCell)map.get(arr[1]);
itemCell.setCellValue(arr[2]);
itemCell.setIsChange(true);
return false;
}

ListView每行的布局,内部代码是有冗余的,因为是Demo,所以先以效果未准,未进行代码重构,注意不能往ListItemCustom中传入每行的Map来进行值得获取或者输入更新等操作,因为Map是按地址方式,再结合ListView的绘制方式,最终的map不是你想象的map。
代码如下:
public class ListItemCustom extends LinearLayout{
public ListItemCustom(Context context){
super(c ontext);
}
public ListItemCustom(Context context, AttributeSet attrs) {
super(context, attrs);
}
private String type = "-1";
private Context context = null;
private boolean isRead = false;
private int[] headWidthArr = null;
private ListView listView = null;
private KeyBoard keyBoard = null;
private int orderNum = -1;
private ArrayList<View> viewList = new ArrayList();
private int rowNum = -1;
private CallBackInterface callBack = null;
public void buildItem(String type,Context context,boolean isRead,int[] headWidthArr
,ArrayList<ItemCell> itemCells,int rowNum
,ListView listView,KeyBoard keyBoard,CallBackInterface callBack){
this.context = context;
this.isRead = isRead;
this.headWidthArr = headWidthArr;
this.setOrientation(LinearLayout.VERTICAL);
this.rowNum = rowNum;
this.listView = listView;
this.keyBoard = keyBoard;
this.callBack = callBack;
this.type = type;
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,45));
this.addCell(itemCells);
}
public void refreshData(ArrayList<ItemCell> itemCells,int rowNum){
this.rowNum = rowNum;
this.refreshCells(itemCells);
}
private void refreshCells(ArrayList<ItemCell> itemCells){
for(int i=0;i<itemCells.size();i++){
ItemCell itemCell = itemCells.get(i);
if(itemCell.getCellType()==ItemCellValueType.VALUE_NONE){
TextView view = (TextView)viewList.get(i);
view.setId(itemCell.getCellId());
view.setText(itemCell.getCellValue());
}else if(itemCell.getCellType()==ItemCellValueType.VALUE_NUMBER){
EditText view= (EditText)viewList.get(i);
view.setId(itemCell.getCellId());
view.setText(itemCell.getCellValue());
//view.setText(itemCell.getCellId()+"");
//view.setTag(itemCell.getCellKey()+"");
this.setEditView(view,itemCell.getCellKey());
this.setOnKeyBorad(view, itemCell.getCellInRow());
}else if(itemCell.getCellType()==ItemCellValueType.VALUE_STRING){
EditText view= (EditText)viewList.get(i);
view.setId(itemCell.getCellId());
view.setText(itemCell.getCellValue());
//view.setText(itemCell.getCellId()+"");
//view.setTag(itemCell.getCellKey()+"");
this.setEditView(view,itemCell.getCellKey());
}
}
}
private int getCellWidth(int cellStart,int cellEnd){
int width = 0;
for(int i=cellStart;i<cellEnd;i++){
width = this.headWidthArr[i] + width;
}
return width;
}
private void addCell(ArrayList<ItemCell> itemCells){
LinearLayout secondLayout = new LinearLayout(context);
secondLayout.setOrientation(LinearLayout.HORIZONTAL);
secondLayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
this.addView(secondLayout);
int cellIndex = 0;
for(int i=0;i<itemCells.size();i++){
ItemCell itemCell = itemCells.get(i);
int endIndex = cellIndex+itemCell.getCellSpan();
int width = getCellWidth(cellIndex,endIndex);
cellIndex = endIndex;
if(itemCell.getCellType()==ItemCellValueType.VALUE_NONE){
TextView view = (TextView)getReadView();
view.setId(itemCell.getCellId());
view.setText(itemCell.getCellValue());
view.setWidth(width);
secondLayout.addView(view);
viewList.add(view);
}else if(itemCell.getCellType()==ItemCellValueType.VALUE_NUMBER){
EditText view= (EditText)getInputView();
view.setId(itemCell.getCellId());
view.setText(itemCell.getCellValue());
view.setWidth(width);
//view.setText(itemCell.getCellId()+"");
//view.setTag(itemCell.getCellKey()+"");
this.setEditView(view,itemCell.getCellKey());
this.setOnKeyBorad(view, itemCell.getCellInRow());
secondLayout.addView(view);
viewList.add(view);
}else if(itemCell.getCellType()==ItemCellValueType.VALUE_STRING){
EditText view= (EditText)getInputView();
view.setId(itemCell.getCellId());
view.setText(itemCell.getCellValue());
view.setWidth(width);
//view.setText(itemCell .getCellId()+"");
//view.setTag(itemCell.getCellKey()+"");
this.setEditView(view,itemCell.getCellKey());
secondLayout.addView(view);
viewList.add(view);
}
if(i!=itemCells.size()-1){
LinearLayout v_line = (LinearLayout)getVerticalLine();
v_line.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
secondLayout.addView(v_line);
}
}
}
private View getVerticalLine(){
return LayoutInflater.from(context).inflate(R.layout.atom_line_v_view, null);
}
private View getReadView(){
return (View)LayoutInflater.from(context).inflate(R.layout.atom_text_view, null);
}
private View getInputView(){
return (View)LayoutInflater.from(context).inflate(R.layout.atom_edttxt_view, null);
}
private void setOnKeyBorad(EditText edtText1,int index){
if(!this.isRead){
onListenText(edtText1,this.listView,index);
}
}
private void onListenText(EditText edtText,ListView listView,int index){
final ListView lsv = listView;
final int idx = index;
edtText.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View arg0, boolean arg1) {
if(arg1){
int[] y = getYPos(lsv,idx);
keyBoard.show((EditText)arg0,y[0],y[1]);
}
}
});
}
private int[] getYPos(ListView listview,int index){
int[] yPosArr = new int[2];
Rect r = new Rect();
listview.getChildAt(0).getGlobalVisibleRect(r);
int first = listview.getFirstVisiblePosition();
yPosArr[1] = (index-first+1)*r.height()+listview.getTop();
yPosArr[0] = (index-first)*r.height()+listview.getTop();
return yPosArr;
}
private void setEditView(EditText edtText1,final String key){
if(this.isRead){
edtText1.setEnabled(false);
}else{
edtText1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
String[] arr = new String[3];
arr[0] = rowNum+"";
arr[1] = key;
arr[2] = arg0.toString();
callBack.exectueMethod(arr);
// ItemCell itemCell = (ItemCell)dataMap.get(key);
// itemCell.setCellValue(arg0.toString());
// itemCell.setIsChange(true);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
});
}
}
public String getType(){
return this.type;
}
}

代码如下:
public class ItemCell {
private String cellValue = "";
private int cellSpan = 1;
private String cellKey = "";
private int cellInRow = 0;
private long cellType = ItemCellValueType.VALUE_NONE;
private int colNum = 0;
private int rowType = 0;
private int cellId = -1;
private boolean isValueFromTable = false;
private boolean isChange = false;
public ItemCell(String cellValue,String cellKey,long cellType,int cellInRow,int cellSpan){
this.cellValue = cellValue;
this.cellType = cellType;
this.cellSpan = cellSpan;
this.cellKey = cellKey;
this.cellInRow = cellInRow;
}
public ItemCell(String cellValue,String cellKey,long cellType,int cellInRow){
this(cellValue,cellKey,cellType,cellInRow,1);
}
public void setColNum(int colNum){
this.colNum = colNum;
}
public int getColNum(){
return this.colNum;
}
public void setRowType(int rowType){
this.rowType = rowType;
}
public int getRowType(){
return this.rowType;
}
public String getCellValue(){
return cellValue;
}
public void setCellValue(String value){
this.cellValue = value;
}
public long getCellType(){
return cellType;
}
public int getCellSpan(){
return cellSpan;
}
public String getCellKey(){
return cellKey;
}
public int getCellInRow(){
return cellInRow;
}
public void setIsChange(boolean isChange){
this.isChange = isChange;
}
public boolean getIsChange(){
ret urn this.isChange;
}
public int getCellId() {
return cellId;
}
public void setCellId(int cellId) {
this.cellId = cellId;
}
public boolean isValueFromTable() {
return isValueFromTable;
}
public void setValueFromTable(boolean isValueFromTable) {
this.isValueFromTable = isValueFromTable;
}
}

custome_list_item.xml文件
代码如下:
<?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="40dp"
android:orientation="vertical"
android:id="@+id/test_dajej"
android:background="#ffffff">
<srit.collection.widget.costomtable.ListItemCustom android:id="@+id/custome_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

以上是核心的文件内容。有了列合并,行合并也不远了。可以在自定义的布局中多加个LinearLayout来实现行合并。 您可能感兴趣的文章:Android自定义View实现仿GitHub的提交活跃表格Android listView 绘制表格实例详解Android自定义DataGridView数据表格控件Android应用中通过Layout_weight属性用ListView实现表格Android中使用ListView实现漂亮的表格效果Android提高之ListView实现自适应表格的方法android表格效果之ListView隔行变色实现代码Android自定义View实现课程表表格


--结束END--

本文标题: Android中使用ListView绘制自定义表格技巧分享

本文链接: https://www.lsjlt.com/news/27611.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android自定义view绘制表格的方法
    本文实例为大家分享了Android自定义view绘制表格的具体代码,供大家参考,具体内容如下 先上效果图 平时很少有这样的表格需求,不过第一想法就是自定义view绘制表格,事实上我...
    99+
    2024-04-02
  • MPAndroidChart自定义图表绘制使用实例
    目录引言1. LineGradientChart2. 散点图3. SteppedChart引言 声明:文中的MPChart代指MPAndroidChart. 本文主要讲解LineCh...
    99+
    2024-04-02
  • Android表格自定义控件使用详解
    近期公司要做报表功能,在网上搜索下表格的样式后便自己写了一个自定义的表格控件,该表格控件能根据设置的数据中数据的最大值自动设置左侧信息栏显示的值,使得条形图能尽量的充满控件,条形图部...
    99+
    2024-04-02
  • Android中如何使用自定义view实现ListView下拉的视差特效功能
    这篇文章给大家分享的是有关Android中如何使用自定义view实现ListView下拉的视差特效功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、概述:现在流型的APP如微信朋友圈,QQ空间,微博个人展示都...
    99+
    2023-05-30
    android view listview
  • 如何在Android中使用PopupWindow制作一个自定义弹窗
    本篇文章给大家分享的是有关如何在Android中使用PopupWindow制作一个自定义弹窗,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。代码:PopupWindow ...
    99+
    2023-05-31
    android popupwindow
  • Android开发如何使用自定义View将圆角矩形绘制在Canvas上的方法
    这篇文章主要介绍了Android开发如何使用自定义View将圆角矩形绘制在Canvas上的方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Android开发使用自定义Vie...
    99+
    2023-05-30
    android view canvas
  • 如何在PHP中使用不同的数据类型?shell重定向技巧分享!
    PHP作为一种流行的脚本语言,为开发人员提供了许多有用的工具和功能,其中包括不同的数据类型和shell重定向技巧。在本文中,我们将深入探讨如何在PHP中使用不同的数据类型和shell重定向技巧,为您提供有关这些主题的详细信息。 PHP中的...
    99+
    2023-08-09
    数据类型 shell 重定向
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作