设为首页 加入收藏

TOP

Android多个页面滑动视图实现(二)
2014-11-24 13:19:09 来源: 作者: 【 】 浏览:3
Tags:Android 多个 页面 滑动 实现
Scroller;


public class ScrollLayout extends ViewGroup {
private static final String TAG = "ScrollLayout";
private Scroller mScroller = null;
private VelocityTracker mVelocityTracker = null;

private int mCurScreen = 0;
private int mDefaultScreen = 1;

private static final int TOUCH_STATE_REST = 0;
private static final int TOUCH_STATE_SCROLLING = 1;


private static final int SNAP_VELOCITY = 600;

private int mTouchState = TOUCH_STATE_REST;
private int mTouchSlop = 0;
private float mLastMotionX = 0;
private float mLastMotionY = 0;

private OnScreenChangedListener mOnScreenChangedListener;

public void setOnScreenChangedListener(OnScreenChangedListener l){
mOnScreenChangedListener = l;
}

public interface OnScreenChangedListener {
void onScreenChanged(ScrollLayout sc ,int whichScreen);
}

public ScrollLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

mScroller = new Scroller(context);


mCurScreen = mDefaultScreen;
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
if (changed) {
int childLeft = 0;
final int childCount = getChildCount();

for (int i=0; i final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
childView.layout(childLeft, 0,
childLeft+childWidth, childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.e(TAG, "onMeasure");
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("ScrollLayout only can run at EXACTLY mode!");
}

final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("ScrollLayout only can run at EXACTLY mode!");
}

// The children are given the same width and height as the scrollLayout
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
// Log.e(TAG, "moving to screen "+mCurScreen);
scrollTo(mCurScreen * width, 0);
}

/**
* According to the position of current layout
* scroll to the destination page.
*/
public void snapToDestination() {
final int screenWidth = getWidth();
final int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;
snapToScreen(destScreen);
}

public void snapToScreen(int whichScreen)
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Eclipse中Android项目的目录结构 下一篇Android的AIDL实现进程间通信

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: