mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -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());
|
||||
boolean selected = this.selected.contains(habit);
|
||||
|
||||
HabitCardView cardView = (HabitCardView) holder.itemView;
|
||||
listView.bindCardView(cardView, habit, score, checkmarks, selected,
|
||||
listView.bindCardView(holder, habit, score, checkmarks, selected,
|
||||
position);
|
||||
}
|
||||
|
||||
@@ -271,6 +270,6 @@ public class HabitCardListAdapter
|
||||
int k = selected.indexOf(h);
|
||||
if (k < 0) selected.add(h);
|
||||
else selected.remove(h);
|
||||
notifyItemChanged(position);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +56,6 @@ public class CheckmarkButtonView extends FrameLayout
|
||||
{
|
||||
setOnClickListener(v -> controller.onClick());
|
||||
setOnLongClickListener(v -> controller.onLongClick());
|
||||
setOnTouchListener((v, ev) -> {
|
||||
getParent().requestDisallowInterceptTouchEvent(true);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
public void setValue(int value)
|
||||
@@ -71,24 +67,12 @@ public class CheckmarkButtonView extends FrameLayout
|
||||
public void toggle()
|
||||
{
|
||||
value = (value == Checkmark.CHECKED_EXPLICITLY ? Checkmark.UNCHECKED :
|
||||
Checkmark.CHECKED_EXPLICITLY);
|
||||
Checkmark.CHECKED_EXPLICITLY);
|
||||
|
||||
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
|
||||
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
|
||||
protected void onDraw(Canvas canvas)
|
||||
{
|
||||
@@ -115,4 +99,16 @@ public class CheckmarkButtonView extends FrameLayout
|
||||
|
||||
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;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.AttributeSet;
|
||||
import android.widget.LinearLayout;
|
||||
import android.content.*;
|
||||
import android.support.annotation.*;
|
||||
import android.util.*;
|
||||
import android.widget.*;
|
||||
|
||||
import org.isoron.uhabits.HabitsApplication;
|
||||
import org.isoron.uhabits.R;
|
||||
import org.isoron.uhabits.models.Habit;
|
||||
import org.isoron.uhabits.ui.habits.list.controllers.CheckmarkButtonController;
|
||||
import org.isoron.uhabits.utils.DateUtils;
|
||||
import org.isoron.uhabits.utils.Preferences;
|
||||
import org.isoron.uhabits.*;
|
||||
import org.isoron.uhabits.models.*;
|
||||
import org.isoron.uhabits.ui.habits.list.controllers.*;
|
||||
import org.isoron.uhabits.utils.*;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.*;
|
||||
|
||||
public class CheckmarkPanelView extends LinearLayout
|
||||
{
|
||||
@@ -100,6 +98,7 @@ public class CheckmarkPanelView extends LinearLayout
|
||||
public void setController(Controller controller)
|
||||
{
|
||||
this.controller = controller;
|
||||
setupCheckmarkButtons();
|
||||
}
|
||||
|
||||
public void setHabit(@NonNull Habit habit)
|
||||
|
||||
@@ -38,6 +38,8 @@ public class HabitCardListView extends RecyclerView
|
||||
@Nullable
|
||||
private Controller controller;
|
||||
|
||||
private final ItemTouchHelper touchHelper;
|
||||
|
||||
public HabitCardListView(Context context, AttributeSet attrs)
|
||||
{
|
||||
super(context, attrs);
|
||||
@@ -46,15 +48,16 @@ public class HabitCardListView extends RecyclerView
|
||||
setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
|
||||
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,
|
||||
* containing the given data.
|
||||
*
|
||||
* @param cardView an old HabitCardView that should be reused if possible,
|
||||
* possibly null
|
||||
* @param holder the ViewHolder containing the HabitCardView that should
|
||||
* be built
|
||||
* @param habit the habit for this card
|
||||
* @param score the current score for the habit
|
||||
* @param checkmarks the list of checkmark values to be included in the
|
||||
@@ -63,32 +66,19 @@ public class HabitCardListView extends RecyclerView
|
||||
* @param position
|
||||
* @return the HabitCardView generated
|
||||
*/
|
||||
public View bindCardView(@NonNull HabitCardView cardView,
|
||||
public View bindCardView(@NonNull HabitCardViewHolder holder,
|
||||
@NonNull Habit habit,
|
||||
int score,
|
||||
int[] checkmarks,
|
||||
boolean selected,
|
||||
int position)
|
||||
{
|
||||
HabitCardView cardView = (HabitCardView) holder.itemView;
|
||||
cardView.setHabit(habit);
|
||||
cardView.setSelected(selected);
|
||||
cardView.setCheckmarkValues(checkmarks);
|
||||
cardView.setScore(score);
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
if (controller != null) setupCardViewController(holder, position);
|
||||
return cardView;
|
||||
}
|
||||
|
||||
@@ -123,8 +113,26 @@ public class HabitCardListView extends RecyclerView
|
||||
super.onDetachedFromWindow();
|
||||
}
|
||||
|
||||
public interface Controller extends CheckmarkButtonController.Listener,
|
||||
HabitCardController.Listener
|
||||
protected void setupCardViewController(@NonNull HabitCardViewHolder holder,
|
||||
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);
|
||||
|
||||
@@ -137,6 +145,36 @@ public class HabitCardListView extends RecyclerView
|
||||
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
|
||||
{
|
||||
@Override
|
||||
@@ -149,13 +187,13 @@ public class HabitCardListView extends RecyclerView
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLongPressDragEnabled()
|
||||
public boolean isItemViewSwipeEnabled()
|
||||
{
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isItemViewSwipeEnabled()
|
||||
public boolean isLongPressDragEnabled()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -87,11 +87,6 @@ public class HabitCardView extends FrameLayout
|
||||
{
|
||||
checkmarkPanel.setController(null);
|
||||
if (controller == null) return;
|
||||
|
||||
setOnClickListener(v -> {
|
||||
|
||||
});
|
||||
|
||||
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