1.开始一直反应不过来一个问题:RecycleView不是自带滚动效果吗?为啥子条目还不能全部滚动,显示出来呢?

意识到:只有当RecycleView 适配器中条目数量特别多,才可以滚动。

然而自己的布局,只是一个TextView内容特别多。

2.所以网上看到看到下面的帖子里面的布局。自己修改后,完成。

https://stackoverflow.com/questions/43753083/recycleview-using-view-holder-able-to-scroll-inside

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <android.support.v4.widget.NestedScrollView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content">
  8.  
  9. <android.support.v4.widget.SwipeRefreshLayout
  10. android:id="@+id/swipe_refresh_layout"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent">
  13.  
  14. <android.support.v7.widget.RecyclerView
  15. android:id="@+id/recycler_view"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"
  18. android:clipToPadding="false"
  19. android:scrollbarStyle="outsideOverlay"
  20. android:paddingBottom="28dp" />
  21. </android.support.v4.widget.SwipeRefreshLayout>
  22.  
  23. </android.support.v4.widget.NestedScrollView>
  24.  
  25. </LinearLayout>

二、存在的问题:记得是:左右滑动加上后,就无法上下滚动了。

后来切换了,采用的

  1. public class GallerySnapHelper extends SnapHelper {
  2. private static final float INVALID_DISTANCE = 1f;
  3. private static final float MILLISECONDS_PER_INCH = 40f;
  4. private OrientationHelper mHorizontalHelper;
  5. private RecyclerView mRecyclerView;
  6.  
  7. private OnSwipeListener mListener;
  8.  
  9. public OnSwipeListener getmListener() {
  10. return mListener;
  11. }
  12.  
  13. public void setmListener(OnSwipeListener mListener) {
  14. this.mListener = mListener;
  15. }
  16.  
  17. @Override
  18. public void attachToRecyclerView(@Nullable RecyclerView recyclerView) throws IllegalStateException {
  19. mRecyclerView = recyclerView;
  20. super.attachToRecyclerView(recyclerView);
  21. }
  22.  
  23. @Override
  24. public int[] calculateDistanceToFinalSnap(@NonNull RecyclerView.LayoutManager layoutManager, @NonNull View targetView) {
  25. int[] out = new int[2];
  26. if (layoutManager.canScrollHorizontally()) {
  27. out[0] = distanceToStart(targetView, getHorizontalHelper(layoutManager));
  28. } else {
  29. out[0] = 0;
  30. }
  31. return out;
  32. }
  33.  
  34. private int distanceToStart(View targetView, OrientationHelper helper) {
  35. return helper.getDecoratedStart(targetView) - helper.getStartAfterPadding();
  36. }
  37.  
  38. @Nullable
  39. protected LinearSmoothScroller createSnapScroller(final RecyclerView.LayoutManager layoutManager) {
  40. if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
  41. return null;
  42. }
  43. return new LinearSmoothScroller(mRecyclerView.getContext()) {
  44. @Override
  45. protected void onTargetFound(View targetView, RecyclerView.State state, Action action) {
  46. int[] snapDistances = calculateDistanceToFinalSnap(mRecyclerView.getLayoutManager(), targetView);
  47. final int dx = snapDistances[0];
  48. final int dy = snapDistances[1];
  49. final int time = calculateTimeForDeceleration(Math.max(Math.abs(dx), Math.abs(dy)));
  50. if (time > 0) {
  51. action.update(dx, dy, time, mDecelerateInterpolator);
  52. }
  53. }
  54.  
  55. @Override
  56. protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
  57. return MILLISECONDS_PER_INCH / displayMetrics.densityDpi;
  58. }
  59. };
  60. }
  61.  
  62. @Override
  63. public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
  64. LogUtil.d(MainActivity.TAG,"---velocityX:::"+velocityX+"----velocityY:::"+velocityY);
  65. if(velocityX>0) {
  66. if (!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)) {
  67. LogUtil.d(MainActivity.TAG, "-!(layoutManager instanceof RecyclerView.SmoothScroller.ScrollVectorProvider)---RecyclerView.NO_POSITION--");
  68. return RecyclerView.NO_POSITION;
  69. }
  70.  
  71. final int itemCount = layoutManager.getItemCount();
  72. if (itemCount == 0) {
  73. LogUtil.d(MainActivity.TAG, "-itemCount=0---RecyclerView.NO_POSITION--");
  74. return RecyclerView.NO_POSITION;
  75. }
  76.  
  77. final View currentView = findSnapView(layoutManager);
  78. if (currentView == null) {
  79. LogUtil.d(MainActivity.TAG, "-currentView = null---RecyclerView.NO_POSITION--");
  80. return RecyclerView.NO_POSITION;
  81. }
  82.  
  83. final int currentPosition = layoutManager.getPosition(currentView);
  84. if (currentPosition == RecyclerView.NO_POSITION) {
  85. LogUtil.d(MainActivity.TAG, "-currentPosition = RecyclerView.NO_POSITION---RecyclerView.NO_POSITION--");
  86. return RecyclerView.NO_POSITION;
  87. }
  88.  
  89. RecyclerView.SmoothScroller.ScrollVectorProvider vectorProvider =
  90. (RecyclerView.SmoothScroller.ScrollVectorProvider) layoutManager;
  91. // deltaJumps sign comes from the velocity which may not match the order of children in
  92. // the LayoutManager. To overcome this, we ask for a vector from the LayoutManager to
  93. // get the direction.
  94. PointF vectorForEnd = vectorProvider.computeScrollVectorForPosition(itemCount - 1);
  95. if (vectorForEnd == null) {
  96. LogUtil.d(MainActivity.TAG, "-vectorForEnd = null---RecyclerView.NO_POSITION--");
  97. // cannot get a vector for the given position.
  98. return RecyclerView.NO_POSITION;
  99. }
  100.  
  101. //在松手之后,列表最多只能滚多一屏的item数
  102. int deltaThreshold = layoutManager.getWidth() / getHorizontalHelper(layoutManager).getDecoratedMeasurement(currentView);
  103.  
  104. int hDeltaJump;
  105. if (layoutManager.canScrollHorizontally()) {
  106. LogUtil.d(MainActivity.TAG, "----layoutManager.canScrollHorizontally()-----" + layoutManager.canScrollHorizontally());
  107. hDeltaJump = estimateNextPositionDiffForFling(layoutManager,
  108. getHorizontalHelper(layoutManager), velocityX, 0);
  109.  
  110. if (hDeltaJump > deltaThreshold) {
  111. hDeltaJump = deltaThreshold;
  112. }
  113. if (hDeltaJump < -deltaThreshold) {
  114. hDeltaJump = -deltaThreshold;
  115. }
  116.  
  117. if (vectorForEnd.x < 0) {
  118. hDeltaJump = -hDeltaJump;
  119. }
  120. } else {
  121. Log.d(TAG, "----hDeltaJump = 0--layoutManager.canScrollHorizontally()---" + layoutManager.canScrollHorizontally());
  122. hDeltaJump = 0;
  123. }
  124.  
  125. if (mListener != null) {
  126. mListener.onSwipedClear();
  127. }
  128.  
  129. if (hDeltaJump == 0) {
  130. Log.d(TAG, "----if (hDeltaJump == 0)---true--");
  131. return RecyclerView.NO_POSITION;
  132. }
  133.  
  134. int targetPos = currentPosition + hDeltaJump;
  135. if (targetPos < 0) {
  136. targetPos = 0;
  137. }
  138. if (targetPos >= itemCount) {
  139. targetPos = itemCount - 1;
  140. }
  141. Log.d(TAG, "----targetPos-----" + targetPos);
  142. return targetPos;
  143. }else {
  144. //当判断是右滑时,不处理。
  145. Log.d(TAG, "-----“-1”-----");
  146. return -1;
  147. }
  148. }
  149.  
  150. @Override
  151. public View findSnapView(RecyclerView.LayoutManager layoutManager) {
  152. return findStartView(layoutManager, getHorizontalHelper(layoutManager));
  153. }
  154.  
  155. private View findStartView(RecyclerView.LayoutManager layoutManager, OrientationHelper helper) {
  156. if (layoutManager instanceof LinearLayoutManager) {
  157.  
  158. LogUtil.d(MainActivity.TAG, "-findStartView---layoutManager instanceof LinearLayoutManager--");
  159. int firstChildPosition = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
  160. if (firstChildPosition == RecyclerView.NO_POSITION) {
  161. LogUtil.d(MainActivity.TAG, "-findStartView---firstChildPosition == RecyclerView.NO_POSITION--");
  162. return null;
  163. }
  164.  
  165. /*
  166. 注释该段代码,否则不能正常滑动
  167. if (((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1) {
  168. ///0
  169. LogUtil.d(MainActivity.TAG, "-findStartView---layoutManager.getItemCount() - 1::::"+(layoutManager.getItemCount() - 1));
  170. LogUtil.d(MainActivity.TAG, "-findStartView---((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1--");
  171. return null;
  172. }*/
  173.  
  174. View firstChildView = layoutManager.findViewByPosition(firstChildPosition);
  175. if (helper.getDecoratedEnd(firstChildView) >= helper.getDecoratedMeasurement(firstChildView) / 2 && helper.getDecoratedEnd(firstChildView) > 0) {
  176. return firstChildView;
  177. } else {
  178. return layoutManager.findViewByPosition(firstChildPosition + 1);
  179. }
  180. } else {
  181. LogUtil.d(MainActivity.TAG, "-findStartView--!!!!!--layoutManager instanceof LinearLayoutManager--");
  182. return null;
  183. }
  184. }
  185.  
  186. private int estimateNextPositionDiffForFling(RecyclerView.LayoutManager layoutManager,
  187. OrientationHelper helper, int velocityX, int velocityY) {
  188. int[] distances = calculateScrollDistance(velocityX, velocityY);
  189. float distancePerChild = computeDistancePerChild(layoutManager, helper);
  190. if (distancePerChild <= 0) {
  191. return 0;
  192. }
  193. int distance = distances[0];
  194. if (distance > 0) {
  195. return (int) Math.floor(distance / distancePerChild);
  196. } else {
  197. return (int) Math.ceil(distance / distancePerChild);
  198. }
  199. }
  200.  
  201. private float computeDistancePerChild(RecyclerView.LayoutManager layoutManager,
  202. OrientationHelper helper) {
  203. View minPosView = null;
  204. View maxPosView = null;
  205. int minPos = Integer.MAX_VALUE;
  206. int maxPos = Integer.MIN_VALUE;
  207. int childCount = layoutManager.getChildCount();
  208. if (childCount == 0) {
  209. return INVALID_DISTANCE;
  210. }
  211.  
  212. for (int i = 0; i < childCount; i++) {
  213. View child = layoutManager.getChildAt(i);
  214. final int pos = layoutManager.getPosition(child);
  215. if (pos == RecyclerView.NO_POSITION) {
  216. continue;
  217. }
  218. if (pos < minPos) {
  219. minPos = pos;
  220. minPosView = child;
  221. }
  222. if (pos > maxPos) {
  223. maxPos = pos;
  224. maxPosView = child;
  225. }
  226. }
  227. if (minPosView == null || maxPosView == null) {
  228. return INVALID_DISTANCE;
  229. }
  230. int start = Math.min(helper.getDecoratedStart(minPosView),
  231. helper.getDecoratedStart(maxPosView));
  232. int end = Math.max(helper.getDecoratedEnd(minPosView),
  233. helper.getDecoratedEnd(maxPosView));
  234. int distance = end - start;
  235. if (distance == 0) {
  236. return INVALID_DISTANCE;
  237. }
  238. return 1f * distance / ((maxPos - minPos) + 1);
  239. }
  240.  
  241. private OrientationHelper getHorizontalHelper(RecyclerView.LayoutManager layoutManager) {
  242. if (mHorizontalHelper == null) {
  243. mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager);
  244. }
  245. return mHorizontalHelper;
  246. }
  247.  
  248. }
  1. public interface OnSwipeListener<T>{
  2. /**
  3. * 卡片还在滑动时回调
  4. *
  5. * @param viewHolder 该滑动卡片的viewHolder
  6. * @param ratio 滑动进度的比例
  7. * @param direction 卡片滑动的方向,CardConfig.SWIPING_LEFT 为向左滑,CardConfig.SWIPING_RIGHT 为向右滑,
  8. * CardConfig.SWIPING_NONE 为不偏左也不偏右
  9. */
  10. void onSwiping(RecyclerView.ViewHolder viewHolder, float ratio, int direction);
  11.  
  12. /**
  13. * 卡片完全滑出时回调
  14. *
  15. * @param viewHolder 该滑出卡片的viewHolder
  16. * @param t 该滑出卡片的数据
  17. * @param direction 卡片滑出的方向,CardConfig.SWIPED_LEFT 为左边滑出;CardConfig.SWIPED_RIGHT 为右边滑出
  18. */
  19. void onSwiped(RecyclerView.ViewHolder viewHolder, T t, int direction);
  20.  
  21. /**
  22. * 所有的卡片全部滑出时回调
  23. */
  24. void onSwipedClear();
  25.  
  26. }
  1. Activity 界面代码
    GallerySnapHelper mGallerySnapHelper = new GallerySnapHelper();
  2. mGallerySnapHelper.setmListener(new OnSwipeListener() {
  3. @Override
  4. public void onSwiping(RecyclerView.ViewHolder viewHolder, float ratio, int direction) {
  5.  
  6. }
  7.  
  8. @Override
  9. public void onSwiped(RecyclerView.ViewHolder viewHolder, Object o, int direction) {
  10.  
  11. }
  12.  
  13. @Override
  14. public void onSwipedClear() {
  15. Log.d(TAG,"-----onSwipedClear----");
  16. giRecyclerView.postDelayed(new Runnable() {
  17. @Override
  18. public void run() {
  19. groupIdeaHttp.getJtidea(URLCODE_GET_GROUP_IDEA,oaapi.getBASE_URL3());
  20. giRecyclerView.getAdapter().notifyDataSetChanged();
  21. }
  22. }, 100L);
  23. }
  24. });
  25.  
  26. mGallerySnapHelper.attachToRecyclerView(giRecyclerView);
  27. giRecyclerView.setAdapter(groupIdeaAdapter);

