Skip to content

Commit c0c4e32

Browse files
committed
feat: reorderLongPressEnabled property (disabled by default)
1 parent 741e843 commit c0c4e32

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/collectionview-common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
136136
public loadMoreThreshold: number;
137137

138138
public reorderEnabled: boolean;
139+
public reorderLongPressEnabled: boolean;
139140
protected _dataUpdatesSuspended = false;
140141

141142
public layoutStyle: string = 'grid';
@@ -644,3 +645,9 @@ export const reorderingEnabledProperty = new Property<CollectionViewBase, boolea
644645
valueConverter: booleanConverter,
645646
});
646647
reorderingEnabledProperty.register(CollectionViewBase);
648+
export const reorderLongPressEnabledProperty = new Property<CollectionViewBase, boolean>({
649+
name: 'reorderLongPressEnabled',
650+
defaultValue: false,
651+
valueConverter: booleanConverter,
652+
});
653+
reorderLongPressEnabledProperty.register(CollectionViewBase);

src/collectionview.android.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
profile,
1616
} from '@nativescript/core';
1717
import { layout } from '@nativescript/core/utils/utils';
18-
import { CollectionViewItemDisplayEventData, CollectionViewItemEventData, Orientation, reorderingEnabledProperty, reverseLayoutProperty } from './collectionview';
18+
import { CollectionViewItemDisplayEventData, CollectionViewItemEventData, Orientation, reorderLongPressEnabledProperty, reorderingEnabledProperty, reverseLayoutProperty } from './collectionview';
1919
import { CLog, CLogTypes, CollectionViewBase, ListViewViewTypes, isScrollEnabledProperty, orientationProperty } from './collectionview-common';
2020

2121
export * from './collectionview-common';
@@ -567,15 +567,8 @@ export class CollectionView extends CollectionViewBase {
567567

568568
_longPressGesture: androidx.core.view.GestureDetectorCompat;
569569
_itemTouchListerner: androidx.recyclerview.widget.RecyclerView.OnItemTouchListener;
570-
public [reorderingEnabledProperty.setNative](value: boolean) {
570+
public [reorderLongPressEnabledProperty.setNative](value: boolean) {
571571
if (value) {
572-
if (!this._simpleItemTouchCallback) {
573-
const ItemTouchHelper = androidx.recyclerview.widget.ItemTouchHelper;
574-
this._simpleItemTouchCallback = new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END , 0);
575-
this._simpleItemTouchCallback.owner = new WeakRef(this);
576-
this._itemTouchHelper = new androidx.recyclerview.widget.ItemTouchHelper(this._simpleItemTouchCallback);
577-
this._itemTouchHelper.attachToRecyclerView(this.nativeViewProtected);
578-
}
579572
if (!this._longPressGesture) {
580573
this._longPressGesture = new androidx.core.view.GestureDetectorCompat(this._context, new LongPressGestureListenerImpl(new WeakRef(this)));
581574
this._itemTouchListerner = new androidx.recyclerview.widget.RecyclerView.OnItemTouchListener({
@@ -590,7 +583,22 @@ export class CollectionView extends CollectionViewBase {
590583
onRequestDisallowInterceptTouchEvent:(disallowIntercept: boolean) =>{
591584
}
592585
});
593-
this.nativeViewProtected.addOnItemTouchListener(this._itemTouchListerner);
586+
}
587+
this.nativeViewProtected.addOnItemTouchListener(this._itemTouchListerner);
588+
} else {
589+
if (this._itemTouchListerner) {
590+
this.nativeViewProtected.removeOnItemTouchListener(this._itemTouchListerner);
591+
}
592+
}
593+
}
594+
public [reorderingEnabledProperty.setNative](value: boolean) {
595+
if (value) {
596+
if (!this._simpleItemTouchCallback) {
597+
const ItemTouchHelper = androidx.recyclerview.widget.ItemTouchHelper;
598+
this._simpleItemTouchCallback = new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END , 0);
599+
this._simpleItemTouchCallback.owner = new WeakRef(this);
600+
this._itemTouchHelper = new androidx.recyclerview.widget.ItemTouchHelper(this._simpleItemTouchCallback);
601+
this._itemTouchHelper.attachToRecyclerView(this.nativeViewProtected);
594602
}
595603
}
596604
}

src/collectionview.ios.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
profile,
2020
} from '@nativescript/core';
2121
import { layout } from '@nativescript/core/utils/utils';
22-
import { CollectionViewItemDisplayEventData, CollectionViewItemEventData, Orientation, reorderingEnabledProperty, reverseLayoutProperty } from './collectionview';
22+
import { CollectionViewItemDisplayEventData, CollectionViewItemEventData, Orientation, reorderLongPressEnabledProperty, reorderingEnabledProperty, reverseLayoutProperty } from './collectionview';
2323
import { CLog, CLogTypes, CollectionViewBase, ListViewViewTypes, isBounceEnabledProperty, isScrollEnabledProperty, itemTemplatesProperty, orientationProperty } from './collectionview-common';
2424

2525
export * from './collectionview-common';
@@ -274,7 +274,7 @@ export class CollectionView extends CollectionViewBase {
274274
break;
275275
}
276276
}
277-
public [reorderingEnabledProperty.setNative](value: boolean) {
277+
public [reorderLongPressEnabledProperty.setNative](value: boolean) {
278278
if (value) {
279279
if (!this.reorderLongPressGesture) {
280280
this.reorderLongPressHandler = ReorderLongPressImpl.initWithOwner(new WeakRef(this));
@@ -283,11 +283,17 @@ export class CollectionView extends CollectionViewBase {
283283
} else {
284284
this.reorderLongPressGesture.enabled = true;
285285
}
286-
this.on('touch', this.onReorderingTouch, this);
287286
} else {
288287
if (this.reorderLongPressGesture) {
289288
this.reorderLongPressGesture.enabled = false;
290289
}
290+
}
291+
292+
}
293+
public [reorderingEnabledProperty.setNative](value: boolean) {
294+
if (value) {
295+
this.on('touch', this.onReorderingTouch, this);
296+
} else {
291297
this.off('touch', this.onReorderingTouch, this);
292298
}
293299
}

0 commit comments

Comments
 (0)