mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Do not store position of ViewHolder
This commit is contained in:
@@ -150,8 +150,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);
|
||||||
|
|
||||||
listView.bindCardView(holder, habit, score, checkmarks, selected,
|
listView.bindCardView(holder, habit, score, checkmarks, selected);
|
||||||
position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -67,15 +67,13 @@ public class HabitCardListView extends RecyclerView
|
|||||||
* @param checkmarks the list of checkmark values to be included in the
|
* @param checkmarks the list of checkmark values to be included in the
|
||||||
* card
|
* card
|
||||||
* @param selected true if the card is selected, false otherwise
|
* @param selected true if the card is selected, false otherwise
|
||||||
* @param position
|
|
||||||
* @return the HabitCardView generated
|
* @return the HabitCardView generated
|
||||||
*/
|
*/
|
||||||
public View bindCardView(@NonNull HabitCardViewHolder holder,
|
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 visibleCheckmarks[] =
|
int visibleCheckmarks[] =
|
||||||
Arrays.copyOfRange(checkmarks, 0, checkmarkCount);
|
Arrays.copyOfRange(checkmarks, 0, checkmarkCount);
|
||||||
@@ -85,7 +83,7 @@ public class HabitCardListView extends RecyclerView
|
|||||||
cardView.setSelected(selected);
|
cardView.setSelected(selected);
|
||||||
cardView.setCheckmarkValues(visibleCheckmarks);
|
cardView.setCheckmarkValues(visibleCheckmarks);
|
||||||
cardView.setScore(score);
|
cardView.setScore(score);
|
||||||
if (controller != null) setupCardViewController(holder, position);
|
if (controller != null) setupCardViewController(holder);
|
||||||
return cardView;
|
return cardView;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,8 +123,7 @@ public class HabitCardListView extends RecyclerView
|
|||||||
super.onDetachedFromWindow();
|
super.onDetachedFromWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setupCardViewController(@NonNull HabitCardViewHolder holder,
|
protected void setupCardViewController(@NonNull HabitCardViewHolder holder)
|
||||||
int position)
|
|
||||||
{
|
{
|
||||||
HabitCardView cardView = (HabitCardView) holder.itemView;
|
HabitCardView cardView = (HabitCardView) holder.itemView;
|
||||||
HabitCardController cardController = new HabitCardController();
|
HabitCardController cardController = new HabitCardController();
|
||||||
@@ -135,7 +132,7 @@ public class HabitCardListView extends RecyclerView
|
|||||||
cardController.setView(cardView);
|
cardController.setView(cardView);
|
||||||
|
|
||||||
GestureDetector detector = new GestureDetector(getContext(),
|
GestureDetector detector = new GestureDetector(getContext(),
|
||||||
new CardViewGestureDetector(position, holder));
|
new CardViewGestureDetector(holder));
|
||||||
|
|
||||||
cardView.setOnTouchListener((v, ev) -> {
|
cardView.setOnTouchListener((v, ev) -> {
|
||||||
detector.onTouchEvent(ev);
|
detector.onTouchEvent(ev);
|
||||||
@@ -158,21 +155,18 @@ public class HabitCardListView extends RecyclerView
|
|||||||
private class CardViewGestureDetector
|
private class CardViewGestureDetector
|
||||||
extends GestureDetector.SimpleOnGestureListener
|
extends GestureDetector.SimpleOnGestureListener
|
||||||
{
|
{
|
||||||
private final int position;
|
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private final HabitCardViewHolder holder;
|
private final HabitCardViewHolder holder;
|
||||||
|
|
||||||
public CardViewGestureDetector(int position,
|
public CardViewGestureDetector(@NonNull HabitCardViewHolder holder)
|
||||||
@NonNull HabitCardViewHolder holder)
|
|
||||||
{
|
{
|
||||||
this.position = position;
|
|
||||||
this.holder = holder;
|
this.holder = holder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLongPress(MotionEvent e)
|
public void onLongPress(MotionEvent e)
|
||||||
{
|
{
|
||||||
|
int position = holder.getAdapterPosition();
|
||||||
if (controller != null) controller.onItemLongClick(position);
|
if (controller != null) controller.onItemLongClick(position);
|
||||||
touchHelper.startDrag(holder);
|
touchHelper.startDrag(holder);
|
||||||
}
|
}
|
||||||
@@ -180,6 +174,7 @@ public class HabitCardListView extends RecyclerView
|
|||||||
@Override
|
@Override
|
||||||
public boolean onSingleTapUp(MotionEvent e)
|
public boolean onSingleTapUp(MotionEvent e)
|
||||||
{
|
{
|
||||||
|
int position = holder.getAdapterPosition();
|
||||||
if (controller != null) controller.onItemClick(position);
|
if (controller != null) controller.onItemClick(position);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user