recycleview的 item的自动居中以及自定义左对齐集代码分析
2017-01-26 22:23
381 查看
今天闲的无事写了这个 就当储备了 O(∩_∩)O哈哈~这里先说简单的 就是item的自动居中这个很简但就是 就是加两句话
// SnapHelper snapHelper = new LinearSnapHelper(); // snapHelper.attachToRecyclerView(my_recycle);//这里放置的是的recycleview这两句话就搞定了 这里源码的具体分析我这里就不做什么具体描述了简书上就有 我这里要说的是下一个需求自动左对齐 这里用原生的肯定不行就需要我们写个类继承LinearSnapHelper 然后对其进行处理这里我先提下这次比较重要的两个方法calculateDistanceToFinalSnap和findSnapView我们主要对这里进行了处理首先findSnapView
@Override public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) { //这里是个数组0返回的是横向的距离 1返回的是纵向的距离 默认的话你将每次都返回到第一个view int[] out = new int[2]; //这里判断你设置的recycleview的布局管理器方向是横向还是纵向 if (layoutManager.canScrollHorizontally()) { //然后将距离开始的距离存放在内 out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager)); } else { out[0] = 0; } if (layoutManager.canScrollVertically()) { out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager)); } else { out[1] = 0; } return out; }
//这里是获取开始显示的view方法
@Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) { if (layoutManager instanceof LinearLayoutManager) { if (layoutManager.canScrollHorizontally()) { return getStartView(layoutManager, getHorizontalHelper(layoutManager)); } else { return getStartView(layoutManager, getVerticalHelper(layoutManager)); } } return super.findSnapView(layoutManager); }
这里我们是根据他的滚动方向要进行一些处理 根据我们getstartview进行一些逻辑判断 来处理显示最左边的view是哪个 然后是我们的 calculateDistanceToFinalSnap计算到targetView要移动的距离
@Override public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) { //这里是个数组0返回的是横向的距离 1返回的是纵向的距离 默认的话你将每次都返回到第一个view int[] out = new int[2]; //这里判断你设置的recycleview的布局管理器方向是横向还是纵向 if (layoutManager.canScrollHorizontally()) { //然后将距离开始的距离存放在内 out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager)); } else { out[0] = 0; } if (layoutManager.canScrollVertically()) { out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager)); } else { out[1] = 0; } return out; }
这里我们是来计算我们的recycleview需要移动的距离 来保持左对齐 好了说了这些还是贴出源码吧
package com.example.admin.juzhongietm;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.LinearSnapHelper;import android.support.v7.widget.OrientationHelper;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.view.View;/*** Created by admin on 2017/1/26.*/public class StartSnapHelper extends LinearSnapHelper {private OrientationHelper mVerticalHelper, mHorizontalHelper;public StartSnapHelper() {}//这个是放入的被设置的recycleview'的@Overridepublic void attachToRecyclerView(@Nullable RecyclerView recyclerView)throws IllegalStateException {super.attachToRecyclerView(recyclerView);}/*** 计算到targetView要移动的距离 这个也是关键 这个方法是在我们判断处理之后(也就是findSnapView方法) 所以distanceToStart的方法中直接计算此时recycleview的离最屏幕开始处的距离减去内边距就是需要移动的距离*/@Override public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) { //这里是个数组0返回的是横向的距离 1返回的是纵向的距离 默认的话你将每次都返回到第一个view int[] out = new int[2]; //这里判断你设置的recycleview的布局管理器方向是横向还是纵向 if (layoutManager.canScrollHorizontally()) { //然后将距离开始的距离存放在内 out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager)); } else { out[0] = 0; } if (layoutManager.canScrollVertically()) { out[1] = distanceToStart(targetView, getVerticalHelper(layoutManager)); } else { out[1] = 0; } return out; }//这里是获取开始显示的view方法@Overridepublic View findSnapView(RecyclerView.LayoutManager layoutManager) { if (layoutManager instanceof LinearLayoutManager) { if (layoutManager.canScrollHorizontally()) { return getStartView(layoutManager, getHorizontalHelper(layoutManager)); } else { return getStartView(layoutManager, getVerticalHelper(layoutManager)); } } return super.findSnapView(layoutManager); }//这里是 计算距离开始的距离 由于calculateDistanceToFinalSnap的方法是在findSnapView之后所以这里计算的是recycleview的离最屏幕开始处的距离减去内边距就是需要移动的距离private int distanceToStart(View targetView, OrientationHelper helper) {Log.i("StartSnapHelper", "disanceToStart: "+helper.getDecoratedStart(targetView)+"========="+helper.getStartAfterPadding());return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();}//这里是我们自定义的右对齐的显示规则 也就是这里就是我实现自动最左对齐的关键 当然如果有其他的奇葩右对齐 -- (什么奇葩客户没见过)实现原理一样的private View getStartView(RecyclerView.LayoutManager layoutManager,OrientationHelper helper) {//这里返回null表示不做任何操作保持当前的位置if (layoutManager instanceof LinearLayoutManager) {Log.i("StartSnapHelper", "getStartView: "+1);int firstChild = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();boolean isLastItem = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()== layoutManager.getItemCount() - 1;//当发现显示了最后一个item的时候不执行任何操作if (firstChild == RecyclerView.NO_POSITION || isLastItem) {Log.i("StartSnapHelper", "getStartView: "+2);return null;}View child = layoutManager.findViewByPosition(firstChild);Log.i("StartSnapHelper11", "getStartView: "+helper.getDecoratedEnd(child)+ "------------"+helper.getDecoratedMeasurement(child));//当发现当前第一个view最后到结束位置到屏幕距离大于item的一半的时候自动复位 或者用getDecoratedStart()判断条件反过来而已if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2&& helper.getDecoratedEnd(child) > 0) {Log.i("StartSnapHelper", "getStartView: "+3);return child;} else {//如果发现最后停留的位置小于item自身的一半 则再次判断是不是最后一个view 如果是将不执行任何操作if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition()== layoutManager.getItemCount() - 1) {Log.i("StartSnapHelper", "getStartView: "+4);return null;} else {//如果不是返回下一个viewLog.i("StartSnapHelper", "getStartView: "+5);return layoutManager.findViewByPosition(firstChild + 1);}}}return super.findSnapView(layoutManager);}private OrientationHelper getVerticalHelper(RecyclerView.LayoutManager layoutManager) {if (mVerticalHelper == null) {mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager);}return mVerticalHelper;}private OrientationHelper getHorizontalHelper(RecyclerView.LayoutManager layoutManager) {if (mHorizontalHelper == null) {mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);}return mHorizontalHelper;}}
上面我应该是给了足够多的注释了 这里要注意的是我们这么处理是因为 由于calculateDistanceToFinalSnap的方法是在findSnapView之后
所以我们思路基本上就是根据当前item的第一的移动位置和本身的长度进行对比 进行一系列的判断 来确定的最靠左的是哪个item 然后我们的计算移动
距离的calculateDistanceToFinalSnap方法就可以根据我们findSnapView返回的view计算出实现左对其的距离了
O(∩_∩)O哈哈~今天就说这么多 希望能对你有所帮助
相关文章推荐
- 自定义RecycleView实现TV应用上的item焦点获取以及设置当前选中的item居中
- 自定义RecycleView实现TV应用上的item焦点获取以及设置当前选中的item居中
- 操盘机器人新增板块资金流向分析模型,可自定义板块、分析周期以及系统自动学习功能
- UIScrollView,UITableView,UITextView自动滚动代码分析
- 安卓复杂滑动案例 自定义behavior源码分析 实现头布局图片的缩放透明度变化,RecycleView的滑动布局,坐标变化
- 操盘机器人新增板块资金流向分析模型,可自定义板块、分析周期以及系统自动学习功能
- Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析
- (二十八)RecyclerView ItemTouchHelper 源码分析以及拓展
- 在xib和story board上实现代码自动布局(解决添加约束时,大于等于小于等于繁琐的问题,以及占位view的问题)
- 安卓复杂滑动案例 自定义behavior源码分析 实现头布局图片的缩放透明度变化,RecycleView的滑动布局,坐标变化
- 如何自定义RecycleView item的间距
- android-进阶(3)-自定义view(2)-Android中View绘制流程以及相关方法的分析
- RecycleView的通用适配器BaseQuickAdapter处理item中子控件的点击事件以及cardview卡片阴影
- 如何居中对齐一个UICollectionView的 item (虽然略看了一下,但是代码是有效果的)
- Android RecyclerView的Item自定义动画及DefaultItemAnimator源码分析
- 利用Recycleview水平平移并自动挪动Item位置(仿Instagram效果)
- ssm 自定义注解实现mybatis自动维护表结构以及利用freemarker生成代码
- 源码推荐(12.01B):一行代码搞定自动布局,自定义UICollectionViewFlowLayout
- Android自定义View(一)View绘制流程以及invalidate()等相关方法分析
- 自定义心型view,可自定义多种属性以及自动淡出动画效果