Skip to content

Commit

Permalink
fix(android): fix drag and drop issues (#13387)
Browse files Browse the repository at this point in the history
* fix: allow drop-and-drop with single touch

* chore(android): change opacity on drag (parity with iOS)

* fix: remove typo

* chore: add null-check

* chore: remove custom opacity

* fix: remove unused imports

* chore(android): add spacing to drag icon
  • Loading branch information
hansemannn committed Apr 11, 2022
1 parent d9ef416 commit cffee52
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
16 changes: 8 additions & 8 deletions android/modules/ui/res/drawable/titanium_icon_drag.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#C3C3C3">
<path
android:fillColor="@android:color/black"
android:pathData="M20,9H4v2h16V9zM4,15h16v-2H4V15z"/>
android:width="24dp"
android:height="34dp"
android:tint="#C3C3C3"
android:viewportWidth="24"
android:viewportHeight="34">
<path
android:fillColor="@android:color/black"
android:pathData="M 20 14 L 4 14 L 4 16 L 20 16 Z M 4 20 L 20 20 L 20 18 L 4 18 Z" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import android.graphics.drawable.PaintDrawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.lang.ref.WeakReference;

import ti.modules.titanium.ui.UIModule;
Expand Down Expand Up @@ -157,6 +160,23 @@ public void bind(final ListItemProxy proxy, final boolean selected)
if (isEditing && canMove) {
this.rightImage.setImageDrawable(dragDrawable);
this.rightImage.setVisibility(View.VISIBLE);

RecyclerView.ViewHolder mViewHolder = this;

this.rightImage.setOnTouchListener(new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
if (motionEvent.getActionMasked() == MotionEvent.ACTION_DOWN) {
TiListView listView = listViewProxy.getListView();
listView.startDragging(mViewHolder);
}
return false;
}
});
} else {
this.rightImage.setOnTouchListener(null);
}

if (proxy != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public int getIndexOfSection(ListSectionProxy section)
*
* @return TiListView
*/
private TiListView getListView()
public TiListView getListView()
{
final TiUIListView view = (TiUIListView) this.view;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class TiListView extends TiSwipeRefreshLayout implements OnSearchChangeLi
private final ListViewProxy proxy;
private final TiNestedRecyclerView recyclerView;
private final List<KrollDict> selectedItems = new ArrayList<>();
private final ItemTouchHelper itemTouchHelper;

private boolean hasLaidOutChildren = false;
private SelectionTracker tracker = null;
Expand Down Expand Up @@ -176,7 +177,7 @@ public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy)

// Create ItemTouchHelper for swipe-to-delete and move gestures.
final ItemTouchHandler itemTouchHandler = new ItemTouchHandler(this.adapter, this.proxy, this.recyclerView);
final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHandler);
itemTouchHelper = new ItemTouchHelper(itemTouchHandler);
itemTouchHelper.attachToRecyclerView(this.recyclerView);

// Fire `postlayout` on layout changes.
Expand Down Expand Up @@ -606,6 +607,16 @@ public void setSeparator(int color, int height)
this.recyclerView.invalidate();
}

/**
* Starts dragging programatically.
*
* @param vHolder The dedicated view holder
*/
public void startDragging(RecyclerView.ViewHolder vHolder)
{
itemTouchHelper.startDrag(vHolder);
}

/**
* Set row separator drawable.
*
Expand Down

0 comments on commit cffee52

Please sign in to comment.