最近项目里要用到表格,闲着没事,研究了一下,主要的开发框架是:TableLayout+GridView+TableRow+BaseAdapter。看一下最终效果:
为什么要使用BaseAdapter?
因为当向表格中填充数据时,如果在主线程中new出row填充数据,会造成线程等待,界面会加载很慢,甚至造成异常,使用适配器,像listView一样,可以有效地对表格row进行复用,数据加载更快。
为什么要用GridView?
有人会问既然用了TableLayout了,动态加载数据时用一个Linearlayout添加表格数据就好了呀,可是Linearlayout没有setAdapter方法,GridView自带setAdapter方法,而且GridView本身就是网格视图,功能更强大。
TableLayout不带分割线Api,怎么实现的?
这里有点技巧,首先是边框:设置TableLayout为黑色背景,将TableRow的Layout_magin设置为0.1dp,tableRow中的每一个子TextView之间划线,加一个0.1dp宽度的View,即可。
整体的代码如下:
界面的布局文件:(红字为边框及分割线)
<?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="match_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
<span style="color:#ff0000;">android:background="#000000"</span> >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
<span style="color:#ff0000;">android:layout_margin="0.1dp"
android:background="#FFFFFF"</span> >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:padding="5dp"
android:text="姓名"
android:textColor="#000000"
android:textSize="18sp" />
<span style="color:#ff0000;"><View
android:layout_width="0.1dp"
android:layout_height="match_parent"
android:background="#000000" /></span>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="5dp"
android:text="选择专业"
android:textColor="#000000"
android:textSize="18sp" />
<span style="color:#ff0000;"><View
android:layout_width="0.1dp"
android:layout_height="match_parent"
android:background="#000000" /></span>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="考试成绩"
android:textColor="#000000"
android:textSize="18sp" />
<span style="color:#ff0000;"><View
android:layout_width="0.1dp"
android:layout_height="match_parent"
android:background="#000000" /></span>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:text="排名"
android:textColor="#000000"
android:textSize="18sp" />
</TableRow>
<GridView
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</GridView>
</TableLayout>
</LinearLayout>
每一个TableRow的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="match_parent"
android:orientation="vertical" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0.1dp"
android:layout_marginLeft="0.1dp"
android:layout_marginRight="0.1dp"
android:background="#FFFFFF" >
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:padding="5dp"
android:textColor="#000000"
android:textSize="14sp" />
<View
android:layout_width="0.1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:id="@+id/major"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="5dp"
android:textColor="#000000"
android:textSize="14sp" />
<View
android:layout_width="0.1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:id="@+id/score"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:textColor="#000000"
android:textSize="14sp" />
<View
android:layout_width="0.1dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:id="@+id/rank"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="5dp"
android:textColor="#000000"
android:textSize="14sp" />
</TableRow>
</LinearLayout>
Adapter:
public class TableRowAdapter extends BaseAdapter {
private List<TableData> datas;
private Context context;
public TableRowAdapter(Context ctx,List<TableData> datas) {
context = ctx;
this.datas = datas;
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TableData data = datas.get(position);
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.tableview_layout, null);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.score =(TextView)convertView.findViewById(R.id.score);
holder.major = (TextView)convertView.findViewById(R.id.major);
holder.rank = (TextView)convertView.findViewById(R.id.rank);
holder.name.setText(data.name);
holder.score.setText(data.score);
holder.major.setText(data.major);
holder.rank.setText(data.rank);
return convertView;
}
class ViewHolder{
TextView name;
TextView score;
TextView major;
TextView rank;
}
}
主界面代码:
public class TableLayoutActivity extends Activity {
private GridView tableLayout;
private TableRowAdapter adapter;
private List<TableData> datas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.table_layout);
tableLayout = (GridView)this.findViewById(R.id.table_layout);
datas = new ArrayList<TableData>();
datas.add(new TableData("张三","大学物理","95","1"));
datas.add(new TableData("李四","化学","92","2"));
datas.add(new TableData("王五","计算机","87","3"));
datas.add(new TableData("赵六","生物","82","4"));
for(int i = 0;i<30;i++){
datas.add(new TableData("haha"+i+5,"English",70-i+"",i+5+""));
}
adapter = new TableRowAdapter(this, datas);
tableLayout.setAdapter(adapter);
}
}