吼吼 第一次写原创文章 有点小紧张
最近项目里要实现一个表格功能,并且被选中项要保持高亮,以前没做过类似的,经验少,上手慢,查了一些资料终于是实现了,在这里记录一下,加深印象。
这是实现的效果图:
将ListView定义成表格的样式关键在于布局文件。每列之间的竖线用宽为2像素,高为fill_parent,颜色为灰色的view实现的。
ListView的布局文件 item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#c8d0d8" />
<TextView
android:id="@+id/someday_group_name_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="20dip" />
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#c8d0d8" />
<TextView
android:id="@+id/name_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="20dip" />
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#c8d0d8" />
<TextView
android:id="@+id/position_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="20dip" />
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#c8d0d8" />
<TextView
android:id="@+id/devil_nut_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="20dip" />
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#c8d0d8" />
<TextView
android:id="@+id/hobby_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="20dip" />
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#c8d0d8" />
<TextView
android:id="@+id/dream_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:textColor="#000000"
android:textSize="20dip" />
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:background="#c8d0d8" />
</LinearLayout>
ListView 每行的灰色线条自己做了一直宽度为2像素的灰色图片,在ListView属性 android:divider="@drawable/listview_line_bg" 中引用就可以了
main.xml 的ListView 代码:
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#ffffffff"
android:choiceMode="singleChoice"
android:divider="@drawable/listview_line_bg" />
这里将ListView的选择模式设置为单选模式 android:choiceMode="singleChoice", 默认为 none 选择的时候没有效果
然后定义一个int类型值为-1的常量,在Listview的监听器中记录每次所选的item的id。
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Constant.SELECTED = position;//记录所选项
}
});
在Listview的Adapter中判断哪一个item被选中了,设置对应的item的背景颜色,其它的背景色设置为透明,就实现了被选中的item一直保持高亮效果
if(Constant.SELECTED == position){//保持被选中项高亮显示
convertView.setBackgroundColor(Color.rgb(102, 204, 255));
}else{
convertView.setBackgroundColor(Color.TRANSPARENT);
}
好了 这样用Listview实现的表格就完成了。