android listview实现表格样式

      初学android,试着写了个用listview实现的表格式样,先看下表格:

      首先看两个布局文件,mylistview.xml中的内容:

                   

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <!--实现滚动-->

    <HorizontalScrollView
        android:layoutDirection="ltr"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
       
     <ListView
         android:id="@+id/mylist"
         android:layout_width="fill_parent"
         android:dividerHeight="1px"
         android:divider="#FF909090"  //实现水平分隔线
         android:layout_height="fill_parent"
         ></ListView>
    </HorizontalScrollView>"
</RelativeLayout>

列表项中的布局:

<?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" >
    <TableLayout
        android:id="@+id/mylistItem"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

        <!--每列后的竖线-->
         <TextView
             android:id="@+id/name"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
         <View
             android:layout_width="0.5dp"
             android:background="#FF909090"
             android:layout_height="wrap_content"/>
         <TextView
             android:id="@+id/age"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
          <View
             android:layout_width="0.5dp"
             android:background="#FF909090"
             android:layout_height="wrap_content"/>
          <TextView
             android:id="@+id/sex"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
         <View
             android:layout_width="0.5dp"
             android:background="#FF909090"
             android:layout_height="wrap_content"/>
         <TextView
             android:id="@+id/school"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
          <View
             android:layout_width="0.5dp"
             android:background="#FF909090"
             android:layout_height="wrap_content"/>
          <TextView
             android:id="@+id/high"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
          <View
             android:layout_width="0.5dp"
             android:background="#FF909090"
             android:layout_height="wrap_content"/>
          </TableRow>
    </TableLayout>
       

</LinearLayout>

后台的实现是使用了adapter的getview。在activity文件中初始化适配器和列表数据,现在贴出主要的getview方法:

@Override
 public View getView(int position,View contentView,ViewGroup parent){
  
  if (null == contentView) {
   contentView = mInflater.inflate(R.layout.list_item,null);
  }
  if (views[position] != null) {//对象池,防止getView方法重复调用
   return views[position];
  }

 Display display = context.getWindowManager().getDefaultDisplay();
  this.width = display.getWidth();


  String[] data = table.get(position);
  TextView title = (TextView) contentView.findViewById(R.id.name);
  TextView age = (TextView) contentView.findViewById(R.id.age);
  TextView sex = (TextView) contentView.findViewById(R.id.sex);
  TextView school = (TextView) contentView.findViewById(R.id.school);
  TextView high = (TextView) contentView.findViewById(R.id.high);
  System.out.println("position============"+position);
  if (position == 0) {//标题行居中
   contentView.setBackgroundColor(contentView.getResources().getColor(R.color.deep_gray));
   title.setGravity(Gravity.CENTER);
   age.setGravity(Gravity.CENTER);
   sex.setGravity(Gravity.CENTER);
   school.setGravity(Gravity.CENTER);
   high.setGravity(Gravity.CENTER);
  } else if (position % 2 == 0) {//奇偶行背景色
   contentView.setBackgroundColor(contentView.getResources().getColor(R.color.palegreen));
  }else {
   contentView.setBackgroundColor(contentView.getResources().getColor(R.color.lightgreen));
  }
  title.setWidth((int)(width*0.2));//width是屏幕宽度,在此实现宽度的百分比
  age.setWidth((int)(width*0.2));
  sex.setWidth((int)(width*0.2));
  school.setWidth((int)(width*0.2));
  high.setWidth((int)(width*0.2));
  title.setText(data[0]);
  age.setText(data[1]);
  sex.setText(data[2]);
  school.setText(data[3]);
  high.setText(data[4]);
  views[position] = contentView;

//当然在这还可以对行加上点击事件
  return views[position];
  
 }

   以上就是实现的主要的代码,刚学习android,希望各位提出宝贵意见。

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 27
    评论
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值