mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 17:18:52 -06:00
Remove some drag-and-drop glitches
This commit is contained in:
@@ -149,8 +149,7 @@ public class HabitCardListAdapter
|
|||||||
int checkmarks[] = cache.getCheckmarks(habit.getId());
|
int checkmarks[] = cache.getCheckmarks(habit.getId());
|
||||||
boolean selected = this.selected.contains(habit);
|
boolean selected = this.selected.contains(habit);
|
||||||
|
|
||||||
HabitCardView cardView = (HabitCardView) holder.itemView;
|
listView.bindCardView(holder, habit, score, checkmarks, selected,
|
||||||
listView.bindCardView(cardView, habit, score, checkmarks, selected,
|
|
||||||
position);
|
position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,6 +270,6 @@ public class HabitCardListAdapter
|
|||||||
int k = selected.indexOf(h);
|
int k = selected.indexOf(h);
|
||||||
if (k < 0) selected.add(h);
|
if (k < 0) selected.add(h);
|
||||||
else selected.remove(h);
|
else selected.remove(h);
|
||||||
notifyItemChanged(position);
|
notifyDataSetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,10 +56,6 @@ public class CheckmarkButtonView extends FrameLayout
|
|||||||
{
|
{
|
||||||
setOnClickListener(v -> controller.onClick());
|
setOnClickListener(v -> controller.onClick());
|
||||||
setOnLongClickListener(v -> controller.onLongClick());
|
setOnLongClickListener(v -> controller.onLongClick());
|
||||||
setOnTouchListener((v, ev) -> {
|
|
||||||
getParent().requestDisallowInterceptTouchEvent(true);
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValue(int value)
|
public void setValue(int value)
|
||||||
@@ -77,18 +73,6 @@ public class CheckmarkButtonView extends FrameLayout
|
|||||||
postInvalidate();
|
postInvalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init()
|
|
||||||
{
|
|
||||||
addView(
|
|
||||||
inflate(getContext(), R.layout.list_habits_card_checkmark, null));
|
|
||||||
ButterKnife.bind(this);
|
|
||||||
|
|
||||||
setWillNotDraw(false);
|
|
||||||
setHapticFeedbackEnabled(false);
|
|
||||||
|
|
||||||
tvCheck.setTypeface(InterfaceUtils.getFontAwesome(getContext()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDraw(Canvas canvas)
|
protected void onDraw(Canvas canvas)
|
||||||
{
|
{
|
||||||
@@ -115,4 +99,16 @@ public class CheckmarkButtonView extends FrameLayout
|
|||||||
|
|
||||||
super.onDraw(canvas);
|
super.onDraw(canvas);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void init()
|
||||||
|
{
|
||||||
|
addView(
|
||||||
|
inflate(getContext(), R.layout.list_habits_card_checkmark, null));
|
||||||
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
|
setWillNotDraw(false);
|
||||||
|
setHapticFeedbackEnabled(false);
|
||||||
|
|
||||||
|
tvCheck.setTypeface(InterfaceUtils.getFontAwesome(getContext()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,19 +19,17 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.ui.habits.list.views;
|
package org.isoron.uhabits.ui.habits.list.views;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.*;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.*;
|
||||||
import android.util.AttributeSet;
|
import android.util.*;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.HabitsApplication;
|
import org.isoron.uhabits.*;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.ui.habits.list.controllers.*;
|
||||||
import org.isoron.uhabits.ui.habits.list.controllers.CheckmarkButtonController;
|
import org.isoron.uhabits.utils.*;
|
||||||
import org.isoron.uhabits.utils.DateUtils;
|
|
||||||
import org.isoron.uhabits.utils.Preferences;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.*;
|
||||||
|
|
||||||
public class CheckmarkPanelView extends LinearLayout
|
public class CheckmarkPanelView extends LinearLayout
|
||||||
{
|
{
|
||||||
@@ -100,6 +98,7 @@ public class CheckmarkPanelView extends LinearLayout
|
|||||||
public void setController(Controller controller)
|
public void setController(Controller controller)
|
||||||
{
|
{
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
|
setupCheckmarkButtons();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHabit(@NonNull Habit habit)
|
public void setHabit(@NonNull Habit habit)
|
||||||
|
|||||||
@@ -38,6 +38,8 @@ public class HabitCardListView extends RecyclerView
|
|||||||
@Nullable
|
@Nullable
|
||||||
private Controller controller;
|
private Controller controller;
|
||||||
|
|
||||||
|
private final ItemTouchHelper touchHelper;
|
||||||
|
|
||||||
public HabitCardListView(Context context, AttributeSet attrs)
|
public HabitCardListView(Context context, AttributeSet attrs)
|
||||||
{
|
{
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
@@ -46,15 +48,16 @@ public class HabitCardListView extends RecyclerView
|
|||||||
setLayoutManager(new LinearLayoutManager(getContext()));
|
setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
|
|
||||||
TouchHelperCallback callback = new TouchHelperCallback();
|
TouchHelperCallback callback = new TouchHelperCallback();
|
||||||
new ItemTouchHelper(callback).attachToRecyclerView(this);
|
touchHelper = new ItemTouchHelper(callback);
|
||||||
|
touchHelper.attachToRecyclerView(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Builds a new HabitCardView to be eventually added to this list,
|
* Builds a new HabitCardView to be eventually added to this list,
|
||||||
* containing the given data.
|
* containing the given data.
|
||||||
*
|
*
|
||||||
* @param cardView an old HabitCardView that should be reused if possible,
|
* @param holder the ViewHolder containing the HabitCardView that should
|
||||||
* possibly null
|
* be built
|
||||||
* @param habit the habit for this card
|
* @param habit the habit for this card
|
||||||
* @param score the current score for the habit
|
* @param score the current score for the habit
|
||||||
* @param checkmarks the list of checkmark values to be included in the
|
* @param checkmarks the list of checkmark values to be included in the
|
||||||
@@ -63,32 +66,19 @@ public class HabitCardListView extends RecyclerView
|
|||||||
* @param position
|
* @param position
|
||||||
* @return the HabitCardView generated
|
* @return the HabitCardView generated
|
||||||
*/
|
*/
|
||||||
public View bindCardView(@NonNull HabitCardView cardView,
|
public View bindCardView(@NonNull HabitCardViewHolder holder,
|
||||||
@NonNull Habit habit,
|
@NonNull Habit habit,
|
||||||
int score,
|
int score,
|
||||||
int[] checkmarks,
|
int[] checkmarks,
|
||||||
boolean selected,
|
boolean selected,
|
||||||
int position)
|
int position)
|
||||||
{
|
{
|
||||||
|
HabitCardView cardView = (HabitCardView) holder.itemView;
|
||||||
cardView.setHabit(habit);
|
cardView.setHabit(habit);
|
||||||
cardView.setSelected(selected);
|
cardView.setSelected(selected);
|
||||||
cardView.setCheckmarkValues(checkmarks);
|
cardView.setCheckmarkValues(checkmarks);
|
||||||
cardView.setScore(score);
|
cardView.setScore(score);
|
||||||
|
if (controller != null) setupCardViewController(holder, position);
|
||||||
if (controller != null)
|
|
||||||
{
|
|
||||||
HabitCardController cardController = new HabitCardController();
|
|
||||||
cardController.setListener(controller);
|
|
||||||
cardView.setController(cardController);
|
|
||||||
cardController.setView(cardView);
|
|
||||||
|
|
||||||
cardView.setOnClickListener(v -> controller.onItemClick(position));
|
|
||||||
cardView.setOnLongClickListener(v -> {
|
|
||||||
controller.onItemLongClick(position);
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return cardView;
|
return cardView;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,8 +113,26 @@ public class HabitCardListView extends RecyclerView
|
|||||||
super.onDetachedFromWindow();
|
super.onDetachedFromWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Controller extends CheckmarkButtonController.Listener,
|
protected void setupCardViewController(@NonNull HabitCardViewHolder holder,
|
||||||
HabitCardController.Listener
|
int position)
|
||||||
|
{
|
||||||
|
HabitCardView cardView = (HabitCardView) holder.itemView;
|
||||||
|
HabitCardController cardController = new HabitCardController();
|
||||||
|
cardController.setListener(controller);
|
||||||
|
cardView.setController(cardController);
|
||||||
|
cardController.setView(cardView);
|
||||||
|
|
||||||
|
GestureDetector detector = new GestureDetector(getContext(),
|
||||||
|
new CardViewGestureDetector(position, holder));
|
||||||
|
|
||||||
|
cardView.setOnTouchListener((v, ev) -> {
|
||||||
|
detector.onTouchEvent(ev);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface Controller
|
||||||
|
extends CheckmarkButtonController.Listener, HabitCardController.Listener
|
||||||
{
|
{
|
||||||
void drag(int from, int to);
|
void drag(int from, int to);
|
||||||
|
|
||||||
@@ -137,6 +145,36 @@ public class HabitCardListView extends RecyclerView
|
|||||||
void startDrag(int position);
|
void startDrag(int position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class CardViewGestureDetector
|
||||||
|
extends GestureDetector.SimpleOnGestureListener
|
||||||
|
{
|
||||||
|
private final int position;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final HabitCardViewHolder holder;
|
||||||
|
|
||||||
|
public CardViewGestureDetector(int position,
|
||||||
|
@NonNull HabitCardViewHolder holder)
|
||||||
|
{
|
||||||
|
this.position = position;
|
||||||
|
this.holder = holder;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLongPress(MotionEvent e)
|
||||||
|
{
|
||||||
|
if (controller != null) controller.onItemLongClick(position);
|
||||||
|
touchHelper.startDrag(holder);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onSingleTapUp(MotionEvent e)
|
||||||
|
{
|
||||||
|
if (controller != null) controller.onItemClick(position);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class TouchHelperCallback extends ItemTouchHelper.Callback
|
class TouchHelperCallback extends ItemTouchHelper.Callback
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
@@ -149,13 +187,13 @@ public class HabitCardListView extends RecyclerView
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isLongPressDragEnabled()
|
public boolean isItemViewSwipeEnabled()
|
||||||
{
|
{
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isItemViewSwipeEnabled()
|
public boolean isLongPressDragEnabled()
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,11 +87,6 @@ public class HabitCardView extends FrameLayout
|
|||||||
{
|
{
|
||||||
checkmarkPanel.setController(null);
|
checkmarkPanel.setController(null);
|
||||||
if (controller == null) return;
|
if (controller == null) return;
|
||||||
|
|
||||||
setOnClickListener(v -> {
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
checkmarkPanel.setController(controller);
|
checkmarkPanel.setController(controller);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +219,5 @@ public class HabitCardView extends FrameLayout
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Controller extends CheckmarkPanelView.Controller
|
public interface Controller extends CheckmarkPanelView.Controller {}
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user