最关键的代码:
package com.dianxing.dialog;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;
public class ScrollLayout extends ViewGroup {
private static final String TAG = "ScrollLayout";
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mCurScreen;
private int mDefaultScreen = 0;
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;
private float mLastMotionX;
private float mLastMotionY;
private OnCurrentViewChangedListener mOnCurrentViewChangedListener;
public OnCurrentViewChangedListener getmOnCurrentViewChangedListener() {
return mOnCurrentViewChangedListener;
}
public void setmOnCurrentViewChangedListener(
OnCurrentViewChangedListener mOnCurrentViewChangedListener) {
this.mOnCurrentViewChangedListener = mOnCurrentViewChangedListener;
}
public interface OnCurrentViewChangedListener {
public void onCurrentViewChanged(View view, int currentview);
}
public ScrollLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO 再执行这个函数
Log.i(TAG, "ScrollLayout(1)");
}
public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO 首先执行这个函数
Log.i(TAG, "ScrollLayout(2)");
mScroller = new Scroller(context);
mCurScreen = mDefaultScreen;
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
Log.i(TAG, "mTouchSlop ===== " + mTouchSlop);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
Log.i(TAG, "onLayout");
Log.i(TAG, "changed === " + changed);
if (changed) {
int childLeft = 0;
final int childCount = getChildCount();
for (int i=0; i<childCount; i++) {
final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
Log.i(TAG, "childView.getMeasuredWidth() === " + childView.getMeasuredWidth());
childView.layout(childLeft, 0, childLeft+childWidth, childView.getMeasuredHeight());
Log.i(TAG, "childView.getMeasuredHeight() === " + childView.getMeasuredHeight());
childLeft += childWidth;
Log.i(TAG, "childLeft === " + childLeft);
}
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.e(TAG, "onMeasure");
Log.e(TAG, "onMeasure widthMeasureSpec === " + widthMeasureSpec);
Log.e(TAG, "onMeasure heightMeasureSpec ==== " + heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
Log.e(TAG, "width ==onMeasure== " + width);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
Log.e(TAG, "widthMode ==onMeasure== " + widthMode);
// if (widthMode != MeasureSpec.EXACTLY) {
// throw new IllegalStateException("ScrollLayout only canmCurScreen run at EXACTLY mode!");
// }
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Log.e(TAG, "heightMode ==onMeasure== " + heightMode);
// 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();
Log.i(TAG, "count = getChildCount() ===== " + count);
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();
Log.i(TAG, "screenWidth ==snapToDestination== " + screenWidth);
final int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;
Log.i(TAG, "destScreen ==snapToDestination== " + destScreen);
snapToScreen(destScreen);
}
public void snapToScreen(int whichScreen) {
// get the valid layout page
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
Log.i(TAG, "whichScreen ==== " + whichScreen);
Log.i(TAG, "getScrollX() ==== " + getScrollX());
Log.i(TAG, "whichScreen*getWidth() ==== " + whichScreen*getWidth());
if (getScrollX() != (whichScreen*getWidth())) {
final int delta = whichScreen*getWidth()-getScrollX();
Log.i(TAG, "delta ===== " + delta);
mScroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta)*2);
mCurScreen = whichScreen;
Log.i(TAG, "mCurScreen = whichScreen ===== " + mCurScreen );
// 这里监听是为了在滑动的时候能改变的button的图�?
if (mOnCurrentViewChangedListener != null) {
mOnCurrentViewChangedListener.onCurrentViewChanged(this,mCurScreen);
}
invalidate(); // Redraw the layout
}
}
public void setToScreen(int whichScreen) {
Log.i(TAG, "whichScreen ==setToScreen== " + whichScreen);
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
mCurScreen = whichScreen;
scrollTo(whichScreen*getWidth(), 0);
// TODO 在这里做监听主要是为了点击button的时候,能够把当前的mCurScreen传给mOnCurrentViewChangedListener,使得达到改变图片的状
if (mOnCurrentViewChangedListener != null) {
mOnCurrentViewChangedListener.onCurrentViewChanged(this,mCurScreen);
}
}
public int getCurScreen() {
return mCurScreen;
}
@Override
public void computeScroll() {
// TODO Auto-generated method stub
Log.i(TAG, "computeScroll()");
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "event down!");
if (!mScroller.isFinished()){
mScroller.abortAnimation();
}
mLastMotionX = x;
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int)(mLastMotionX - x);
mLastMotionX = x;
Log.v(TAG, "event ACTION_MOVE");
scrollBy(deltaX, 0);
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "event : up");
// if (mTouchState == TOUCH_STATE_SCROLLING) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
int velocityX = (int) velocityTracker.getXVelocity(); // 滑动的速度,左边为负,右边为正
Log.e(TAG, "velocityX:"+velocityX);
if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
// Fling enough to move left
Log.e(TAG, "snap 向右滑动");
snapToScreen(mCurScreen - 1);
} else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) {
// Fling enough to move right,
Log.e(TAG, "snap 向左滑动");
snapToScreen(mCurScreen + 1);
} else {
snapToDestination();
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
// }
mTouchState = TOUCH_STATE_REST;
break;
case MotionEvent.ACTION_CANCEL:
mTouchState = TOUCH_STATE_REST;
break;
}
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
Log.e(TAG, "onInterceptTouchEvent-slop:"+mTouchSlop);
final int action = ev.getAction();
Log.i(TAG, "action ==== " + action);
if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
return true;
}
final float x = ev.getX();
final float y = ev.getY();
switch (action) {
case MotionEvent.ACTION_MOVE:
final int xDiff = (int)Math.abs(mLastMotionX-x);
if (xDiff>mTouchSlop) {
Log.i(TAG, "MotionEvent.ACTION_MOVE");
mTouchState = TOUCH_STATE_SCROLLING;
}
break;
case MotionEvent.ACTION_DOWN:
Log.v(TAG, "MotionEvent.ACTION_DOWN");
mLastMotionX = x;
mLastMotionY = y;
mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
Log.e(TAG, "MotionEvent.ACTION_UP");
mTouchState = TOUCH_STATE_REST;
break;
}
return mTouchState != TOUCH_STATE_REST;
}
}
主页面:
package com.dianxing.dialog;
import com.dianxing.dialog.ScrollLayout.OnCurrentViewChangedListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MeMainActivity extends Activity {
private LinearLayout layout;
private ScrollLayout scrollLayout;
private ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.memain, null);
scrollLayout = (ScrollLayout) layout.findViewById(R.id.scroll_privage);
scrollLayout.setmOnCurrentViewChangedListener(mOnCurrentViewChangedListener);
getLayout();
setContentView(layout);
}
private void getLayout(){
// TODO 添加布局页面
final LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
for(int i=0; i < 10 ; i++){
Log.i("Main", i + "");
final View view = layoutInflater.inflate(R.layout.meitem, null);
final LinearLayout item_fisrtLayout = (LinearLayout) view.findViewById(R.id.item_fisrtLayout);
TextView textView = (TextView) view.findViewById(R.id.tv);
ImageView iamgeViewLeft = (ImageView) view.findViewById(R.id.iamgeViewLeft);
ImageView iamgeViewRight = (ImageView) view.findViewById(R.id.iamgeViewRight);
iamgeViewLeft.setImageDrawable(getResources().getDrawable(R.drawable.qwe));
iamgeViewRight.setImageDrawable(getResources().getDrawable(R.drawable.awq));
scrollLayout.addView(item_fisrtLayout);
// TODO 添加按钮
final LinearLayout imageButtonLayout = (LinearLayout) layout.findViewById(R.id.imageButtonLayout);
final View viewButton = layoutInflater.inflate(R.layout.mebutton, null);
imageButton = (ImageButton) viewButton.findViewById(R.id.imageButton);
imageButtonLayout.addView(imageButton);
}
}
private OnCurrentViewChangedListener mOnCurrentViewChangedListener = new OnCurrentViewChangedListener() {
@Override
public void onCurrentViewChanged(View view, int currentview) {
Log.i("Main", "currentview ==== " + currentview );
if(currentview == 0){
imageButton.setImageDrawable(getResources().getDrawable(R.drawable.mv_dot_light));
}
}
};
}
memain.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title_tv"
android:text="北京*东直门"
android:layout_marginTop="5dip"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:background="#999999"
android:orientation="vertical"
android:layout_marginTop="5dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.dianxing.dialog.ScrollLayout
android:id="@+id/scroll_privage"
android:layout_width="fill_parent"
android:layout_height="130dip">
</com.dianxing.dialog.ScrollLayout>
<LinearLayout
android:id="@+id/imageButtonLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginBottom="5dip"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</LinearLayout>
meitem.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/item_fisrtLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_marginLeft="20dip"
android:textSize="20sp"
android:paddingTop="5dip"
android:layout_width="wrap_content"
android:layout_height="30dip"
android:singleLine="true"
android:text="你可能喜欢的优惠.你可能喜欢的优惠。。。。"
android:ellipsize="end"
android:textColor="#F0F0"/>
<LinearLayout
android:orientation="horizontal"
android:gravity="center_horizontal"
android:paddingTop="5dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iamgeViewLeft"
android:layout_marginRight="5dip"
android:layout_width="135dip"
android:background="@drawable/qwe"
android:layout_height="90dip"
android:textColor="#F0F0"/>
<ImageView
android:id="@+id/iamgeViewRight"
android:layout_marginLeft="5dip"
android:layout_width="130dip"
android:layout_height="90dip"
android:background="@drawable/awq"
android:textColor="#F0F0"/>
</LinearLayout>
</LinearLayout>
mebutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:background="#00000000"
android:layout_height="wrap_content"
android:padding="3dip"
android:src="@drawable/mv_dot_light"/>