Skip to content

Commit

Permalink
fix(android): add elevation and support transparent backgrounds
Browse files Browse the repository at this point in the history
  • Loading branch information
garymathews authored and sgtcoolguy committed Jan 19, 2021
1 parent 6d9c62c commit 9025413
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/titanium_ui_listview_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:clipToPadding="false"
android:outlineProvider="bounds">

<!-- Header Container -->
<RelativeLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/titanium_ui_tableview_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:clipToPadding="false"
android:outlineProvider="bounds">

<!-- Header Container -->
<RelativeLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,13 @@ public ItemTouchHandler(TiRecyclerViewAdapter adapter, RecyclerViewProxy recycle
}

/**
* Override drag direction flags.
* Determine if proxy supports movement.
*
* @param recyclerView Current RecyclerView.
* @param viewHolder Current Holder.
* @return Integer containing drag flags.
* @param holderProxy
* @return Boolean
*/
@Override
public int getDragDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder)
private boolean canMove(TiViewProxy holderProxy)
{
final TiRecyclerViewHolder holder = (TiRecyclerViewHolder) viewHolder;
final TiViewProxy holderProxy = holder.getProxy();

String moveProperty = TiC.PROPERTY_MOVABLE;
if (holderProxy instanceof ListItemProxy) {

Expand All @@ -63,11 +58,46 @@ public int getDragDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerView
final boolean defaultValue = this.recyclerViewProxy.getProperties().optBoolean(moveProperty, false);

// Obtain move value from current holder proxy.
final boolean canMove = holderProxy.getProperties().optBoolean(moveProperty, defaultValue);
return holderProxy.getProperties().optBoolean(moveProperty, defaultValue);
}

/**
* Determine if proxy supports swipe.
*
* @param holderProxy
* @return Boolean
*/
private boolean canSwipe(TiViewProxy holderProxy)
{
String editProperty = TiC.PROPERTY_EDITABLE;
if (holderProxy instanceof ListItemProxy) {
editProperty = TiC.PROPERTY_CAN_EDIT;
}

// Obtain default edit value from RecyclerView proxy.
final boolean defaultValue = this.recyclerViewProxy.getProperties().optBoolean(editProperty, false);

// Obtain edit value from current holder proxy.
return holderProxy.getProperties().optBoolean(editProperty, defaultValue);
}

/**
* Override drag direction flags.
*
* @param recyclerView Current RecyclerView.
* @param viewHolder Current Holder.
* @return Integer containing drag flags.
*/
@Override
public int getDragDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder)
{
final TiRecyclerViewHolder holder = (TiRecyclerViewHolder) viewHolder;
final TiViewProxy holderProxy = holder.getProxy();

if ((isEditing() || isMoving()) && canMove) {
if ((isEditing() || isMoving()) && canMove(holderProxy)) {
return ItemTouchHelper.UP | ItemTouchHelper.DOWN;
}

return 0;
}

Expand All @@ -84,18 +114,7 @@ public int getSwipeDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerVie
final TiRecyclerViewHolder holder = (TiRecyclerViewHolder) viewHolder;
final TiViewProxy holderProxy = holder.getProxy();

String editProperty = TiC.PROPERTY_EDITABLE;
if (holderProxy instanceof ListItemProxy) {
editProperty = TiC.PROPERTY_CAN_EDIT;
}

// Obtain default edit value from RecyclerView proxy.
final boolean defaultValue = this.recyclerViewProxy.getProperties().optBoolean(editProperty, false);

// Obtain edit value from current holder proxy.
final boolean canSwipe = holderProxy.getProperties().optBoolean(editProperty, defaultValue);

if ((isEditing() || isMoving()) && canSwipe) {
if ((isEditing() || isMoving()) && canSwipe(holderProxy)) {
return ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
}
return 0;
Expand Down Expand Up @@ -189,18 +208,30 @@ public void onChildDraw(@NonNull Canvas c,
{
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

if ((!isEditing() && !isMoving()) || dX == 0) {
final TiRecyclerViewHolder holder = (TiRecyclerViewHolder) viewHolder;
final TiViewProxy holderProxy = holder.getProxy();
final View view = holder.itemView;

if (!isEditing() && !isMoving()) {

// No swipe, do not render.
return;
}

final View view = viewHolder.itemView;
if (isCurrentlyActive) {

final int iconMargin = view.getHeight() - icon.getIntrinsicHeight();
// Set elevation for row being moved.
view.setElevation(canMove(holderProxy) ? 30 : 0);
}

final int iconMargin = (view.getHeight() - icon.getIntrinsicHeight()) / 2;
final int iconTop = view.getTop() + (view.getHeight() - icon.getIntrinsicHeight()) / 2;
final int iconBottom = iconTop + icon.getIntrinsicHeight();
final int backgroundWidth = view.getMeasuredWidth() / 2;

// Add additional background width when `borderRadius` is set.
// This is to apply the red background behind the curved borders.
final int backgroundWidth = holderProxy.getProperty(TiC.PROPERTY_BORDER_RADIUS) != null
? view.getMeasuredWidth() / 2 : 0;

if (dX > 0) {
final int iconLeft = view.getLeft() + iconMargin;
Expand All @@ -219,6 +250,11 @@ public void onChildDraw(@NonNull Canvas c,
// Set bounds for left swipe.
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);
background.setBounds(backgroundLeft, view.getTop(), view.getRight(), view.getBottom());
} else {

// Reset bounds.
icon.setBounds(0, 0, 0, 0);
background.setBounds(0, 0, 0, 0);
}

background.draw(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ListViewHolder extends TiRecyclerViewHolder
private static final String TAG = "ListViewHolder";

// Top
private final ViewGroup header;
private final TiCompositeLayout header;
private final TextView headerTitle;

// Middle
Expand Down

0 comments on commit 9025413

Please sign in to comment.