转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38656929

用手机淘宝浏览商品详情时,商品图片是放在后面的,在第一个ScrollView滚动到最底下时会有提示,继续拖动才干浏览图片。仿照这个效果写一个出来并不难,仅仅要定义一个Layout管理两个ScrollView即可了,当第一个ScrollView滑究竟部时,再次向上滑动进入第二个ScrollView。效果例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhvbmdrZWppbmd3YW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

须要注意的地方是:

1、假设是手动滑究竟部须要再次按下才干继续往下滑,自己主动滚动究竟部则不须要

2、在由上一个ScrollView滑动到下一个ScrollView的过程中多仅仅手指相继拖动也不会导致布局的剧变,也就是多个pointer的滑动不会导致move距离的剧变。

这个Layout的实现思路是:

在布局中放置两个ScrollView,并为其设置OnTouchListener。时刻推断ScrollView的滚动距离,一旦第一个ScrollView滚动究竟部,则标识改为可向上拖动。此时開始记录滑动距离mMoveLen,依据mMoveLen又一次layout两个ScrollView;同理,监听第二个ScrollView是否滚动到顶部,以往下拖动。

OK,明确了原理之后能够看代码了:

package com.jingchen.tbviewer;

import java.util.Timer;
import java.util.TimerTask; import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.ScrollView; /**
* 包括两个ScrollView的容器
*
* @author chenjing
*
*/
public class ScrollViewContainer extends RelativeLayout { /**
* 自己主动上滑
*/
public static final int AUTO_UP = 0;
/**
* 自己主动下滑
*/
public static final int AUTO_DOWN = 1;
/**
* 动画完毕
*/
public static final int DONE = 2;
/**
* 动画速度
*/
public static final float SPEED = 6.5f; private boolean isMeasured = false; /**
* 用于计算手滑动的速度
*/
private VelocityTracker vt; private int mViewHeight;
private int mViewWidth; private View topView;
private View bottomView; private boolean canPullDown;
private boolean canPullUp;
private int state = DONE; /**
* 记录当前展示的是哪个view。0是topView,1是bottomView
*/
private int mCurrentViewIndex = 0;
/**
* 手滑动距离,这个是控制布局的主要变量
*/
private float mMoveLen;
private MyTimer mTimer;
private float mLastY;
/**
* 用于控制是否变动布局的还有一个条件,mEvents==0时布局能够拖拽了,mEvents==-1时能够舍弃将要到来的第一个move事件,
* 这点是去除多点拖动剧变的关键
*/
private int mEvents; private Handler handler = new Handler() { @Override
public void handleMessage(Message msg) {
if (mMoveLen != 0) {
if (state == AUTO_UP) {
mMoveLen -= SPEED;
if (mMoveLen <= -mViewHeight) {
mMoveLen = -mViewHeight;
state = DONE;
mCurrentViewIndex = 1;
}
} else if (state == AUTO_DOWN) {
mMoveLen += SPEED;
if (mMoveLen >= 0) {
mMoveLen = 0;
state = DONE;
mCurrentViewIndex = 0;
}
} else {
mTimer.cancel();
}
}
requestLayout();
} }; public ScrollViewContainer(Context context) {
super(context);
init();
} public ScrollViewContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public ScrollViewContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init() {
mTimer = new MyTimer(handler);
} @Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
if (vt == null)
vt = VelocityTracker.obtain();
else
vt.clear();
mLastY = ev.getY();
vt.addMovement(ev);
mEvents = 0;
break;
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
// 多一仅仅手指按下或抬起时舍弃将要到来的第一个事件move。防止多点拖拽的bug
mEvents = -1;
break;
case MotionEvent.ACTION_MOVE:
vt.addMovement(ev);
if (canPullUp && mCurrentViewIndex == 0 && mEvents == 0) {
mMoveLen += (ev.getY() - mLastY);
// 防止上下越界
if (mMoveLen > 0) {
mMoveLen = 0;
mCurrentViewIndex = 0;
} else if (mMoveLen < -mViewHeight) {
mMoveLen = -mViewHeight;
mCurrentViewIndex = 1; }
if (mMoveLen < -8) {
// 防止事件冲突
ev.setAction(MotionEvent.ACTION_CANCEL);
}
} else if (canPullDown && mCurrentViewIndex == 1 && mEvents == 0) {
mMoveLen += (ev.getY() - mLastY);
// 防止上下越界
if (mMoveLen < -mViewHeight) {
mMoveLen = -mViewHeight;
mCurrentViewIndex = 1;
} else if (mMoveLen > 0) {
mMoveLen = 0;
mCurrentViewIndex = 0;
}
if (mMoveLen > 8 - mViewHeight) {
// 防止事件冲突
ev.setAction(MotionEvent.ACTION_CANCEL);
}
} else
mEvents++;
mLastY = ev.getY();
requestLayout();
break;
case MotionEvent.ACTION_UP:
mLastY = ev.getY();
vt.addMovement(ev);
vt.computeCurrentVelocity(700);
// 获取Y方向的速度
float mYV = vt.getYVelocity();
if (mMoveLen == 0 || mMoveLen == -mViewHeight)
break;
if (Math.abs(mYV) < 500) {
// 速度小于一定值的时候当作精巧释放,这时候两个View往哪移动取决于滑动的距离
if (mMoveLen <= -mViewHeight / 2) {
state = AUTO_UP;
} else if (mMoveLen > -mViewHeight / 2) {
state = AUTO_DOWN;
}
} else {
// 抬起手指时速度方向决定两个View往哪移动
if (mYV < 0)
state = AUTO_UP;
else
state = AUTO_DOWN;
}
mTimer.schedule(2);
try {
vt.recycle();
} catch (Exception e) {
e.printStackTrace();
}
break; }
super.dispatchTouchEvent(ev);
return true;
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
topView.layout(0, (int) mMoveLen, mViewWidth,
topView.getMeasuredHeight() + (int) mMoveLen);
bottomView.layout(0, topView.getMeasuredHeight() + (int) mMoveLen,
mViewWidth, topView.getMeasuredHeight() + (int) mMoveLen
+ bottomView.getMeasuredHeight());
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!isMeasured) {
isMeasured = true; mViewHeight = getMeasuredHeight();
mViewWidth = getMeasuredWidth(); topView = getChildAt(0);
bottomView = getChildAt(1); bottomView.setOnTouchListener(bottomViewTouchListener);
topView.setOnTouchListener(topViewTouchListener);
}
} private OnTouchListener topViewTouchListener = new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
ScrollView sv = (ScrollView) v;
if (sv.getScrollY() == (sv.getChildAt(0).getMeasuredHeight() - sv
.getMeasuredHeight()) && mCurrentViewIndex == 0)
canPullUp = true;
else
canPullUp = false;
return false;
}
};
private OnTouchListener bottomViewTouchListener = new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
ScrollView sv = (ScrollView) v;
if (sv.getScrollY() == 0 && mCurrentViewIndex == 1)
canPullDown = true;
else
canPullDown = false;
return false;
}
}; class MyTimer {
private Handler handler;
private Timer timer;
private MyTask mTask; public MyTimer(Handler handler) {
this.handler = handler;
timer = new Timer();
} public void schedule(long period) {
if (mTask != null) {
mTask.cancel();
mTask = null;
}
mTask = new MyTask(handler);
timer.schedule(mTask, 0, period);
} public void cancel() {
if (mTask != null) {
mTask.cancel();
mTask = null;
}
} class MyTask extends TimerTask {
private Handler handler; public MyTask(Handler handler) {
this.handler = handler;
} @Override
public void run() {
handler.obtainMessage().sendToTarget();
} }
} }

