当前位置: 首页 > news >正文

自己做网站 微信全球电子商务网站排名

自己做网站 微信,全球电子商务网站排名,门户网站的建设方案,企业响应式网站建设报价目录 一、滑动到指定位置#xff08;target position#xff09;并且置顶 1. RecycleView默认的几个实现方法及缺陷 2. 优化源码实现置顶方案 3. 其他实现置顶的方案 二、调整平移滑动调速 在实际项目里#xff0c;RecycleView 可以说是我们最常用到的组件#xff0c;…目录 一、滑动到指定位置target position并且置顶 1. RecycleView默认的几个实现方法及缺陷 2. 优化源码实现置顶方案 3. 其他实现置顶的方案 二、调整平移滑动调速 在实际项目里RecycleView 可以说是我们最常用到的组件作为绑定并展示LIST数据的组件经常需要实现平滑滚动到列表里的某个目标ITEM并且将其置顶在屏幕最上方而且在特殊情形下我们需要控制滑动速度来控制滚动的时长。 一、滑动到指定位置target position并且置顶 1. RecycleView默认的几个实现方法及缺陷 ((LinearLayoutManager)recycleView.getLayoutManager()).scrollToPositionWithOffset(int position, int offset); 如果你没有滑动过程动画的要求那上面这行代码将offset的值设置为0就一步到位地满足需求了。 recycleView.scrollToPosition(int position); recycleView.smoothScrollToPosition(int position); 以上两个方法遵循的是最少滑动原则只要target position那项item已经完全可见了就马上停止滑动要是target position已经可见了那根本不会滑动。所以按不同的滑动方向会出现不同的结果如果target position在屏幕可视范围的上方则它默认会将target position置顶反之target position在屏幕可视范围的下方则滚动完成后target postion会处于屏幕的最下方无法实现我们的置顶需求。所以缺陷很明显要么不动要么无法置顶。 2. 优化源码实现置顶方案 我们看下recycleview提供的方法的源代码看看是否可以进行改进 public void smoothScrollToPosition(int position) {if (mLayoutSuppressed) {return;}if (mLayout null) {Log.e(TAG, Cannot smooth scroll without a LayoutManager set. Call setLayoutManager with a non-null argument.);return;}mLayout.smoothScrollToPosition(this, mState, position);} 由代码可以看出RecyclerView的滑动方法是调用LayoutManager的smoothScrollToPosition方法 Overridepublic void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,int position) {LinearSmoothScroller linearSmoothScroller new LinearSmoothScroller(recyclerView.getContext());linearSmoothScroller.setTargetPosition(position);startSmoothScroll(linearSmoothScroller);} 其中LinearSmoothScroller提供了三个滑动策略 /*** Align child views left or top with parent views left or top** see #calculateDtToFit(int, int, int, int, int)* see #calculateDxToMakeVisible(android.view.View, int)* see #calculateDyToMakeVisible(android.view.View, int)*/public static final int SNAP_TO_START -1;/*** Align child views right or bottom with parent views right or bottom** see #calculateDtToFit(int, int, int, int, int)* see #calculateDxToMakeVisible(android.view.View, int)* see #calculateDyToMakeVisible(android.view.View, int)*/public static final int SNAP_TO_END 1;/*** pDecides if the child should be snapped from start or end, depending on where it* currently is in relation to its parent./p* pFor instance, if the view is virtually on the left of RecyclerView, using* {code SNAP_TO_ANY} is the same as using {code SNAP_TO_START}/p** see #calculateDtToFit(int, int, int, int, int)* see #calculateDxToMakeVisible(android.view.View, int)* see #calculateDyToMakeVisible(android.view.View, int)*/public static final int SNAP_TO_ANY 0; LinearSmoothScroller确定滑动方案的方法 /*** When scrolling towards a child view, this method defines whether we should align the top* or the bottom edge of the child with the parent RecyclerView.** return SNAP_TO_START, SNAP_TO_END or SNAP_TO_ANY; depending on the current target vector* see #SNAP_TO_START* see #SNAP_TO_END* see #SNAP_TO_ANY*/protected int getVerticalSnapPreference() {return mTargetVector null || mTargetVector.y 0 ? SNAP_TO_ANY :mTargetVector.y 0 ? SNAP_TO_END : SNAP_TO_START;} 重写LinearSmoothScroller的getVerticalSnapPreference方法 class LinearTopSmoothScroller extends LinearSmoothScroller {public LinearTopSmoothScroller(Context context) {super(context);}Overrideprotected int getVerticalSnapPreference() {return SNAP_TO_START;}} 这里为什么返回 SNAP_TO_START可以看到LinearSmoothScrollerl的方法calculateDtToFit()根据不同滚动策略获取到需要滚动的距离SNAP_TO_START是按置顶的方案来计算的。所以我们在getVerticalSnapPreference方法里固定返回SNAP_TO_START就可以实现目的。 public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, intsnapPreference) {switch (snapPreference) {case SNAP_TO_START:return boxStart - viewStart;case SNAP_TO_END:return boxEnd - viewEnd;case SNAP_TO_ANY:final int dtStart boxStart - viewStart;if (dtStart 0) {return dtStart;}final int dtEnd boxEnd - viewEnd;if (dtEnd 0) {return dtEnd;}break;default:throw new IllegalArgumentException(snap preference should be one of the constants defined in SmoothScroller, starting with SNAP_);}return 0;} 调用方式一 void scrollItemToTop(int position) {LinearSmoothScroller smoothScroller new LinearTopSmoothScroller(this);smoothScroller.setTargetPosition(position);linearLayoutManager.startSmoothScroll(smoothScroller);} 调用方式二 自定义一个类继承自 LinearLayoutManager private class TopLayoutManager extends LinearLayoutManager {public TopLayoutManager(Context context) {super(context);}public TopLayoutManager(Context context, int orientation, boolean reverseLayout) {super(context, orientation, reverseLayout);}Overridepublic void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {RecyclerView.SmoothScroller smoothScroller new LinearTopSmoothScroller(recyclerView.getContext());smoothScroller.setTargetPosition(position);startSmoothScroll(smoothScroller);}private class LinearTopSmoothScroller extends LinearSmoothScroller {public LinearTopSmoothScroller(Context context) {super(context);}Overrideprotected int getVerticalSnapPreference() {return SNAP_TO_START;}} } 调用 TopLayoutManager topLayoutManager new TopLayoutManager(this); recycleview.setLayoutManager(topLayoutManager); recycleview.smoothScrollToPosition(position); 3. 其他实现置顶的方案 可以参考这篇文章Android RecyclerView滚动定位 它主要解决的是滚动到屏幕下面ITEM无法置顶的问题思路是先用scrollToPosition将要置顶的项先移动显示出来然后计算这一项离顶部的距离用scrollBy完成最后的100米 这个方案还有个好处就是如果target position很远滑动距离很长也不会导致屏幕滚动过长的时间。向上向下动态滚动动画过程距离都不超过一个屏幕的距离。 如果希望target position在滚动结束后停留在屏幕中间可以参考下这篇文章 RecyclerView smoothScroll to position in the center. android 另外如果希望置顶后可以有一定的偏移量离顶部有一定距离可以参考这篇文章 RecyclerView的smooth scroller -- 诸多案例 二、调整平移滑动调速 同理可以在LinearSmoothScroller类找到决定滚动速度的方法并修改。 /*** Calculates the scroll speed.** param displayMetrics DisplayMetrics to be used for real dimension calculations* return The time (in ms) it should take for each pixel. For instance, if returned value is* 2 ms, it means scrolling 1000 pixels with LinearInterpolation should take 2 seconds.*/protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;} 上面的MILLISECONDS_PER_INCH的值为25F如果希望更快可以将值改小这个方法的返回值表示滚动一个像素需要的时间单位ms如果返回值为2ms表示滚动1000个像素需要花费2秒时长。 平滑滚动到target position【置顶调速】的调用方式 RecyclerView.SmoothScroller smoothScroller new LinearSmoothScroller(this) {Override protected int getVerticalSnapPreference() {return LinearSmoothScroller.SNAP_TO_START;}Overrideprotected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {//默认值是25F(MILLISECONDS_PER_INCH)值越小滑动速度越快,值越大则越慢return 100F / displayMetrics.densityDpi;}};smoothScroller.setTargetPosition(position);linearLayoutManager.startSmoothScroll(smoothScroller); 目前还有一个问题虽然我们可以调整速度但是这里始终是一个固定的滚动速度试想如果滚动的距离特别远仍然需要滚动很长的时间又或者滚动距离太近那么滚动动画一瞬间就结束了缺少了流畅感。 所以我们可以根据需要滚动的远或近来设置不同的滚动速度 RecyclerView.SmoothScroller smoothScroller new LinearSmoothScroller(this) {Override protected int getVerticalSnapPreference() {return LinearSmoothScroller.SNAP_TO_START;}Overrideprotected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {// 第一个可见位置int firstItem linearLayoutManager.findFirstVisibleItemPosition();int diff Math.abs(position - firstItem);// 将 diff 作分母滚动距离越远速度越快return 100F / displayMetrics.densityDpi * diff;}};
http://www.yutouwan.com/news/381311/

