经常会在项目中遇到这样的布局,即一个列表中会有各种不同的布局,但是其中的部分布局又是一致,而且要一起滑动,这样的布局是很麻烦的!
这里说两种解决办法
一、这是最常见的一种,用scrollView去包裹整个布局,但是整个方法弊端很多
如果其中涉及到listView的话,这时候就要去重写listView了,即:
public class MyListView extends ListView {
public MyListView(Context context) {
// TODO Auto-generated method stub
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
// TODO Auto-generated method stub
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
// TODO Auto-generated method stub
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
public MyListView(Context context) {
// TODO Auto-generated method stub
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
// TODO Auto-generated method stub
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
// TODO Auto-generated method stub
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
然后布局就直接摆就行了。
但是整个方法弊端很明显,如果listView数据不多的话,可以用这种方法,很方便,一旦数据多的话,就不要用了,因为这里面的ListView是不复用的,所以尽量减少使用!
二、就是通过填写listView不同的item布局来实现。
adapter里要重写如下方法:
@Overridepublic int getItemViewType(int position) {
if (position == 0) {
return 0;
} else if (position == 1) {
return 1;
} else if (position == 2) {
return 2;
} else {
return 3;
}
}
@Override
public int getViewTypeCount() {
return 4;
}
通过返回不同的数据类型来给listView设置不同的布局,然后再getView里通过不同的类型来实现不同的item
View oneview;
View secondview;
View thridview;
View fourthView;
int type = getItemViewType(position);
if (type == 0) {
Holder2 holder2;
if (convertView == null) {
holder2 = new Holder2();
secondview = LayoutInflater.from(ListActivity.this)
.inflate(R.layout.item_list, null);
holder2.lv_item = (ListView) secondview
.findViewById(R.id.lv);
secondview.setTag(holder2);
convertView = secondview;
} else {
holder2 = (Holder2) convertView.getTag();
}
} else if (type == 1) {
Holder1 holder1;
if (convertView == null) {
holder1 = new Holder1();
oneview = LayoutInflater.from(ListActivity.this).inflate(
R.layout.item_one, null);
holder1.tv = (TextView) oneview.findViewById(R.id.tv_one);
oneview.setTag(holder1);
convertView = oneview;
} else {
holder1 = (Holder1) convertView.getTag();
}
}
View secondview;
View thridview;
View fourthView;
int type = getItemViewType(position);
if (type == 0) {
Holder2 holder2;
if (convertView == null) {
holder2 = new Holder2();
secondview = LayoutInflater.from(ListActivity.this)
.inflate(R.layout.item_list, null);
holder2.lv_item = (ListView) secondview
.findViewById(R.id.lv);
secondview.setTag(holder2);
convertView = secondview;
} else {
holder2 = (Holder2) convertView.getTag();
}
} else if (type == 1) {
Holder1 holder1;
if (convertView == null) {
holder1 = new Holder1();
oneview = LayoutInflater.from(ListActivity.this).inflate(
R.layout.item_one, null);
holder1.tv = (TextView) oneview.findViewById(R.id.tv_one);
oneview.setTag(holder1);
convertView = oneview;
} else {
holder1 = (Holder1) convertView.getTag();
}
}
但是要记住的是如果item里涉及到listVIew一定要重写listVIew,同第一种方法。