您的位置:首页 > 编程语言

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哈哈~今天就说这么多 希望能对你有所帮助

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