相关文章:

  • 网站开发课题开发背景主流建站开源程序有哪些
  • 如何将自己做的网站放到网上wordpress媒体库无法显示
  • 学校网站网页建设开题报告书网站建设需要具备的能力
  • 深圳网站制作要多少钱做网站公司多少钱
  • 页面设计制作网站源码莱西网站制作联赛与超
  • 网站宣传推广平台asp做的网站怎么运行
  • 个人网站备案 网站服务内容英文网站建设费用
  • 宁波本地网站排行意派h5制作平台
  • 配送网站开发景德镇网站制作公司
  • 找个美工做淘宝网站需要多少钱网站建设与运营课程
  • 手机网站横幅制作模板wordpress做分类信息网站
  • 商务网站建设流程步骤阿里云商业网站建设视频
  • 免费企业电话名录手机优化系统
  • 网站同时做竞价和seowordpress撤销更改
  • 租赁空间网站建设建设网站过程中
  • 推广型网站建设销售吉林省交通建设集团有限公司网站
  • 夏天做啥网站致富免费下载微信并安装
  • 网络营销发展的新趋势东莞百度推广优化
  • 怎样优化排名自己网站枣庄网站建设哪家公司好
  • 网站运营有前途吗wordpress 获取文章列表
  • 福州网站建设市场搜索引擎下载入口
  • 站内营销推广方案软件商店安装下载
  • 镇江网站建设介绍服务搜索引擎优化seo专员招聘
  • 网站建设个一般需要花费多少钱网站建设报表明细
  • 国外有趣的网站wordpress 猜你喜欢
  • 网站开发毕设开题报告怎么写实验建设网站 南京林业大学
  • 做一个网站后期维护需要多少钱网站 用户体验
  • 虹口手机网站制作城市建设理论研究官方网站
  • 城市建设学校网站管理规章制度公司如何做网站宣传
  • 有人知道网站怎么做吗wordpress模板优化