# MaterialScrollBar **Repository Path**: chinasoft4_ohos/MaterialScrollBar ## Basic Information - **Project Name**: MaterialScrollBar - **Description**: 易于使用的ohos库,可轻松实现滚动跟随效果 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2021-06-28 - **Last Updated**: 2022-06-12 ## Categories & Tags **Categories**: harmonyos-listview **Tags**: None ## README # MaterialScrollBar #### 项目介绍 - 项目名称:MaterialScrollBar - 所属系列:openharmony的第三方组件适配移植 - 功能:易于使用的ohos库,可轻松实现滚动跟随效果 - 项目移植状态:主功能完成 - 调用差异:无 - 开发版本:sdk6,DevEco Studio 2.2 Beta1 - 基线版本:Release 13.4.1 #### 效果演示 ![效果演示](./img/demo.gif) #### 安装教程 1、在项目根目录下的build.gradle文件中 ```gradle allprojects { repositories { maven { url 'https://s01.oss.sonatype.org/content/repositories/releases/' } } } ``` 2.在app模块的build.gradle文件中 ```gradle dependencies { implementation('com.gitee.chinasoft_ohos:MaterialScrollBar:1.0.0') ...... } ``` sdk6,DevEco Studio 2.2 Beta1下项目可直接运行, 如无法运行,删除项目.gradle,.idea,build,gradle,build.gradle文件, 并依据自己的版本创建新项目,将新项目的对应文件复制到根目录下 #### 使用说明 1.如何使用 创建一个MateriaScroller对象,并在布局中引用 ``` ``` 2.自定义气泡框,拖拽bar的类Indicator ``` /** * 定义滚动条 */ @Override public Component provideHandleView(ComponentContainer container) { handle = new Hand(getContext()); DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(60, 300); handle.setLayoutConfig(layoutConfig); handle.setMarginLeft(10); return handle; } * 定义气泡框 */ @Override public Component provideBubbleView(ComponentContainer container) { int radius = AttrHelper.vp2px(paddingDefault, getContext()); float[] radii = new float[]{ radius, radius, radius, radius, 0, 0, radius, radius }; ShapeElement shapeElement2 = new ShapeElement(getContext(), ResourceTable.Graphic_materia_default); shapeElement2.setCornerRadiiArray(radii); bubble = LayoutScatter.getInstance(getContext()) .parse(ResourceTable.Layout_fastscroll_defalut_bubble, container, false); bubble.setBackground(shapeElement2); bubble.setMarginLeft(15); return bubble; } ``` 3.MateriaScroller 定义属性 ``` handleColor = AttrValue.get(attrSet, "handleColor", new Color(STYLE_NONE)); handleComColor = AttrValue.get(attrSet, "handlecomColor", new Color(STYLE_NONE)); bubbleColor = AttrValue.get(attrSet, "bubbleColor", new Color(STYLE_NONE)); bubbleTextColor = AttrValue.get(attrSet, "bubbleTextColor", new Color(STYLE_NONE)); bubbleTextSize = AttrValue.getDimension(attrSet, "bubbleTextSize", STYLE_NONE); textStyle = AttrValue.get(attrSet, "textStyle", ""); ``` 处理滑动事件 ``` ** * 获取可滑动的总高度 * * @return 高度总滑动值 */ protected int getTotalScrollH() { return totalScrollH; } private void initHandleMovement() { handle.setTouchEventListener(new TouchEventListener() { @Override public boolean onTouchEvent(Component component, TouchEvent event) { MmiPoint mScreenPosition = event.getPointerScreenPosition(0); switch (event.getAction()) { case TouchEvent.PRIMARY_POINT_DOWN: scrollMax(); if (titleProvider != null) { viewProvider.onHandleGrabbed(); float offsetY = mScreenPosition.getY() - handle.getLocationOnScreen()[1]; offset = offsetY; return true; } break; case TouchEvent.POINT_MOVE: scrollMax(); isChangingPosition = true; float relativePos = getRelativeTouchPosition(event); if (relativePos > 1) relativePos = 1; else if (relativePos < 0) relativePos = 0; setScrolledPosition(relativePos); setRecyclerViewPosition(relativePos); ``` 4.滑动工具类ScrollingUtilities 控件在其整个屏幕上的坐标位置 ``` public static float getViewRawY(Component view) { int[] location = new int[2]; location[0] = 0; location[1] = ((Component) view.getComponentParent()).getLocationOnScreen()[1]; return location[1]; } ``` 获取最小值 ``` public static float getValueInRange(float min, float max, float value) { float minimum = Math.max(min, value); return Math.min(minimum, max); } ``` 5.属性工具类AttrValue ``` 读取 xml 属性值 public static T get(AttrSet attrSet, String key, T defValue) { if (!attrSet.getAttr(key).isPresent()) { return (T) defValue; } Attr attr = attrSet.getAttr(key).get(); if (defValue instanceof String) { return (T) attr.getStringValue(); } else if (defValue instanceof Long) { return (T) (Long) (attr.getLongValue()); } else if (defValue instanceof Float) { return (T) (Float) (attr.getFloatValue()); } else if (defValue instanceof Integer) { return (T) (Integer) (attr.getIntegerValue()); } else if (defValue instanceof Boolean) { return (T) (Boolean) (attr.getBoolValue()); } else if (defValue instanceof Color) { return (T) (attr.getColorValue()); } else if (defValue instanceof Element) { return (T) (attr.getElement()); } else { return (T) defValue; } ``` #### 测试信息 ``` CodeCheck代码测试无异常 CloudTest代码测试无异常 病毒安全检测通过 当前版本demo功能与原组件基本无差异 ``` #### 版本迭代 - 1.0.0 #### 版权和许可信息 Material Scroll Bar: ``` Copyright 2016-2017 Turing Technologies, an unincorporated orginisation of Wynne Plaga. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. This licensing is applicable to all code offered as part of this repository, which can be identified by the lisence notice preceding the content AND/OR by its inclusion in a package starting with "com. turingtechnologies.materialscrollbar". ``` RecyclerView-FastScroll: ``` Copyright (C) 2016 Tim Malseed Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` Launcher 3: ``` Copyright (C) 2010 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ```