Android HorizontaScrollView和滑动屏幕时出现问题
在我的代码中,我使用HorizontalScrollView实现了滑动,因为我得到了一个正确的工作代码,向左或向右滑动工作正常。我的问题是我想显示一个动态页码。为此,我在其中添加了一些额外的代码,但它不工作。下面是代码Android HorizontaScrollView和滑动屏幕时出现问题,android,android-layout,Android,Android Layout,在我的代码中,我使用HorizontalScrollView实现了滑动,因为我得到了一个正确的工作代码,向左或向右滑动工作正常。我的问题是我想显示一个动态页码。为此,我在其中添加了一些额外的代码,但它不工作。下面是代码 public class PaginationLayout extends LinearLayout { private int mPageActive = 0; private HorizontalScrollView mScroll; private
public class PaginationLayout extends LinearLayout {
private int mPageActive = 0;
private HorizontalScrollView mScroll;
private LinearLayout mBar;
public PaginationLayout(Context context) {
super(context);
setOrientation(LinearLayout.VERTICAL);
final GestureDetector mGestureDetector = new GestureDetector(new MySimpleOnGestureListener());
mScroll = new HorizontalScrollView(context);
mScroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
return true;
} else {
return false;
}
}
});
super.addView(mScroll);
}
@Override
public void addView(View child) {
mScroll.addView(child);
}
@Override
protected void onSizeChanged(int arg0, int arg1, int arg2, int arg3) {
super.onSizeChanged(arg0, arg1, arg2, arg3);
View chield = mScroll.getChildAt(0);
if (chield != null) {
if (chield.getMeasuredWidth() > getWidth()) {
mBar.setVisibility(LinearLayout.VISIBLE);
} else {
mBar.setVisibility(LinearLayout.INVISIBLE);
}
}
}
public void previous() {
mPageActive = (mPageActive > 0) ? mPageActive - 1 : 0;
mScroll.smoothScrollTo(mPageActive * mScroll.getWidth(), 0);
}
public void next() {
int pageWidth = mScroll.getWidth();
int nextPage = (mPageActive + 1) * pageWidth;
if (nextPage - mScroll.getScrollX() <= pageWidth) {
mScroll.smoothScrollTo(nextPage, 0);
mPageActive++;
} else {
mScroll.smoothScrollTo(mScroll.getScrollX(), 0);
}
}
private class MySimpleOnGestureListener extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1 != null && e2 != null) {
if (e1.getX() - e2.getX() > 0) {
// forward...
next();
return true;
} else if (e2.getX() - e1.getX() > 0) {
// back...
previous();
return true;
}
}
return false;
}
}
public void setPage(int noOfPageToScroll ) {
int pageWidth = mScroll.getWidth(); //problem is coming in this line its coming 0 always
int nextPage = (noOfPageToScroll + 1) * pageWidth;
Log.v("trace","Pagination.setPage() called with noOfPageToScroll = "+noOfPageToScroll+"and nextPage = "+nextPage+ " and pageWidth = "+pageWidth);
mScroll.smoothScrollTo(nextPage, 0);
}
}
//据我所知,问题是我没有得到mscroll.width()==0,它是在绘制视图之前调用的,或者可能是其他原因
} catch (Exception e) {
Log.e("Error", "Problem in setUpViews() code 4 ");
e.printStackTrace();
}
}
//请帮助我,忽略任何语法错误。提前感谢我使用viewflipper解决了这个问题,因为在ZontalScrollView中,视图大小变得非常大
} catch (Exception e) {
Log.e("Error", "Problem in setUpViews() code 4 ");
e.printStackTrace();
}
}