RecycleView 使用自定义CardLayouManager内容无法滚动问题的更多相关文章

  1. jquery鼠标移动div内容上下左右滚动

    jquery鼠标移动div内容上下左右滚动 点击这里查看效果:http://keleyi.com/keleyi/phtml/jqtexiao/9.htm <!DOCTYPE html PUBLI ...

  2. 第九篇 :微信公众平台开发实战Java版之如何实现自定义分享内容

    第一部分:微信JS-SDK介绍 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统 ...

  3. 微信js接口自定义分享内容

    最近客户有个要求,需要给网页添加微信分享功能,当然指的是用微信自带浏览器的时候,希望用户在最后一页点击分享的时候是分享的首页.曾经无意中看到过微信公众开发者平台提供了js接口,所以试着做了做,果然,跌 ...

  4. jQuery自定义插件--banner图滚动

    前言 jQuery是一个功能强大的库,提供了开发JavaScript项目所需的所有核心函数.很多时候我们使用jQuery的原因就是因为其使用插件的功能,然而,有时候我们还是需要使用自定义代码来扩展这些 ...

  5. 简单实现弹出弹框页面背景半透明灰,弹框内容可滚动原页面内容不可滚动的效果(JQuery)

    弹出弹框 效果展示 实现原理 html结构比较简单,即: <div>遮罩层 <div>弹框</div> </div> 先写覆盖显示窗口的遮罩层div.b ...

  6. CALayer 知识:创建带阴影效果的圆角图片图层和创建自定义绘画内容图层

    效果如下: KMLayerDelegate.h #import <UIKit/UIKit.h> @interface KMLayerDelegate : NSObject @end KML ...

  7. 滚动条ScrollViewer防止滚动时按内容跳跃式滚动的设置

    原文:滚动条ScrollViewer防止滚动时按内容跳跃式滚动的设置 属性中将CanContentScroll设置为False,滚动时就不会跳了,会连续的滚动

  8. js实现页面元素随着内容的滚动而滚动

      CreateTime--2017年9月4日16:55:06 Author:Marydon js实现页面元素随着内容的滚动而滚动 分析: CSS样式,使用绝对定位确定好页面元素在屏幕的位置(如:正中 ...

  9. webbrowser内容滚动(javascript内容无缝滚动)

    一 使用webbrowser现有方法 引用:https://blog.csdn.net/xiaokailele/article/details/48392673 public partial clas ...

随机推荐

  1. Webpack配置及使用

    ##如何将js模块化 ### module.exports() ### module.require() ### 自定义文件,进入时需要./ ### npm下载得到文件,不需要./ ##如果使用第三方 ...

  2. <转>如何用C++实现自动微分

    作者:李瞬生转摘链接:https://www.zhihu.com/question/48356514/answer/123290631来源:知乎著作权归作者所有. 实现 AD 有两种方式,函数重载与代 ...

  3. 新装Windows Server 2008 R2的设置

    原文链接:https://www.kafan.cn/edu/2898054.html 1.2008设置自动登录 具体方法:开始→运行→输入“rundll32 netplwiz.dll,UsersRun ...

  4. 【Social listening实操】作为一个合格的“增长黑客”,你还得重视外部数据的分析!

    本文转自知乎 作者:苏格兰折耳喵 ----------------------------------------------------- 在本文中,作者引出了"外部数据"这一概 ...

  5. 25.纯 CSS 创作一个慧星拖尾效果的 loader 动画

    原文地址:https://segmentfault.com/a/1190000014916281 简化地址:https://codepen.io/pen/?editors=1100 HTML代码: & ...

  6. .bat脚本基本命令语法 http://www.cnblogs.com/iTlijun/p/6137027.html

    这个是我找到的非常好的一篇文章了: 目录批处理的常见命令(未列举的命令还比较多,请查阅帮助信息)     1.REM 和 ::     2.ECHO 和 @     3.PAUSE     4.ERR ...

  7. 白鹭引擎 - 遮罩( Rectangle )

    1: 矩形遮罩 class Main extends egret.DisplayObjectContainer { /** * Main 类构造器, 初始化的时候自动执行, ( 子类的构造函数必须调用 ...

  8. Redis 常见配置

  9. leetcode990

    class Finder: def __init__(self): self.Parent = [i for i in range(26)] def union(self, p, q): self.P ...

  10. selenium报错

    Python 2.7.15 selenium 2.53.6 Firefox    47.0.1 pycharm 2017.3.7 # coding:utf-8 from selenium import ...