package com.xspacing.listviewspace;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private Context mContext;
private CustomListView mListView;
private MyBaseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (CustomListView) findViewById(R.id.main_list_view);
mContext = this;
final List
package com.xspacing.listviewspace;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class CustomListView extends ListView {
public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpac = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpac);
}
}
package com.xspacing.listviewspace;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
List list;
Context mContext;
// 类型
public static final int ITEM_VIEW_TYPE_DATA = 0;
public static final int ITEM_VIEW_TYPE_SPACE = 1;
public static final int ITEM_VIEW_TYPE_COUNT = 2;
public MyBaseAdapter(List list, Context mContext) {
this.list = list;
this.mContext = mContext;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
// 类型
@Override
public int getItemViewType(int position) {
return list.get(position) instanceof Student ? ITEM_VIEW_TYPE_DATA : ITEM_VIEW_TYPE_SPACE;
}
// 获取类型数
@Override
public int getViewTypeCount() {
return ITEM_VIEW_TYPE_COUNT;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
switch (getItemViewType(position)) {
case ITEM_VIEW_TYPE_DATA:
ViewHolder holder = null;
// 是否复用
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_view, parent, false);
holder = new ViewHolder();
holder.mImageView = (ImageView) convertView.findViewById(R.id.main_img);
holder.mTextVieName = (TextView) convertView.findViewById(R.id.main_tv_name);
holder.mTextDesc = (TextView) convertView.findViewById(R.id.main_tv_desc);
// 设置tag
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Student student = (Student) list.get(position);
holder.mImageView.setImageResource(student.reId);
holder.mTextVieName.setText(student.name);
holder.mTextDesc.setText(student.desc);
break;
case ITEM_VIEW_TYPE_SPACE:
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_view_space, parent, false);
}
break;
default:
break;
}
return convertView;
}
class ViewHolder {
ImageView mImageView;
TextView mTextVieName;
TextView mTextDesc;
}
}
package com.xspacing.listviewspace;
public class Student {
public String name; //标题
public int reId; //图片
public String desc;//描述
public Student(String name, String desc,int reId) {
this.name = name;
this.desc = desc;
this.reId=reId;
}
public Student() {
}
}
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xspacing.listviewspace.MainActivity" >
<com.xspacing.listviewspace.CustomListView
android:id="@+id/main_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
com.xspacing.listviewspace.CustomListView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_list_view_space"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="#aaa"
android:clickable="false"
android:orientation="vertical" >
LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/main_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/main_tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一个textview"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="@+id/main_tv_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="第二个textview" />
LinearLayout>
LinearLayout>
效果图: