# SlideAndDragListView
**Repository Path**: HarmonyOS-tpc/SlideAndDragListView
## Basic Information
- **Project Name**: SlideAndDragListView
- **Description**: 这是一个自定义ListContaner控件,可以左右滑动菜单,上下拖拽改变item位置
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 4
- **Forks**: 1
- **Created**: 2021-04-15
- **Last Updated**: 2023-04-17
## Categories & Tags
**Categories**: harmonyos-listview
**Tags**: None
## README
# SlideAndDragListView
这是一个自定义ListContaner控件,可以左右滑动菜单,上下拖拽改变item位置。
## 概述
* 支持左右滑动显示菜单
* 支持长按或触摸上下拖拽改变item位置
* 左右滑动菜单以及上下拖拽效果没有原组件体验好
## 演示
## 集成
```
方式一:
通过library生成har包,添加har包到libs文件夹内
在entry的gradle内添加如下代码
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
方式二:
allprojects{
repositories{
mavenCentral()
}
}
implementation 'io.openharmony.tpc.thirdlib:SlideAndDragListView:1.0.0'
```
## entry运行要求
通过DevEco studio,并下载SDK
将项目中的build.gradle文件中dependencies→classpath版本改为对应的版本(即你的IDE新建项目中所用的版本)
### 示例
```xml
```
```java
/**
* 左右滑动菜单
*/
public class SlideDragListAbilitySlice extends AbilitySlice implements SlideAndDragListView.OnDragDropListener, SlideAndDragListView.OnSlideListener,
SlideAndDragListView.OnMenuItemClickListener, SlideAndDragListView.OnItemDeleteListener,
SlideAndDragListView.OnItemScrollBackListener {
private SlideAndDragListView mListView;
private Menu mMenu;
private ListItemEnity mDraggedEntity;
private ArrayList mAppList = new ArrayList<>();
private int menuWith = 150, menuHeight = 180, menuBtnHeight = 180, menuBtnWith = 150, marginTop = 25;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_drag_del);
initData();
initMenu();
initUiAndListener();
}
public void initData() {
ListItemEnity listItemEnity = new ListItemEnity();
listItemEnity.setItemLabel("111111");
listItemEnity.setItemImageId(ResourceTable.Media_icon);
mAppList.add(listItemEnity);
listItemEnity = new ListItemEnity();
listItemEnity.setItemLabel("2222222");
listItemEnity.setItemImageId(ResourceTable.Media_icon);
mAppList.add(listItemEnity);
listItemEnity = new ListItemEnity();
listItemEnity.setItemLabel("3333333");
listItemEnity.setItemImageId(ResourceTable.Media_icon);
mAppList.add(listItemEnity);
listItemEnity = new ListItemEnity();
listItemEnity.setItemLabel("4444444");
listItemEnity.setItemImageId(ResourceTable.Media_icon);
mAppList.add(listItemEnity);
listItemEnity = new ListItemEnity();
listItemEnity.setItemLabel("5555555");
listItemEnity.setItemImageId(ResourceTable.Media_icon);
mAppList.add(listItemEnity);
listItemEnity = new ListItemEnity();
listItemEnity.setItemLabel("66666666");
listItemEnity.setItemImageId(ResourceTable.Media_icon);
mAppList.add(listItemEnity);
listItemEnity = new ListItemEnity();
listItemEnity.setItemLabel("7777777");
listItemEnity.setItemImageId(ResourceTable.Media_icon);
mAppList.add(listItemEnity);
}
public void initMenu() {
mMenu = new Menu(true);
ShapeElement shapeElement = new ShapeElement();
shapeElement.setRgbColor(convertColor(0xffff4444));
ShapeElement shapeElementGreen = new ShapeElement();
shapeElementGreen.setRgbColor(convertColor(0xff669900));
ShapeElement shapeElementBlue = new ShapeElement();
shapeElementBlue.setRgbColor(convertColor(0xff0099cc));
ShapeElement shapeElementYellow = new ShapeElement();
shapeElementYellow.setRgbColor(convertColor(0xffff8800));
mMenu.addItem(new MenuItem.Builder().setWidth(menuWith)
.setHeight(menuHeight)
.setMarginTop(marginTop)
.setBackground(shapeElementYellow)
.setText("One")
.setTextColor(Color.WHITE.getValue())
.setTextSize(40)
.setMenuBtnHeight(menuBtnHeight)
.setMenuBtnWidth(menuBtnWith)
.build());
mMenu.addItem(new MenuItem.Builder().setWidth(menuWith)
.setHeight(menuHeight)
.setMarginTop(marginTop)
.setBackground(shapeElementGreen)
.setText("Two")
.setTextColor(Color.WHITE.getValue())
.setTextSize(40)
.setMenuBtnHeight(menuBtnHeight)
.setMenuBtnWidth(menuBtnWith)
.build());
mMenu.addItem(new MenuItem.Builder().setWidth(menuWith)
.setHeight(menuHeight)
.setMarginTop(marginTop)
.setBackground(shapeElementBlue)
.setText("Three")
.setDirection(MenuItem.DIRECTION_RIGHT)
.setTextColor(Color.WHITE.getValue())
.setTextSize(40)
.setMenuBtnHeight(menuBtnHeight)
.setMenuBtnWidth(menuBtnWith)
.build());
mMenu.addItem(new MenuItem.Builder().setWidth(menuWith)
.setHeight(menuHeight)
.setMarginTop(marginTop)
.setBackground(shapeElement)
.setText("删除")
.setTextSize(40)
.setTextColor(Color.WHITE.getValue())
.setDirection(MenuItem.DIRECTION_RIGHT)
.setMenuBtnHeight(menuBtnHeight)
.setMenuBtnWidth(menuBtnWith)
.build());
}
public void initUiAndListener() {
mListView = (SlideAndDragListView) findComponentById(ResourceTable.Id_lv_edit);
DirectionalLayout componentContainer = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_group);
mListView.initView(componentContainer);
mListView.setMenu(mMenu);
mListView.setItemProvider(mAdapter);
// mListView.setOnScrollListener(this);
mListView.setOnDragDropListener(this);
// mListView.setOnItemClickListener(this);
mListView.setOnSlideListener(this);
mListView.setOnMenuItemClickListener(this);
mListView.setOnItemDeleteListener(this);
// mListView.setOnItemLongClickListener(new );
mListView.setOnItemScrollBackListener(this);
}
private BaseItemProvider mAdapter = new BaseItemProvider() {
@Override
public int getCount() {
return mAppList.size();
}
@Override
public Object getItem(int position) {
return mAppList.get(position);
}
@Override
public long getItemId(int position) {
return mAppList.get(position).hashCode();
}
@Override
public Component getComponent(int position, Component convertView, ComponentContainer componentContainer) {
CustomViewHolder cvh;
if (convertView == null) {
cvh = new CustomViewHolder();
convertView = LayoutScatter.getInstance(SlideDragListAbilitySlice.this).parse(ResourceTable.Layout_item_custom_btn, null, false);
cvh.imgLogo = (Image) convertView.findComponentById(ResourceTable.Id_img_item_edit);
cvh.txtName = (Text) convertView.findComponentById(ResourceTable.Id_txt_item_edit);
cvh.btnClick = (Button) convertView.findComponentById(ResourceTable.Id_btn_item_click);
cvh.btnClick.setClickedListener(mOnClickListener);
convertView.setTag(cvh);
} else {
cvh = (CustomViewHolder) convertView.getTag();
}
ListItemEnity item = (ListItemEnity) this.getItem(position);
cvh.txtName.setText(item.getItemLabel());
cvh.imgLogo.setPixelMap(item.getItemImageId());
cvh.btnClick.setText(position + "");
cvh.btnClick.setTag(position);
return convertView;
}
class CustomViewHolder {
public Image imgLogo;
public Text txtName;
public Button btnClick;
}
private Component.ClickedListener mOnClickListener = new Component.ClickedListener() {
@Override
public void onClick(Component component) {
Object o = component.getTag();
if (o instanceof Integer) {
new ToastDialog(getContext()).setText("button click-->" + (Integer) o).show();
}
}
};
};
private int mBeginPosition;
@Override
public void onDragViewStart(int beginPosition) {
mBeginPosition = beginPosition;
mDraggedEntity = mAppList.get(beginPosition);
}
@Override
public void onDragDropViewMoved(int fromPosition, int toPosition) {
}
@Override
public void onDragViewDown(int finalPosition) {
ListItemEnity applicationInfo = mAppList.remove(mBeginPosition);
mAppList.add(finalPosition, applicationInfo);
mListView.setItemProvider(mAdapter);
mListView.getItemProvider().notifyDataChanged();
}
@Override
public void onItemMoveFromTo(int fromPosition, int toPosition) {
//TODO 长按拖动回调
ListItemEnity applicationInfo = mAppList.remove(fromPosition);
mAppList.add(toPosition, applicationInfo);
mListView.getItemProvider().notifyDataChanged();
}
@Override
public void onSlideOpen(Component view, Component parentView, int position, int direction) {
}
@Override
public void onSlideClose(Component view, Component parentView, int position, int direction) {
}
@Override
public int onMenuItemClick(Component v, int itemPosition, int buttonPosition, int direction) {
switch (direction) {
case MenuItem.DIRECTION_LEFT:
switch (buttonPosition) {
case 0:
return Menu.ITEM_NOTHING;
case 1:
return Menu.ITEM_SCROLL_BACK;
}
break;
case MenuItem.DIRECTION_RIGHT:
switch (buttonPosition) {
case 0:
return Menu.ITEM_SCROLL_BACK;
case 1:
return Menu.ITEM_DELETE_FROM_BOTTOM_TO_TOP;
}
}
return Menu.ITEM_NOTHING;
}
@Override
public void onItemDeleteAnimationFinished(Component view, int position) {
mAppList.remove(position);
mAdapter.notifyDataChanged();
mListView.getItemProvider().notifyDataSetItemRemoved(position);
mListView.getItemProvider().notifyDataChanged();
}
public RgbColor convertColor(int color) {
int colorInt = color;
int red = (colorInt & 0xff0000) >> 16;
int green = (colorInt & 0x00ff00) >> 8;
int blue = (colorInt & 0x0000ff);
return new RgbColor(red, green, blue);
}
@Override
public void onScrollBackAnimationFinished(Component view, int position) {
}
}
```
## License
Copyright 2015 yydcdut
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.