# ohos-drag-FlowLayout
**Repository Path**: HarmonyOS-tpc/ohos-drag-FlowLayout
## Basic Information
- **Project Name**: ohos-drag-FlowLayout
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2021-06-26
- **Last Updated**: 2023-04-17
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# ohos-drag-FowLayout
this is a draggable flow layout lib (可拖拽的流布局库).
## Download
Gradle:
```groovy
dependencies{
implementation 'io.openharmony.tpc.thirdlib:ohos-drag-FlowLayout:1.0.0'
}
```
# demo
## 特点
- 1, 类似可拖拽的多列ListContainer. 不过ListContainer 宽度/个数是固定的。 这个布局item宽度是不定的(放不下自动换行)。
- 2,长按item拖拽,如果要处理点击事件请调用。
```java
mDragflowLayout.setOnItemClickListener(new ClickToDeleteItemListenerImpl(ResourceTable.Id_iv_close){
@Override
protected void onDeleteSuccess(DragFlowLayout dfl, Component child, Object data) {
//删除成功后的处理。
}
});
```
- 3,可嵌套ScrollerView. demo就是。
- 4, 默认均可拖拽,如果想禁止某些Item拖拽请实现 {@link IDraggable} 接口 .
- 5, 支持预存储一定个数的item. 以避免频繁创建.
```java
//预存指定个数的Item. 这些Item会反复使用
mDragflowLayout.prepareItemsByCount(10);
```
- 6, 1.5.0 新增 拖拽状态监听器 和 view观察者。
```java
//设置拖拽状态监听器
mDragflowLayout.setOnDragStateChangeListener(new DragFlowLayout.OnDragStateChangeListener() {
@Override
public void onDragStateChange(DragFlowLayout dfl, int dragState) {
System.out.println("on drag state change : dragState = " + dragState);
}
});
//添加view观察者
mDragflowLayout.addViewObserver(new IViewObserver() {
@Override
public void onAddView(Component child, int index) {
// Logger.i(TAG, "onAddView", "index = " + index);
}
@Override
public void onRemoveView(Component child, int index) {
// Logger.i(TAG, "onRemoveView", "index = " + index);
}
});
```
## 使用步骤
- 1, 导入下面的gradle 配置。并在xml中添加配置
```xml
```
- 2,设置点击事件处理器 和 数据适配器.
```java
//用这个处理点击事件
mDragflowLayout.setOnItemClickListener(new ClickToDeleteItemListenerImpl(ResourceTable.Id_iv_close){
@Override
protected void onDeleteSuccess(DragFlowLayout dfl, Component child, Object data) {
//删除成功后的处理。
}
});
//DragAdapter 泛型参数就是为了每个Item绑定一个对应的数据。通常很可能是json转化过来的bean对象
mDragflowLayout.setDragAdapter(new DragAdapter() {
@Override
public int getItemLayoutId() {
return ResourceTable.Layout_item_drag_flow;
}
@Override
public void onBindData(Component itemView, int dragState, TestBean data) {
itemView.setTag(data);
Text tv = (Text) itemView.findComponentById(ResourceTable.Id_tv_text);
tv.setText(data.text);
//iv_close是关闭按钮。只有再非拖拽空闲的情况吓才显示
itemView.findComponentById(ResourceTable.Id_iv_close).setVisibility(
dragState!= DragFlowLayout.DRAG_STATE_IDLE
&& data.draggable ? Component.VISIBLE : Component.INVISIBLE);
}
@Override
public TestBean getData(Component itemView) {
return (TestBean) itemView.getTag();
}
});
```
- 3, item管理: 对item的增删改查-,即CRUD. 通过api: mDragflowLayout.getDragItemManager()。
即可得到DragItemManager.
- 4, 禁止个别Item拖拽。
```java
//数据实体实现IDraggable (是否可拖拽) 接口,并且 isDraggable 为false即可
private static class TestBean implements IDraggable{
String text;
boolean draggable = true;
public TestBean(String text) {
this.text = text;
}
@Override
public boolean isDraggable() {
return draggable;
}
}
```
## API说明
```java
//设置拖拽状态监听器
public void setOnDragStateChangeListener(OnDragStateChangeListener l)
//获取拖拽状态
public @DragState int getDragState()
//设置Item点击事件处理器
public void setOnItemClickListener(OnItemClickListener l)
//设置数据适配器
void setDragAdapter(DragAdapter adapter)
//获取Item管理器(方便CRUD-增删改查 item)
public DragItemManager getDragItemManager()
//设置全局是否可拖拽。如果false,则无法长按拖拽了。
public void setDraggable(boolean draggable)
//设置缓存component的个数。可避免重复创建item component
public void prepareItemsByCount(int count)
//标记拖拽开始,这个会使得拖拽状态变更为draggable. .
public void beginDrag();
//标记拖拽结束, 内部会自动将拖拽状态改为 DRAG_STATE_IDLE .
public void finishDrag();
```
## License
Copyright 2016
heaven7(donshine723@gmail.com)
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.