凝视写的非常清楚了,有几个关键点须要讲一下:

1、因为这里为两个ScrollView设置了OnTouchListener,所以在其它地方不能再设置了,否则就白搭了。

2、两个ScrollView的layout參数统一由mMoveLen决定。

3、变量mEvents有两个作用:一是防止手动滑究竟部或顶部时继续滑动而改变布局,必须再次按下才干继续滑动;二是在新的pointer down或up时把mEvents设置成-1能够舍弃将要到来的第一个move事件,防止mMoveLen出现剧变。为什么会出现剧变呢?由于如果一開始仅仅有一仅仅手指在滑动,记录的坐标值是这个pointer的事件坐标点,这时候还有一仅仅手指按下了导致事件又多了一个pointer,这时候到来的move事件的坐标可能就变成了新的pointer的坐标。这时计算与上一次坐标的差值就会出现剧变,变化的距离就是两个pointer间的距离。所以要把这个move事件舍弃掉,让mLastY值记录这个pointer的坐标再開始计算mMoveLen。

pointer
up的时候也一样。

理解了这几点。看起来就没什么难度了,代码量也非常小。

MainActivity的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <com.jingchen.tbviewer.ScrollViewContainer
android:layout_width="match_parent"
android:layout_height="match_parent" > <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" > <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <LinearLayout
android:id="@+id/imagesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/h" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/i" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/j" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/k" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/l" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/m" />
</LinearLayout> <TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@id/imagesLayout"
android:background="#eeeeee"
android:gravity="center"
android:text="继续拖动,查看很多其它美女"
android:textSize="20sp" />
</RelativeLayout>
</ScrollView> <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/a" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/b" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/c" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/d" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/e" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/f" /> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/g" />
</LinearLayout>
</ScrollView>
</com.jingchen.tbviewer.ScrollViewContainer> </RelativeLayout>

