教你制作表格样式的ListView

时间:2022-11-20 09:19:14

最近在做工作流的项目,项目开发中需要把项目的明细用表格的样式展示在ListView控件上,前前后后测试了几个demo运用到项目中,感觉相当佩服他们,所以也尝试着学习他们自己制作了一个demo,就当自娱自乐,仅供参考。你们也可以参考以下博客:

http://blog.csdn.net/bewhatyouare/article/details/8373584

http://www.diannao.wang/anzhuo/2015/52-81030.html

项目中由于单条信息过长,一个屏幕不可能完全展示出来,所以用到了HorizontalScrollView控件,此控件可以实现屏幕的横向滚动。

项目开发中ListView适配器的布局界面list_item.xml如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/name"
        style="@style/itemStyle"
        android:layout_width="140dp" />

    <View
        style="@style/viewStyle"
        android:visibility="visible" />

    <TextView
        android:id="@+id/age"
        style="@style/itemStyle"
        android:layout_width="70dp" />

    <View
        style="@style/viewStyle"
        android:visibility="visible" />

    <TextView
        android:id="@+id/height"
        style="@style/itemStyle"
        android:layout_width="55dp" />

    <View
        style="@style/viewStyle"
        android:visibility="visible" />

    <TextView
        android:id="@+id/weight"
        style="@style/itemStyle"
        android:layout_width="150dp" />

    <View
        style="@style/viewStyle"
        android:visibility="visible" />

    <TextView
        android:id="@+id/school"
        style="@style/itemStyle"
        android:layout_width="150dp" />


</LinearLayout>

Activity用到的布局界面activity_main.xml如下:

<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" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:background="#fc8510"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

   
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="标题"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </RelativeLayout>

   
    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="2dip"
        android:layout_marginLeft="2dip"
        android:layout_marginRight="2dip"
        android:background="@drawable/list"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <TextView
                    style="@style/itemHeaderStyle"
                    android:layout_width="140dp"
                    android:text="@string/name" />

                <View
                    style="@style/viewStyle"
                    android:visibility="visible" />

                <TextView
                    style="@style/itemHeaderStyle"
                    android:layout_width="70dp"
                    android:text="@string/age" />

                <View
                    style="@style/viewStyle"
                    android:visibility="visible" />

                <TextView
                    style="@style/itemHeaderStyle"
                    android:layout_width="55dp"
                    android:text="@string/height" />

                <View
                    style="@style/viewStyle"
                    android:visibility="visible" />

                <TextView
                    style="@style/itemHeaderStyle"
                    android:layout_width="150dp"
                    android:text="@string/weight" />

                <View
                    style="@style/viewStyle"
                    android:visibility="visible" />

                <TextView
                    style="@style/itemHeaderStyle"
                    android:layout_width="150dp"
                    android:text="@string/school" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1px"
                android:background="#848484"
                android:visibility="visible" />

            <ListView
                android:id="@id/list"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:cacheColorHint="@null"
                android:divider="#848484"
                android:dividerHeight="1px"
                android:scrollbarSize="10.0dip" >
            </ListView>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1px"
                android:background="#848484"
                android:visibility="visible" />
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

为了增强代码的规范性,降低代码的耦合性,使用的资源设定在适当的地方。

上面的布局界面中使用到的样式在res/values/styles.xml文件中:

<resources>
    <!-- 列表表头样式 -->
    <style name="itemHeaderStyle">
        <item name="android:background">#c8c8c8</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">#3C3C3C</item>
        <item name="android:textSize">15sp</item>
        <item name="android:layout_height">25dp</item>
    </style>

    <!-- 横线样式 -->
    <style name="viewStyle">
        <item name="android:layout_width">1px</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:background">#848484</item>
    </style>
    <!-- 列表样式 -->
    <style name="itemStyle">
        <item name="android:gravity">center</item>
        <item name="android:textColor">#3C3C3C</item>
        <item name="android:textSize">15sp</item>
        <item name="android:singleLine">true</item>
        <item name="android:layout_height">25dp</item>
    </style>
</resources>

布局界面中使用到的字符串资源在res/values/strings.xml文件中:
布局界面中使用到的字符串资源应

 
    <string name="name">姓名</string>
    <string name="age">年龄</string>
    <string name="height">身高</string>
    <string name="weight">体重</string>
    <string name="school">学校</string>


 

 
布局界面中使用到的图片、背景资源放在res/background文件夹中: 

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <!-- 定义边框内部颜色 -->
    <solid android:color="#FFFFFF" />
    <!-- 定义边框颜色和宽度 -->
    <stroke
        android:width="1.0px"
        android:color="#3C3C3C" />
    <!-- 定义边框内部开始和结束时的颜色 -->
    <gradient
        android:endColor="#FFFFFF"
        android:startColor="#FFFFFF" />
    <!-- 定义内边距 -->
    <padding
        android:bottom="1.0px"
        android:left="1.0px"
        android:right="1.0px"
        android:top="1.0px" />

</shape>
接下来就该我们的适配器上场了:我们自定义适配器让它继承自BaseAdapter:
 
public class StudentAdapter extends BaseAdapter {
	
	private LayoutInflater inflater;
	private List<Student> students;
	private Context context = null;

	public ReportAdapter(List<Student> students, Context context) {
		super();
		this.students= students;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}
	
	@Override
	public int getCount() {
		return students.size();
	}

	@Override
	public Object getItem(int position) {
		return students.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.list_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView
					.findViewById(R.id.name);
			holder.age = (TextView) convertView
					.findViewById(R.id.age);
			holder.height = (TextView) convertView
					.findViewById(R.id.height);
			holder.weight = (TextView) convertView
					.findViewById(R.id.weight);
			holder.school = (TextView) convertView
 

 

 
					     .findViewById(R.id.school);
			          convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		int[] colors = { Color.WHITE, Color.parseColor("#d5d5d5") };
		convertView.setBackgroundColor(colors[position % 2]);
		Student student = students.get(position);
		holder.name.setText(student.getName());
		holder.age.setText(student.getAge());
		holder.height.setText(student.getHeight());
		holder.weight.setText(student.getWeight());
		holder.school.setText(student.getSchool());
		return convertView;
	}
	
	private class ViewHolder{
		TextView name;
		TextView age;
		TextView height;
		TextView weight;
		TextView school;
	}

}
public class MainActivity extends ListActivity {
	private List<Student>> list;
	private StudentAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		list = new ArrayList<Student>>();
		//Student实体类我没有创建构造方法,系统也会自动创建了一个无参的构造方法,里面的属性可有可无
		Student s1 = new Student();
                s1.setName("张三");
 		s1.setAge(19);
		
		Student s2 = new Student();
                s2.setName("李四");
 		s2.setHeight(175);
		Student s3 = new Student();
                s3.setName("王五");
 		s3.setWeight(65);
		list.add(s1);
		list.add(s2);
		list.add(s3);
		adapter = new ListAdapter(MainActivity.this, list);
		getListView().setAdapter(adapter);
	}

}