背景
这里是为了解决在ScrollView包裹下,出现的各种奇葩问题。
1.在ScrollView包裹下ListView无法伸展开的问题
public class AdaptSvListView extends DigitalListView {
public AdaptSvListView(Context context) {
super(context);
}
public AdaptSvListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AdaptSvListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* 重写该方法,达到使ListView适应ScrollView的效果
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
2.防止包裹的ListView或GridView设置高度后,ScrollView的初始位置不在0
public class NotZeroScrollView extends ScrollView {
public NotZeroScrollView(Context context) {
super(context);
}
public NotZeroScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NotZeroScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void scrollTo(int x, int y) {
//super.scrollTo(x, y);
}
}
3.可设置可滑动不可滑动的ViewPager
public class ScrollAbleViewPager extends ViewPager {
private boolean scrollAble = true;
public ScrollAbleViewPager(Context context) {
super(context);
}
public ScrollAbleViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 设置是否可滑动
* @param scrollAble false不可滑动,true可滑动
*/
public void setScrollEnabled(boolean scrollAble) {
this.scrollAble = scrollAble;
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
if (!scrollAble)
return false;
else
return super.onTouchEvent(arg0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (!scrollAble)
return false;
else
return super.onInterceptTouchEvent(arg0);
}
}
4.在ScrollView包裹弹出PopupWindows导致ScrollView滚动的问题
public class NotZeroScrollView extends ScrollView {
public NotZeroScrollView(Context context) {
super(context);
}
public NotZeroScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NotZeroScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void scrollTo(int x, int y) {
//super.scrollTo(x, y);
}
}
网友评论