public class MyHorizontalScrollView extends HorizontalScrollView {
private int subChildCount = 0;
private ViewGroup firstChild = null;
private int downX = 0;
public int currentPage = 0;
int paddle;
private ArrayList pointList = new ArrayList();
public MyHorizontalScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyHorizontalScrollView(Context context) {
super(context);
init();
}
private void init() {
setHorizontalScrollBarEnabled(false);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
receiveChildInfo();
}
public void receiveChildInfo() {
firstChild = (ViewGroup) getChildAt(0);
if (firstChild != null) {
subChildCount = firstChild.getChildCount();
for (int i = 0; i < subChildCount; i++) {
if (((View) firstChild.getChildAt(i)).getWidth() > 0) {
Log.i("hx",
"MyHorizontalScrollView---->receiveChildInfo:"
+ i
+ " "
+ ((View) firstChild.getChildAt(i))
.getLeft());
pointList.add(((View) firstChild.getChildAt(i)).getLeft());
}
}
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = (int) ev.getX();
Log.i("hx", "onInterceptTouchEvent---->ACTION_DOWN::" + downX);
break;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// Log.i("PLJ", "MyHorizontalScrollView---->ACTION_DOWN::"+downX);
break;
case MotionEvent.ACTION_MOVE: {
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
if (currentPage == 0) {
paddle = getWidth() / 5;
} else {
paddle = getWidth() / 10;
}
if (Math.abs((ev.getX() - downX)) > paddle) {
if (ev.getX() - downX > 0) {
Log.i("hx", "MyHorizontalScrollView---->ACTION_UP:111:"
+ (ev.getX() - downX) + " " + ev.getX() + " "
+ downX + " " + getWidth() + " " + paddle);
smoothScrollToPrePage((int) Math.abs((ev.getX() - downX)));
} else {
Log.i("hx", "MyHorizontalScrollView---->ACTION_UP:222:"
+ (ev.getX() - downX) + " " + ev.getX() + " "
+ downX + " " + getWidth() + " " + paddle);
smoothScrollToNextPage((int) Math.abs((ev.getX() - downX)));
}
} else {
Log.i("hx",
"MyHorizontalScrollView---->ACTION_UP:333:"
+ (ev.getX() - downX) + " " + ev.getX()
+ " " + downX + " " + getWidth() + " "
+ paddle);
smoothScrollToCurrent();
}
return true;
}
}
return super.onTouchEvent(ev);
}
/** 当前页 **/
private void smoothScrollToCurrent() {
smoothScrollTo(pointList.get(currentPage), 0);
}
/** 上一页 **/
private void smoothScrollToPrePage(int intPre) {
Log.i("hx", "MyHorizontalScrollView---->smoothScrollToPrePage:111:"
+ (currentPage > 0) + " " + currentPage);
if (currentPage > 0) {
currentPage -= Zoom(intPre);
if (currentPage < 0) {
currentPage = 0;
}
/* currentPage--; */Log.i("hx",
"MyHorizontalScrollView---->smoothScrollToPrePage:222:"
+ currentPage + " " + pointList.get(currentPage));
smoothScrollTo(pointList.get(currentPage), 0);
}
}
// private int PreZoom(int intPre){
// int page = 0;
// for(int i=0;i<pointList.size();i++){
// if((pointList.get(i+1) > intPre) && (intPre > pointList.get(i))){
// //if(i != 0){
// page = i+1;
// //}
// break;
// }
// } Log.i("PLJ", "MyHorizontalScrollView---->PreZoom:"+intPre+" "+page);
// return page;
// }
/** 下一页 **/
private void smoothScrollToNextPage(int intNext) {
Log.i("hx", "MyHorizontalScrollView---->smoothScrollToNextPage:111:"
+ (currentPage < subChildCount - 1) + " " + currentPage
+ " " + (subChildCount - 1));
if (currentPage < subChildCount - 1) {
currentPage += Zoom(intNext);
if (currentPage > subChildCount - 1) {
currentPage = subChildCount - 1;
}
/* currentPage++; */Log.i("hx",
"MyHorizontalScrollView---->smoothScrollToNextPage:222:"
+ currentPage + " " + pointList.get(currentPage));
smoothScrollTo(pointList.get(currentPage), 0);
}
}
private int Zoom(int intNext) {
int page = 0;
for (int i = 0; i < pointList.size(); i++) {
if (!pointList.isEmpty()) {
Log.i("hx", "pointList:isEmpty" + i);
if ((pointList.get(i + 1) + 20 > intNext)
&& (intNext > pointList.get(i) + 20)) {
Log.i("hx", "pointList.get(i + 1) : " + pointList.get(i + 1) + 20 +" : "+ intNext);
// if(i != 0){
page = i + 1;
Log.i("hx", "page = i + 1 : " + page);
// }
break;
}
}
}
Log.i("hx", "MyHorizontalScrollView---->Zoomm:" + intNext + " "
+ page);
return page;
}
说我Zoom方法里面的if那一行的list越界了 请问如何做处理