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

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

需要注意的地方是:

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版高仿淘宝客户端源码V2.3

    android版高仿淘宝客户端源码V2.3,这个版本我已经更新到2.3了,源码也上传到源码天堂那里了,大家可以看一下吧,该应用实现了我们常用的购物功能了,也就是在手机上进行网购的流程的,如查看产品(浏 ...

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

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

  4. 仿淘宝商品详情页上拉弹出新ViewController

    新项目就要开始做了,里面有购物那块,就试着先把淘宝商品详情页的效果做了一下. 1.需求 1.第一次上拉时,A视图拉到一定距离将视图B从底部弹出,A视图也向上 2.显示B视图时下拉时,有刷新效果,之后将 ...

  5. android 数据重构(仿淘宝浏览记录,足迹)

    数据结构 ->数据重构 原因 处理这个数据的主要原因是,后台服务器返回的数据格式在ios那边因为其控件可以对数据进行分区显示,可以直接处理,而在android上我们显示控件就是listview, ...

  6. 第十二篇、OC_仿淘宝商品详情页的翻页

    // // GFBProductViewController.m // elmsc // // Created by MAC on 2016/11/26. // Copyright © 2016年 G ...

  7. 【原创smarty仿淘宝商品图片轮播+放大镜效果】

    1.去掉图片集字段,字符串的多余字符 $goods_pic_display=$row[DISPLAY];$goods_pic_display1=str_replace('"', '', $g ...

  8. HTML学习案例--仿淘宝商品信息

    步骤:1.布局分析 2.敲代码 考察知识点: 1.类选择器(素材第四天) 2.CSS关于display,padding,margin的应用 3.如何用div布局 总结: 如果想让一行有两组以上的字块, ...

  9. Android仿淘宝继续上拉进入商品详情页的效果,使用双Fragment动画切换;

    仿淘宝继续上拉进入商品详情页的效果,双Fragment实现: 动画效果: slide_above_in.xml <?xml version="1.0" encoding=&q ...

随机推荐

  1. 【转】Jmeter分布式压力测试

    安装 下载地址:http://jmeter.apache.org/download_jmeter.cgi 安装前提(因为jmeter依赖于Java所以必须先配置好java) 下载后解压: tar -x ...

  2. numpy得到数组的index

    itemindex = numpy.where(array==item)

  3. ROS :为IDE配置环境变量

    ROS hydro 自带安装好了opencv 2.4 为了在自己经常使用的开发环境Eric下调用,需要配置Eric的环境变量,好让它可以调用ROS的资源,当然你用其他IDE也要这样配置,配置好了环境变 ...

  4. 回调方法介绍之中国好室友篇(Java示例)

    前言 在Java社区的各种开源工具中,回调方法的使用俯拾即是.所以熟悉回调方法无疑能加速自己对开源轮子的掌握.网上搜了一些文章,奈何对回调方法的介绍大多只停留在什么是回调方法的程度上.本篇文章尝试从回 ...

  5. Spring 4 官方文档学习(十一)Web MVC 框架之Flash Attributes

    接上一篇中的重定向. http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-fl ...

  6. (转)MFC的ClistCtrl删除选中多行项目

    MFC的ClistCtrl控件添加了多行数据后,若要删除选中的多行数据,可以使用ClistCtrl的成员函数,在网上找了很多例子,发现都有问题,因为在删除ClistCtrl行的时候,删除行下面的行会上 ...

  7. R语言低级绘图函数-arrows

    arrows 函数用来在一张图表上添加箭头,只需要分别指定起始坐标和终止坐标,就可以添加箭头了,还可以通过一些属性对箭头的形状,大小进行调整 基本用法: xo, yo 指定起始点的x和y坐标,x1, ...

  8. Java动态代理-->Spring AOP

    引述要学习Spring框架的技术内幕,必须事先掌握一些基本的Java知识,正所谓“登高必自卑,涉远必自迩”.以下几项Java知识和Spring框架息息相关,不可不学(我将通过一个系列分别介绍这些Jav ...

  9. [ACM] poj 2017 Speed Limit

    Speed Limit Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17030   Accepted: 11950 Des ...

  10. NYOJ467 中缀式变后缀式 【栈】

    中缀式变后缀式 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 人们的日常习惯是把算术表达式写成中缀式,但对于机器来说更"习惯于"后缀式.关于算术 ...