在ScrollView中放了几张图片而已。

MainActivity的代码:

package com.jingchen.tbviewer;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu; public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

啥也没有......

好了,到此结束~

源代码下载

Android自己定义控件实战——仿淘宝商品浏览界面的更多相关文章

  1. Android自定义控件实战——仿淘宝商品浏览界面

    转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38656929 用手机淘宝浏览商品详情时,商品图片是放在后面的,在第一个Scr ...

  2. Android自己定义控件系列五:自己定义绚丽水波纹效果

    尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...

  3. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

  4. android 自己定义控件

    Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...

  5. Android自己定义控件皮肤

    Android自己定义控件皮肤 对于Android的自带控件,其外观仅仅能说中规中矩,而我们平时所示Android应用中,一个简单的button都做得十分美观.甚至于很多button在按下时的外观都有 ...

  6. android 自己定义控件属性(TypedArray以及attrs解释)

    近期在捣鼓android 自己定义控件属性,学到了TypedArray以及attrs.在这当中看了一篇大神博客Android 深入理解Android中的自己定义属性.我就更加深入学习力一番.我就沿着这 ...

  7. Vue实现仿淘宝商品详情属性选择的功能

    Vue实现仿淘宝商品详情属性选择的功能 先看下效果图:(同个属性内部单选,属性与属性之间可以多选) 主要实现过程: 所使用到的数据类型是(一个大数组里面嵌套了另一个数组)具体格式如下:   attrA ...

  8. Android自己定义控件之应用程序首页轮播图

    如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...

  9. Android自己定义控件(状态提示图表)

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重分享成果] 1 背景 前面分析那么多系统源代码了.也该暂停下来歇息一下,趁昨晚闲着看见一个有意思的需求就操 ...

随机推荐

  1. perl .*?和.*

    redis01:/root# cat x2.pl my $str="212121a19823a456123"; if ($str =~/.*a(.*)23/){print &quo ...

  2. 简单的ajax获取json

    一个DBhelper类,用来操作数据库 using System; using System.Collections.Generic; using System.Linq; using System. ...

  3. POJ 3017 单调队列dp

    Cut the Sequence Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8764   Accepted: 2576 ...

  4. jbpm部署流程定义到MySql报乱码解决方案

    问题起因: 我在使用ant将流程定义和流程相关资源部署到JBPM数据库中的时候,报了下面一个错误. 错误提示,大概是: 11:33:40,781 ERROR JDBCExceptionReporter ...

  5. 开源项目之Android 结束篇

    随着公司新的需求以及Android嵌入式的深入,我已经没有多余的时间去扩展学习与Sip或UI不相关的Android开源项目,至此结束! 感想:研究Android已经一年半载了,白天忙公司项目,晚上扩展 ...

  6. IDFA的值什么时候会发生改变

    在何种情况下 , 应用的IDFA值会发生改变? 近期工作中须要获得一个能够唯一地标示每个不同应用的ID,之前的苹果UDID已经不让使用了. 那么我们须要使用新的IDFA来引用.可是在某些情况下这个ID ...

  7. Android学习4、Android该Adapter

    一.Adapter介绍 An Adapter object acts as a bridge between an AdapterView and the underlying data for th ...

  8. VC调试篇

    难怪很多前辈说调试是一个程序员最基本的技能,其重要性甚至超过学习一门语言.不会调试的程序员就意味着他即使会一门语言,却不能编制出任何好的软件. 我以前接触的程序大多是有比较成形的思路和方法,调试起来出 ...

  9. Java输出当前的日期(年月日时分秒毫秒)

    package test.remote.tools.combine; import java.text.SimpleDateFormat; import java.util.Calendar; imp ...

  10. 利用jquery+iframe做一个ajax上传效果

    以下是自学it网--中级班上课笔记 网址:www.zixue.it html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict ...