教你制作表格样式的ListView

最近在做工作流的项目,项目开发中需要把项目的明细用表格的样式展示在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如下:



    

    

    

    

    

    

    

    

    



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



    

   
        
    

   
    

        

            

                

                

                

                

                

                

                

                

                
            

            

            
            

            
        
    

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

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


    
    

    
    
    
    

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

    姓名
    年龄
    身高
    体重
    学校


 
  

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


    
    
    
    
    
    
    
    


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

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

	public void setStudents(List 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> list;
	private StudentAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		list = new ArrayList>();
		//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);
	}

}

 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(Android开发)