实现淘宝商品详情页面的viewPager滑动到最后一张图片跳转的功能
来源:互联网 发布:linux snmpv3配置 编辑:程序博客网 时间:2024/04/28 07:25
之前写过仿淘宝的自定义viewGroup (点我查看)
- 效果 请忽略渣渣转码
- 首先考虑滑动到最后一张要加载一张默认布局
在PagerAdapter的instantiateItem方法中判断如果是最后一张就显示默认布局
@Override public Object instantiateItem(ViewGroup container, int position) { if (position < imgUrls.size()) { ImageView imageView = new ImageView(container.getContext()); GlobalApplication.getApp().getImageLoader() .displayImage(imgUrls.get(position), imageView, GlobalApplication.getApp().getOptions()); container.addView(imageView); return imageView; } else { View hintView = LayoutInflater.from(container.getContext()) .inflate(R.layout.goods_top_viewpager_adapter_hint, container, false); imageView = (ImageView) hintView.findViewById(R.id.iv_hint); textView = (TextView) hintView.findViewById(R.id.tv_hint); container.addView(hintView); return hintView; } }
goods_top_viewpager_adapter_hint 的布局为
需要注意设置text的android:ems=”1”,这个属性是让字体每行显示一个
<LinearLayout 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" android:background="@color/bg_color" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/iv_hint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp6" android:src="@mipmap/arrows_left" tools:ignore="RtlHardcoded"/> <TextView android:id="@+id/tv_hint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp10" android:ems="1" android:text="继续滑动查看图文详情" tools:ignore="RtlHardcoded"/></LinearLayout>
ok到了这一步Ui基本实现了
- 实现滑动到最后一张的跳转功能
相信你马上会想到在OnPageChangeListener 里边去实现业务逻辑- 首先在构造函数里边传递2个值
第一个参数是判断能否跳转
第二个参数是跳转执行的线程
- 首先在构造函数里边传递2个值
/** * @param lastIndex 最后一页的索引 * @param command 事件触发时你想要做的事情 */ public LastPageJumpListener(int lastIndex, Runnable command) { this.lastIndex = lastIndex; this.command = command; }
2.在下边这个函数里 ,如果手指离开屏幕并且当前item是最后一个就让canJump=true
@Override public void onPageScrollStateChanged(int state) { // 判断是否是划动状态且是最后一页 canJump = (state == ViewPager.SCROLL_STATE_SETTLING && curPosition == lastIndex); }
3.在这里设置最后一个页面全部显示出来也可以跳转
@Override public void onPageSelected(int position) { curPosition = position; //最后一个页面选中也能跳转 if (position == lastIndex + 1) { command.run(); canJump = false; } }
4.接着在onPageScrolled里去通过positionOffset(0~1之间)去设置能否开始执行跳转,当然也可以用positionOffsetPixels去判断
//执行旋转动画开始 下边的代码是控制箭头旋转动画 需要注意的是:当第一个旋转未完成前让第二个不能执行动画
@Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { //当最后一页往后划动的时候触发该事件 if (position == lastIndex) { if (positionOffset > 0.3) { if (canJump) { command.run(); // 事件执行一次后进行重置,避免事件多次触发 canJump = false; } //执行旋转动画开始 if (isObjAnmatitor) { isObjAnmatitor = false; ObjectAnimator animator = ObjectAnimator.ofFloat(goodsTopPagerAdapter.imageView, "rotation", 0f, 180f); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); goodsTopPagerAdapter.textView.setText("释放滑动查看图文详情"); isObjAnmatitor2 = true; } }); animator.setDuration(800).start(); } } else if (positionOffset <= 0.3 && positionOffset > 0) { if (isObjAnmatitor2) { isObjAnmatitor2 = false; ObjectAnimator animator = ObjectAnimator.ofFloat(goodsTopPagerAdapter.imageView, "rotation", 180f, 360f); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); goodsTopPagerAdapter.textView.setText("继续滑动查看图文详情"); isObjAnmatitor = true; } }); animator.setDuration(800).start(); } } //执行旋转动画结束 } }
ok到这里一个防淘宝的功能就实现了吗?
5.最后需要注意在使用的时候,当执行跳转后要设置当前的item为你的图片的最后一张(既隐藏默认的加载item)
mViewPager.addOnPageChangeListener(new LastPageJumpListener(goodsTopPagerAdapter.getCount() - 2, new Runnable() { @Override public void run() { showListener.fragmentShow(); mViewPager.setCurrentItem(goodsTopPagerAdapter.getCount() - 2); } }));
4 0
- 实现淘宝商品详情页面的viewPager滑动到最后一张图片跳转的功能
- android仿淘宝详情页面viewPager滑动到最后一张图片跳转的功能
- Android仿淘宝详情页面viewPager滑动到最后一张图片跳转的功能
- 天猫、淘宝、京东商品详情的图片手动滑动ViewPager
- 仿淘宝Banner:左右滑动ViewPager+最后一张滑动查看详情
- 引导页ViewPager当滑动到最后一页,再继续滑动切换页面的效果实现
- Android中仿淘宝商品详情ViewPager页面数据手动滑动
- ios 类似淘宝商品详情页面的效果
- Android listView中点击item或Item中控件跳转对应的详情页面的实现(商品详情查看)
- iOS 类似淘宝商品详情查看翻页效果的实现
- 自定义ViewGroup实现仿淘宝的商品详情页
- android 自定义ViewGroup实现仿淘宝的商品详情页
- android 自定义ViewGroup实现仿淘宝的商品详情页
- android 自定义ViewGroup实现仿淘宝的商品详情页
- 使用URL SCHEME启动淘宝客户端并跳转到某个商品页面的方法
- ViewPager向右滑动到最后,再向右滑动实现界面跳转
- 仿京东、淘宝商品详情页上拉显示详情的嵌套ScrollView
- viewPager+photoView实现网络图片加载左右滑动+手势缩放功能+滑动到下一页其他页面恢复默认大小
- SQL命令和常用语句大全
- css属性继承和优先级
- Unity 图标字体
- About css sprites
- Angularjs分页指令
- 实现淘宝商品详情页面的viewPager滑动到最后一张图片跳转的功能
- MyBatis 学习总结(六)——一对多联表查询
- Python-Matplotlib安装及简单使用
- Dialog和Toast的示例大全
- XML
- ubuntu虚拟机连接手机设备出现 no permissions
- Android 平滑图片加载和缓存库 Glide 使用详解
- KMP算法的java实现
- 动态执行 javascript 函数(带参数)