# RecyclerViewDemo **Repository Path**: lzbgit/RecyclerViewDemo ## Basic Information - **Project Name**: RecyclerViewDemo - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2018-07-27 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # RecyclerViewDemo #### 项目介绍 RecyclerView使用案例 #### 软件架构 软件架构说明 ## 一 下拉刷新上拉加载的RecycleView 1. 重写两个方法 onInterceptTouchEvent
> (1). 往上滑动 且firstCompletelyVisibleItemPosition == 0, 那么拦截事件,禁止向下分发,交给该组件的onTouchEvent方法进行处理
> (2). 往x下滑动,拦截事件,禁止向下分发,交给该组件的onTouchEvent方法进行处理
onTouchEvent
> (1). HeaderView 显示不全,释放则滚回原来的位置
> (2). HeaderView 显示全,释放则进行刷新,回滚到HeaderView高度的位置,刷新完成,则再回滚HeaderView的高度
2.方案对比 (1).https://blog.csdn.net/q714093365/article/details/77063084
(2).https://www.jianshu.com/p/34b78dae9f57
3.inflater使用说明 ```` public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) { synchronized (mConstructorArgs) { Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate"); final Context inflaterContext = mContext; final AttributeSet attrs = Xml.asAttributeSet(parser); Context lastContext = (Context) mConstructorArgs[0]; mConstructorArgs[0] = inflaterContext; View result = root; try { // Look for the root node. int type; //第一步:循环查找root标签 while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Empty } //循环结束,没有找到root标签,抛出异常 if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!"); } //获取标签名 final String name = parser.getName(); if (DEBUG) { System.out.println("**************************"); System.out.println("Creating root view: " + name); System.out.println("**************************"); } // 第二步:判断是否此标签是否为merge,如果是的调用rInflate进一步解析,如果不是的就调用createViewFromTag创建一个View if (TAG_MERGE.equals(name)) { if (root == null || !attachToRoot) { throw new InflateException(" can be used only with a valid " + "ViewGroup root and attachToRoot=true"); } rInflate(parser, root, inflaterContext, attrs, false); } else { // Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, inflaterContext, attrs); ViewGroup.LayoutParams params = null; if (root != null) { if (DEBUG) { System.out.println("Creating params from root: " + root); } // Create layout params that match root, if supplied params = root.generateLayoutParams(attrs); if (!attachToRoot) { // Set the layout params for temp if we are not // attaching. (If we are, we use addView, below) temp.setLayoutParams(params); } } if (DEBUG) { System.out.println("-----> start inflating children"); } //第三步:解析root的子View // Inflate all children under temp against its context. rInflateChildren(parser, temp, attrs, true); if (DEBUG) { System.out.println("-----> done inflating children"); } //第四步:调用addView函数,把整个xml资源布局添加到DecorView的id为Content的节点上。 // We are supposed to attach all the views we found (int temp) // to root. Do that now. if (root != null && attachToRoot) { root.addView(temp, params); } // Decide whether to return the root that was passed in or the // top view found in xml. if (root == null || !attachToRoot) { result = temp; } } } catch (XmlPullParserException e) { final InflateException ie = new InflateException(e.getMessage(), e); ie.setStackTrace(EMPTY_STACK_TRACE); throw ie; } catch (Exception e) { final InflateException ie = new InflateException(parser.getPositionDescription() + ": " + e.getMessage(), e); ie.setStackTrace(EMPTY_STACK_TRACE); throw ie; } finally { // Don't retain static reference on context. mConstructorArgs[0] = lastContext; mConstructorArgs[1] = null; Trace.traceEnd(Trace.TRACE_TAG_VIEW); } return result; } } ```` 4.Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY() >mScrollX:表示离视图起始位置的x水平方向的偏移量 > >mScrollY:表示离视图起始位置的y垂直方向的偏移量 > >分别通过getScrollX() 和getScrollY()方法获得。 > >注意:mScrollX和mScrollY指的并不是坐标,而是偏移量。 5. VelocityTracker介绍 ```` //1. 第一步创建 VelocityTracker mVelocityTracker = VelocityTracker.obtain(); //2. 将 MotionEvent 事件添加到 VelocityTracker 变量中去 mVelocityTracker.addMovement(event); //3. 计算 x、y 轴的速度, //第一个参数表明时间单位,1000 代表 1000 ms 也就是 1 s,计算 1 s 内滚动多少个像素,后面表示最大速度 mVelocityTracker.computeCurrentVelocity(1000,600.0f); //4. 获取速度 float xVelocity = mVelocityTracker.getXVelocity(); float yVelocity = mVelocityTracker.getYVelocity(); ```` 6.Scroller ```` 1、创建一个 Scroller,可以这样编写代码 Scroller mScroller = new Scroller(context); 或者 AccelerateInterpolator interpolator = new AccelerateInterpolator(1.2f); Scroller mScroller = new Scroller(context,interpolator); AccelerateDecelerateInterpolator //先加速后减速 AccelerateInterpolator //加速 AnticipateInterpolator //运动时先向反方向运动一定距离,再向正方向目标运动 BounceInterpolator //模拟弹球轨迹 DecelerateInterpolator // 减速 LinearInterpolator //匀速 2、Scroller 启动动画通过调用这个两个方法中的一个 public void startScroll(int startX, int startY, int dx, int dy) {} public void startScroll(int startX, int startY, int dx, int dy,int duration) {} 3、Scroller 的快速滚动功能 fling public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY) {} startX //开始滚动时 X 坐标 startY //开始滚动时 Y 坐标 velocityX //开始滚动时 X 方向的初始速度 velocityY //开始滚动时 Y 方向的初始速度 minX // 滚动过程,X 坐标不能小于这个数值 maxX //滚动过程,X 坐标不能大于这个值 minY //滚动过程,Y 坐标不能小于这个数值 maxY //滚动过程,Y 坐标不能大于这个数值 ```` #### 参与贡献 1.[Scroller、VelocityTracker介绍](https://blog.csdn.net/briblue/article/details/73441698)