android 仿淘宝、京东商品详情页 向上拖动查看图文详情控件
一、淘宝商品详情页效果
先看一下淘宝详情页的效果
二、实现思路
三、具体实现
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.baoyunlong.view.pulluptoloadmore.MainActivity">
- <com.baoyunlong.view.pulluptoloadmore.PullUpToLoadMore
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <com.baoyunlong.view.pulluptoloadmore.MyScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fillViewport="true">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <ImageView
- android:scaleType="fitXY"
- android:src="@drawable/a1"
- android:layout_width="match_parent"
- android:layout_height="180dp" />
- <TextView
- android:text="这里是标题"
- android:textSize="18dp"
- android:layout_marginRight="10dp"
- android:layout_marginLeft="10dp"
- android:layout_marginTop="10dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_marginTop="10dp"
- android:text="子标题"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:textSize="18dp"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- ..............
- <LinearLayout
- android:layout_height="0dp"
- android:layout_weight="1"
- android:gravity="bottom"
- android:layout_width="match_parent">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:height="50dp"
- android:background="#b11"
- android:gravity="center"
- android:text="继续拖动查看图文详情"
- android:textColor="#000" />
- </LinearLayout>
- </LinearLayout>
- </com.baoyunlong.view.pulluptoloadmore.MyScrollView>
- <com.baoyunlong.view.pulluptoloadmore.MyScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fillViewport="true">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/a1" />
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/a3" />
- .........
- </LinearLayout>
- </com.baoyunlong.view.pulluptoloadmore.MyScrollView>
- </com.baoyunlong.view.pulluptoloadmore.PullUpToLoadMore>
- </RelativeLayout>
代码:
- public class PullUpToLoadMore extends ViewGroup {
- public PullUpToLoadMore(Context context) {
- super(context);
- }
- public PullUpToLoadMore(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public PullUpToLoadMore(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- measureChildren(widthMeasureSpec, heightMeasureSpec);
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- int childCount = getChildCount();
- int childTop = t;
- for (int i = 0; i < childCount; i++) {
- View child = getChildAt(i);
- child.layout(l, childTop, r, childTop + child.getMeasuredHeight());
- childTop += child.getMeasuredHeight();
- }
- }
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- //防止子View禁止父view拦截事件
- this.requestDisallowInterceptTouchEvent(false);
- return super.dispatchTouchEvent(ev);
- }
(2)、监听ScrollView滑动事件的问题
- /**
- * Created by baoyunlong on 16/6/8.
- */
- public class MyScrollView extends ScrollView {
- private static String TAG=MyScrollView.class.getName();
- public void setScrollListener(ScrollListener scrollListener) {
- this.mScrollListener = scrollListener;
- }
- private ScrollListener mScrollListener;
- public MyScrollView(Context context) {
- super(context);
- }
- public MyScrollView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- }
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- switch (ev.getAction()){
- case MotionEvent.ACTION_MOVE:
- if(mScrollListener!=null){
- int contentHeight=getChildAt(0).getHeight();
- int scrollHeight=getHeight();
- int scrollY=getScrollY();
- mScrollListener.onScroll(scrollY);
- if(scrollY+scrollHeight>=contentHeight||contentHeight<=scrollHeight){
- mScrollListener.onScrollToBottom();
- }else {
- mScrollListener.notBottom();
- }
- if(scrollY==0){
- mScrollListener.onScrollToTop();
- }
- }
- break;
- }
- boolean result=super.onTouchEvent(ev);
- requestDisallowInterceptTouchEvent(false);
- return result;
- }
- public interface ScrollListener{
- void onScrollToBottom();
- void onScrollToTop();
- void onScroll(int scrollY);
- void notBottom();
- }
- /**
- * Created by baoyunlong on 16/6/8.
- */
- public class PullUpToLoadMore extends ViewGroup {
- public static String TAG = PullUpToLoadMore.class.getName();
- MyScrollView topScrollView, bottomScrollView;
- VelocityTracker velocityTracker = VelocityTracker.obtain();
- Scroller scroller = new Scroller(getContext());
- int currPosition = 0;
- int position1Y;
- int lastY;
- public int scaledTouchSlop;//最小滑动距离
- int speed = 200;
- boolean isIntercept;
- public boolean bottomScrollVIewIsInTop = false;
- public boolean topScrollViewIsBottom = false;
- public PullUpToLoadMore(Context context) {
- super(context);
- init();
- }
- public PullUpToLoadMore(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- public PullUpToLoadMore(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init();
- }
- private void init() {
- post(new Runnable() {
- @Override
- public void run() {
- topScrollView = (MyScrollView) getChildAt(0);
- bottomScrollView = (MyScrollView) getChildAt(1);
- topScrollView.setScrollListener(new MyScrollView.ScrollListener() {
- @Override
- public void onScrollToBottom() {
- topScrollViewIsBottom = true;
- }
- @Override
- public void onScrollToTop() {
- }
- @Override
- public void onScroll(int scrollY) {
- }
- @Override
- public void notBottom() {
- topScrollViewIsBottom = false;
- }
- });
- bottomScrollView.setScrollListener(new MyScrollView.ScrollListener() {
- @Override
- public void onScrollToBottom() {
- }
- @Override
- public void onScrollToTop() {
- }
- @Override
- public void onScroll(int scrollY) {
- if (scrollY == 0) {
- bottomScrollVIewIsInTop = true;
- } else {
- bottomScrollVIewIsInTop = false;
- }
- }
- @Override
- public void notBottom() {
- }
- });
- position1Y = topScrollView.getBottom();
- scaledTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
- }
- });
- }
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev) {
- //防止子View禁止父view拦截事件
- this.requestDisallowInterceptTouchEvent(false);
- return super.dispatchTouchEvent(ev);
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- int y = (int) ev.getY();
- switch (ev.getAction()) {
- case MotionEvent.ACTION_DOWN:
- lastY = y;
- break;
- case MotionEvent.ACTION_MOVE:
- //判断是否已经滚动到了底部
- if (topScrollViewIsBottom) {
- int dy = lastY - y;
- //判断是否是向上滑动和是否在第一屏
- if (dy > 0 && currPosition == 0) {
- if (dy >= scaledTouchSlop) {
- isIntercept = true;//拦截事件
- lastY=y;
- }
- }
- }
- if (bottomScrollVIewIsInTop) {
- int dy = lastY - y;
- //判断是否是向下滑动和是否在第二屏
- if (dy < 0 && currPosition == 1) {
- if (Math.abs(dy) >= scaledTouchSlop) {
- isIntercept = true;
- }
- }
- }
- break;
- }
- return isIntercept;
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- int y = (int) event.getY();
- velocityTracker.addMovement(event);
- switch (event.getAction()) {
- case MotionEvent.ACTION_MOVE:
- int dy = lastY - y;
- if (getScrollY() + dy < 0) {
- dy = getScrollY() + dy + Math.abs(getScrollY() + dy);
- }
- if (getScrollY() + dy + getHeight() > bottomScrollView.getBottom()) {
- dy = dy - (getScrollY() + dy - (bottomScrollView.getBottom() - getHeight()));
- }
- scrollBy(0, dy);
- break;
- case MotionEvent.ACTION_UP:
- isIntercept = false;
- velocityTracker.computeCurrentVelocity(1000);
- float yVelocity = velocityTracker.getYVelocity();
- if (currPosition == 0) {
- if (yVelocity < 0 && yVelocity < -speed) {
- smoothScroll(position1Y);
- currPosition = 1;
- } else {
- smoothScroll(0);
- }
- } else {
- if (yVelocity > 0 && yVelocity > speed) {
- smoothScroll(0);
- currPosition = 0;
- } else {
- smoothScroll(position1Y);
- }
- }
- break;
- }
- lastY = y;
- return true;
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- measureChildren(widthMeasureSpec, heightMeasureSpec);
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- int childCount = getChildCount();
- int childTop = t;
- for (int i = 0; i < childCount; i++) {
- View child = getChildAt(i);
- child.layout(l, childTop, r, childTop + child.getMeasuredHeight());
- childTop += child.getMeasuredHeight();
- }
- }
- //通过Scroller实现弹性滑动
- private void smoothScroll(int tartY) {
- int dy = tartY - getScrollY();
- scroller.startScroll(getScrollX(), getScrollY(), 0, dy);
- invalidate();
- }
- @Override
- public void computeScroll() {
- if (scroller.computeScrollOffset()) {
- scrollTo(scroller.getCurrX(), scroller.getCurrY());
- postInvalidate();
- }
- }
- }
android 仿淘宝、京东商品详情页 向上拖动查看图文详情控件相关推荐
- Android仿淘宝京东商品规格参数颜色筛选
Android 选择商品属性sku 最近项目中使用SKU属性查询,类似淘宝京东商品的选择,在网上查询了弄了几个源码看看,发现还是实现不了多属性选择问题,再原基础上改动相当费事,所以想干脆自己处理这个问 ...
- Android 仿淘宝京东商品详情页阻力翻页效果
原文链接:http://code.taobao.org/p/android-example/diff/46/trunk/%E5%95%86%E5%9F%8E%E8%AF%A6%E6%83%85/src ...
- Android 仿淘宝京东商品详情视频+图片与图片第一帧获取
近日项目有个新需求就是把原本的商品详情只有图片展示,改为视频+图片方式展示. 此博客只提供记录,与思路具体根据自己需求实现.首先想到的是Google搜索下别人的实现方式来参考实现发现不怎么适合项目需求 ...
- 仿淘宝京东商品规格属性选择的最简单实现
仿淘宝京东商品规格属性选择的最简单实现 商城里面的规格选择,网上大部分是自定义控件实现的,显得很是麻烦,而我的实现方式是大家最常用的控件RecyclerView,特点是性能好,简单.废话不多说,先看实 ...
- android向上拖动查看图文详情控件
摘自:http://blog.csdn.net/qifengdeqingchen/article/details/51659735 一.淘宝商品详情页效果 先看一下淘宝详情页的效果 我们的效果 二.实 ...
- Android 仿淘宝京东等我的订单界面及任意列表拓展
概述 目前像淘宝及展示列表等都有多个item展示的需求,可能大多数如果没做过,第一眼就是ListView去嵌套ListView,虽然这样是可以完成,但是这样做会导致手机过度绘制,为什么呢?因为当一个I ...
- vue仿淘宝京东商品多条件筛选(vue实现)
<template><div id="warp">你选择的是:<mark v-for="(item,index) in arr"& ...
- Android 仿淘宝商品详情页下拉足迹Demo
DropDownMultiPager 仿淘宝等商品详情页下拉足迹效果SimpleDemo 可colne之后看MainActivity的调用,方便二次开发 依赖 compile 'com.nineold ...
- Android仿淘宝、京东Banner滑动查看图文详情
文章目录 写在前面 效果图 原理分析 核心代码 源码地址 写在前面 本文基于 ViewPager2 实现的 Banner 效果,进而实现了仿淘宝.京东Banner滑动至最后一页时继续滑动来查看图文详情 ...
最新文章
- 基于Nginx实现10万+并发,你应该做的Linux内核优化
- linux安装hadoop记录
- CG CTF WEB 起名字真难
- android sharedUserId 共享用户
- php 微信转账,php实现微信公众号企业转账功能
- mysql 提交乱码问题_mysql数据库乱码问题
- 【科普】联邦知识蒸馏概述与思考
- CCF 201403-5 任务调度
- java树广度优先_如何在功能上生成树广度优先 . (使用Haskell)
- 软件测试 PreDay 决策表
- python识别文字坐标_python识别图片上的文字并返回文字在图片中的坐标
- JAVA第一次授课心得_关于第一次java课的感想
- 分享一款光彩四射的CSS3按钮集合
- android点击按钮执行adb命令,Android 按键事件及adb命令模拟
- LogLog Counting
- 图像的transformation与registration
- 2022-2028全球与中国健康资讯交换(HIE)市场现状及未来发展趋势
- Python|用turtle画笔画爱心
- 弘辽科技:拼多多活动价格建议价格太低了怎么办?
- 电脑文件夹拒绝访问,如何解决?