package com.jqyd.adapter;
import java.util.List;
import com.jqyd.app.AlwaysMarqueeTextView;
import com.jqyd.app.TableCell;
import com.jqyd.app.TableRow;
import com.jqyd.manager.R;
import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TableAdapter extends BaseAdapter {
private Context context;
private List<TableRow> table;
public TableAdapter(Context context, List<TableRow> table) {
this.context = context;
this.table = table;
}
private boolean flag;
private int flag_for_flag;
public void setFlag(boolean flag) {
this.flag = flag;
}
public int getFlag_for_flag() {
return flag_for_flag;
}
public void setFlag_for_flag(int flagForFlag) {
flag_for_flag = flagForFlag;
}
@Override
public int getCount() {
return table.size();
}
@Override
public long getItemId(int position) {
return position;
}
public TableRow getItem(int position) {
return table.get(position);
}
public View getView(int position, View convertView, ViewGroup parent) {
TableRow tableRow = table.get(position);
float size=0f;
if(flag_for_flag!=0){
if(flag){
size+=0.5;
}else{
size-=0.5;
}
}
return new TableRowView(this.context, tableRow, position,size);
}
/**
* TableRowView 实现表格行的样式
*
* @author hellogv
*/
class TableRowView extends LinearLayout {
public TableRowView(Context context, TableRow tableRow, int position,float size) {
super(context);
this.setOrientation(LinearLayout.HORIZONTAL);
if (position == 0) {
for (int i = 0; i < tableRow.getSize(); i++) {// 逐个格单元添加到行
TableCell tableCell = tableRow.getCellValue(i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
tableCell.width, tableCell.height);// 按照格单元指定的大小设置空间
layoutParams.setMargins(0, 0, 1, 1);// 预留空隙制造边框
//Log.i("TAB", "字体大小:"+tableCell.textsize);
if (tableCell.type == TableCell.STRING) {// 如果格单元是文本内容
TextView textCell = new TextView(context);
textCell.setLines(1);
if(tableCell.textsize==0f){
textCell.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
}else{
textCell.setTextSize(TypedValue.COMPLEX_UNIT_SP,tableCell.textsize);
}
textCell.setGravity(Gravity.CENTER);
textCell.setBackgroundColor(Color.BLACK);// 背景黑色
textCell.setText(String.valueOf(tableCell.value));
addView(textCell, layoutParams);
} else if (tableCell.type == TableCell.IMAGE) {// 如果格单元是图像内容
ImageView imgCell = new ImageView(context);
imgCell.setBackgroundColor(Color.BLACK);// 背景黑色
imgCell.setImageResource((Integer) tableCell.value);
addView(imgCell, layoutParams);
}
}
} else {
for (int i = 0; i < tableRow.getSize(); i++) {// 逐个格单元添加到行
TableCell tableCell = tableRow.getCellValue(i);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
tableCell.width, tableCell.height);// 按照格单元指定的大小设置空间
layoutParams.setMargins(0, 0, 1, 1);// 预留空隙制造边框
if (tableCell.type == TableCell.STRING) {// 如果格单元是文本内容
// TextView textCell = new TextView(context);
final AlwaysMarqueeTextView textCell_c=new AlwaysMarqueeTextView(context);
if(tableCell.textsize==0f){
textCell_c.setTextSize(TypedValue.COMPLEX_UNIT_SP,22);
}else{
textCell_c.setTextSize(TypedValue.COMPLEX_UNIT_SP,tableCell.textsize);
}
textCell_c.setSingleLine(true);
textCell_c.setTextColor(Color.BLACK);
textCell_c.setGravity(Gravity.CENTER);
textCell_c.setBackgroundResource(R.drawable.borderblack);
textCell_c.setText(String.valueOf(tableCell.value));
textCell_c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textCell_c.setEllipsize(android.text.TextUtils.TruncateAt.MARQUEE);
// textCell_c.setHorizontallyScrolling(true);//水平滑动
textCell_c.setMarqueeRepeatLimit(1);
textCell_c.setFocusable(false);
textCell_c.setClickable(false);
}
});
addView(textCell_c, layoutParams);
} else if (tableCell.type == TableCell.IMAGE) {// 如果格单元是图像内容
ImageView imgCell = new ImageView(context);
imgCell.setBackgroundColor(Color.WHITE);// 背景黑色
imgCell.setImageResource((Integer) tableCell.value);
addView(imgCell, layoutParams);
}
}
}
this.setBackgroundColor(Color.WHITE);// 背景白色,利用空隙来实现边框
}
}
}