How to Use ListView to draw a custom table in Android

Source: Internet
Author: User

What can be implemented first

The effects to be achieved are as follows:
1. columns are not fixed: different columns can be generated based on different data sources.
2. The table content can be used to merge columns according to the definition of the data source.
3. You can select a custom keyboard or system keyboard for the cells to be filled in.
Run these three points to make a simple implementation and paste the source code (because this is part of the main interface, it is not easy to put the entire Demo)
Custom Adapter. CallBackInterface is a custom callback interface, which is defined here because data needs to be saved in time during input. Copy codeThe Code is as follows: public class SiteDetailViewAdapter extends BaseAdapter implements CallBackInterface {
Private Context context;
Private LayoutInflater inflater;
Private ArrayList <HashMap <String, Object> lists;
Private KeyBoard keyBoard = null; // custom KeyBoard
Private ListView listView = null;
Private boolean isReadOnly = false; // whether it is in browsing status
Private String [] arrCellType = null;
Private int [] arrHeadWidth = null; // The width of each column

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 );
}
// Note danielinbiti after Performance Optimization
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;
}

For the layout of each row in ListView, internal code is redundant. Because it is a Demo, the effect is not accurate first, and no code reconstruction is performed, note that you cannot import Map of each row to ListItemCustom for value acquisition or input update operations, because Map is based on the address, and then combined with the ListView painting method, the final map is not the map you think.Copy codeThe Code is as follows: public class ListItemCustom extends LinearLayout {
Public ListItemCustom (Context context ){
Super (context );
}
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.exe ctueMethod (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;
}
}

Copy codeThe Code is as follows: 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 (){
Return 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 FileCopy codeThe Code is as follows: <? 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>

The above is the core file content. With column merge, row merge is not far away. You can add more LinearLayout in the custom layout to merge rows.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.