好久不写博客了,今天记录一下固定高度ListView的写法。ListView是Android中最早的列表控件,可以说使用也是十分广泛的。虽然我们现在用到列表的地方往往用使用更加广泛的RecyclerView多一些,但是ListView还是有很多人使用的,所以也是有必要学习学习。那么ListView如果在ScrollView中使用时,我们为了让两者滑动不要冲突,我们的处理方法往往是把ListView的高度设置为所有内容高度。那就需要我们自定义ListView。
/**
* 固定高度列表控件
* Created by WaterWood on 2018/5/15.
*/
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
在ScrollView中,我们使用这个控件替代ListView,就可以实现我们要的功能,固定ListView的高度。 |