`

自定义组件:ScrollLayout

阅读更多
import android.content.Context;
import android.graphics.Canvas;
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;

/**
 * 仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类
 * @author Yao.GUET
 * blog: http://blog.csdn.net/Yao_GUET
 * date: 2011-05-04
 */
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;

	public ScrollLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		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<childCount; 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 canmCurScreen 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) {
    	// get the valid layout page
    	whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
    	if (getScrollX() != (whichScreen*getWidth())) {
    		
    		final int delta = whichScreen*getWidth()-getScrollX();
    		mScroller.startScroll(getScrollX(), 0, 
    				delta, 0, Math.abs(delta)*2);
    		mCurScreen = whichScreen;
    		invalidate();		// Redraw the layout
    	}
    }
    
    public void setToScreen(int whichScreen) {
    	whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
    	mCurScreen = whichScreen;
    	scrollTo(whichScreen*getWidth(), 0);
    }
    
    public int getCurScreen() {
    	return mCurScreen;
    }
    
	@Override
	public void computeScroll() {
		// TODO Auto-generated method stub
		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;
			
            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 left");
                snapToScreen(mCurScreen - 1);   
            } else if (velocityX < -SNAP_VELOCITY   
                    && mCurScreen < getChildCount() - 1) {   
                // Fling enough to move right   
            	Log.e(TAG, "snap right");
                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();
		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) {
				mTouchState = TOUCH_STATE_SCROLLING;
				
			}
			break;
			
		case 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:
			mTouchState = TOUCH_STATE_REST;
			break;
		}
		
		return mTouchState != TOUCH_STATE_REST;
	}
	
}

import android.app.Activity;
import android.os.Bundle;

public class ScrollLayoutTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  >
  <TextView android:layout_width="fill_parent"
  	android:layout_height="wrap_content"
  	android:text="text text"
  	android:textColor="@android:color/white"
  />
  <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  >
  	<TextView android:layout_width="fill_parent"
  	android:layout_height="fill_parent"
  	android:text="ScrollLayout"
  	android:textColor="@android:color/white"
  	android:layout_weight="1"
  	/>
	  <com.yao_guet.test.ScrollLayout
	  android:id="@+id/ScrollLayoutTest"
	  android:layout_width="fill_parent"
	  android:layout_height="fill_parent"
	  android:layout_weight="1"
	  >
	
		<LinearLayout
		  android:background="#FF00"
		  android:layout_width="fill_parent"
		  android:layout_height="fill_parent"></LinearLayout>
		  
		<FrameLayout
		  android:background="#F0F0"
		  android:layout_width="fill_parent"
		  android:layout_height="fill_parent"></FrameLayout>
		  
		<FrameLayout
		  android:background="#F00F"
		  android:layout_width="fill_parent"
		  android:layout_height="fill_parent">
		  </FrameLayout>
		  
		<LinearLayout
		  android:background="#FF00"
		  android:layout_width="fill_parent"
		  android:layout_height="fill_parent">
		  <Button
		  	android:layout_width="wrap_content"
		  	android:layout_height="wrap_content"
		  	android:text="Button1" />
		  </LinearLayout>
		
		
		<LinearLayout
		  android:layout_width="wrap_content"
		  android:layout_height="wrap_content">
		  <Button
		  	android:layout_width="wrap_content"
		  	android:layout_height="wrap_content"
		  	android:text="Button2" />
		  </LinearLayout>
		</com.yao_guet.test.ScrollLayout>
	</LinearLayout>
</LinearLayout>

Android中使用GridView分页显示系统所安装的应用,支持拖动与手势滑动
http://blog.csdn.net/Yao_GUET/archive/2011/05/05/6397197.aspx
提取Launcher中的WorkSapce,可以左右滑动切换屏幕页面的类
http://blog.csdn.net/yao_guet/article/details/6393962
分享到:
评论
4 楼 悠闲的鱼102104 2014-10-31  
    
3 楼 llty 2011-11-23  
好......。
2 楼 sunjunliangsunjun 2011-10-29  
博主你好 ! 问题想请教一下   !  如何监听ScrollLayout 的滑动事件!根据图片的id处理不同的数据!
1 楼 pisota 2011-07-05  
这个不错,不过里边放TABHOST就有问题,不能切换TAB,只能显示一个TAB页里的内容,其他都是空白

相关推荐

    scrollLayout 实现viewparger左右滑动上下滑动功能

    本文将详细讲解如何通过自定义`ScrollLayout`来实现一个结合了两者功能的组件,使得用户既能左右滑动切换页面,也能上下滑动查看页面内的详细内容。这种设计常见于阅读应用或长内容展示场景。 首先,我们了解`...

    Android之ScrollLayout左右滑动效果实现

    在Android开发中,`ScrollLayout`通常指的是一个可滚动的布局容器,它可以包含多个子视图,并允许用户通过手势进行水平...通过这个过程,开发者可以学习到如何自定义Android UI组件,提升对Android系统底层机制的认识。

    ScrollLayout

    在实际开发中,ScrollLayout常常与其他组件结合使用,如ViewPager或Fragment,以实现更复杂的功能。例如,结合ViewPager可以创建一个可以滑动切换的Tab布局,每个Tab对应一个Fragment,这样既能保持界面清晰,又能...

    scrolllayout

    总结来说,"scrolllayout"在Android开发中涉及到HorizontalScrollView的使用,手势识别(GestureDetector)来处理滑动事件,以及可能的ViewPager2和自定义PageTransformer实现平滑过渡和无限循环的效果。这些技术...

    Android屏幕滑动源码

    首先,我们关注的是`ScrollLayout`这个文件,它很可能是自定义的一个布局组件,用于实现滑动切换的效果。在Android中,通常我们会用到`ViewPager`或者自定义的`ViewGroup`来完成类似的功能。`ViewPager`是Android ...

    Android-仿百度地图抽屉拖拽效果

    抽屉效果通常通过Android的SlidingPaneLayout或者NavigationView来实现,但在这个项目中,开发者可能自定义了一个名为ScrollLayout的组件来达到更灵活的控制和定制。ScrollLayout-master这个文件名暗示了它是项目的...

    android 左右滑动分页源码

    压缩包中的`ScrollLayout`可能是一个自定义布局类,扩展了`LinearLayout`或`FrameLayout`,并添加了滑动手势检测和页面切换逻辑。它可能包含一个`ViewPager`实例,或者直接实现了滑动逻辑。 4. **手势检测**: ...

    MyScroll.zip

    在这个案例中,`ScrollLayout`可能是开发者用来封装滑动抽屉逻辑的自定义组件,它可能继承自`LinearLayout`或`FrameLayout`等基础布局,并且添加了额外的滑动手势处理和动画效果。 描述中的“高德地图滑动效果”...

    android仿出行类悬浮在地图上嵌套滑动控件

    ScrollLayout是一种自定义的滚动布局,它允许在一个父布局中嵌套多个子视图,并且可以实现上下、左右的滑动效果。在这个项目中,ScrollLayout将被用来承载地图和其他UI元素,使得用户能够通过滑动来浏览内容。 1. *...

    安卓scrollLayout

    6. **事件监听**:可以通过`OnScrollChangeListener`监听`ScrollView`的滚动事件,实现自定义的功能,例如在滚动到底部时加载更多数据。 7. **滚动动画**:可以使用`ObjectAnimator`或`ValueAnimator`来创建滚动...

    AllAppList 实例

    4. 设置ScrollLayout:将ListView或RecyclerView添加到ScrollView中,使整个列表可滚动。注意,由于ScrollView已经包含了滚动机制,所以一般不建议在ScrollView内部使用可以滚动的组件(如ListView),因为这可能...

    自定义ViewGroup实现ViewPager的滑动效果(附源码下载)

    android:id="@+id/ScrollLayout" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; android:layout_width="fill_parent" android:layout_height="fill_parent" android:background...

    android首次运行滑动帮助实例(模仿微信)

    1. **使用自定义布局**:`ScrollLayout` 提示这是一个自定义的滚动布局,它允许开发者创建多页的滑动内容。在Android中,可以继承`ViewGroup`并重写`onMeasure()`和`onLayout()`方法来实现自定义布局。每个页面通常...

    android抽屉显示百度地图

    首先,我们需要理解`DrawerLayout`是Android系统提供的一个视图组件,它常用于实现滑动抽屉效果,通常用作导航菜单。抽屉可以从屏幕左侧或右侧滑出,为用户提供更多的操作选项。在Android Studio中,可以通过拖拽或...

    仿launcher

    - **自定义小部件**:开发可扩展的小部件接口,允许用户在主屏幕上添加各种功能组件。 - **动画效果**:为启动器的切换、拖放等操作添加平滑的过渡动画。 3. **LauncherDemo项目分析** "LauncherDemo"很可能是该...

    高仿小米launcher(ZAKER)跨屏拖动item.zip

    - `MiLaucherActivity$4$1.class`和`DragGrid$2.class`、`DragGrid$1.class`:这些是内部类,可能与事件监听器或特定功能的实现有关,例如监听拖放操作或自定义布局的滚动事件。 5. **适配器类**:`DateAdapter....

    oschina客户端源码解析[汇编].pdf

    对于UI的分析,oschina客户端的首页由多个组件构成,包括顶部的`main_header`、可滑动的`ScrollLayout`,以及底部菜单对应的四个页面(资讯、问答、动弹、我的空间)。每个部分都有相应的布局文件,如`main_header`...

    微信小程序使用scroll-view标签实现自动滑动到底部功能的实例代码

    在微信小程序开发中,`scroll-view` 是一个非常重要的组件,它允许开发者创建可滚动的内容区域,以处理超出屏幕范围的内容。本实例主要讲解如何利用 `scroll-view` 实现自动滑动到底部的功能,这对于聊天界面、评论...

Global site tag (gtag.js) - Google Analytics