mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Replace ListView with RecyclerView
This commit is contained in:
@@ -141,6 +141,6 @@ public class ListHabitsRootView extends BaseRootView
|
|||||||
private void updateEmptyView()
|
private void updateEmptyView()
|
||||||
{
|
{
|
||||||
llEmpty.setVisibility(
|
llEmpty.setVisibility(
|
||||||
listAdapter.getCount() > 0 ? View.GONE : View.VISIBLE);
|
listAdapter.getItemCount() > 0 ? View.GONE : View.VISIBLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ public class HabitCardListController implements DragSortListView.DropListener,
|
|||||||
private void cancelSelection()
|
private void cancelSelection()
|
||||||
{
|
{
|
||||||
adapter.clearSelection();
|
adapter.clearSelection();
|
||||||
view.setDragEnabled(true);
|
// view.setDragEnabled(true);
|
||||||
activeMode = new NormalMode();
|
activeMode = new NormalMode();
|
||||||
|
|
||||||
if (selectionListener != null) selectionListener.onSelectionFinish();
|
if (selectionListener != null) selectionListener.onSelectionFinish();
|
||||||
|
|||||||
@@ -20,8 +20,8 @@
|
|||||||
package org.isoron.uhabits.ui.habits.list.model;
|
package org.isoron.uhabits.ui.habits.list.model;
|
||||||
|
|
||||||
import android.support.annotation.*;
|
import android.support.annotation.*;
|
||||||
|
import android.support.v7.widget.*;
|
||||||
import android.view.*;
|
import android.view.*;
|
||||||
import android.widget.*;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
import org.isoron.uhabits.models.*;
|
import org.isoron.uhabits.models.*;
|
||||||
@@ -35,7 +35,8 @@ import java.util.*;
|
|||||||
* The data if fetched and cached by a {@link HabitCardListCache}. This adapter
|
* The data if fetched and cached by a {@link HabitCardListCache}. This adapter
|
||||||
* also holds a list of items that have been selected.
|
* also holds a list of items that have been selected.
|
||||||
*/
|
*/
|
||||||
public class HabitCardListAdapter extends BaseAdapter
|
public class HabitCardListAdapter
|
||||||
|
extends RecyclerView.Adapter<HabitCardViewHolder>
|
||||||
implements HabitCardListCache.Listener
|
implements HabitCardListCache.Listener
|
||||||
{
|
{
|
||||||
@NonNull
|
@NonNull
|
||||||
@@ -61,6 +62,8 @@ public class HabitCardListAdapter extends BaseAdapter
|
|||||||
cache = new HabitCardListCache(allHabits);
|
cache = new HabitCardListCache(allHabits);
|
||||||
cache.setListener(this);
|
cache.setListener(this);
|
||||||
cache.setCheckmarkCount(checkmarkCount);
|
cache.setCheckmarkCount(checkmarkCount);
|
||||||
|
|
||||||
|
setHasStableIds(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancelRefresh()
|
public void cancelRefresh()
|
||||||
@@ -78,7 +81,7 @@ public class HabitCardListAdapter extends BaseAdapter
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount()
|
public int getItemCount()
|
||||||
{
|
{
|
||||||
return cache.getHabitCount();
|
return cache.getHabitCount();
|
||||||
}
|
}
|
||||||
@@ -90,13 +93,37 @@ public class HabitCardListAdapter extends BaseAdapter
|
|||||||
* @return the item at given position
|
* @return the item at given position
|
||||||
* @throws IndexOutOfBoundsException if position is not valid
|
* @throws IndexOutOfBoundsException if position is not valid
|
||||||
*/
|
*/
|
||||||
@Override
|
@Deprecated
|
||||||
@NonNull
|
@NonNull
|
||||||
public Habit getItem(int position)
|
public Habit getItem(int position)
|
||||||
{
|
{
|
||||||
return cache.getHabitByPosition(position);
|
return cache.getHabitByPosition(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HabitCardViewHolder onCreateViewHolder(ViewGroup parent,
|
||||||
|
int viewType)
|
||||||
|
{
|
||||||
|
if(listView == null) return null;
|
||||||
|
View view = listView.createCardView();
|
||||||
|
return new HabitCardViewHolder(view);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBindViewHolder(@Nullable HabitCardViewHolder holder, int position)
|
||||||
|
{
|
||||||
|
if(holder == null) return;
|
||||||
|
if(listView == null) return;
|
||||||
|
|
||||||
|
Habit habit = cache.getHabitByPosition(position);
|
||||||
|
int score = cache.getScore(habit.getId());
|
||||||
|
int checkmarks[] = cache.getCheckmarks(habit.getId());
|
||||||
|
boolean selected = this.selected.contains(habit);
|
||||||
|
|
||||||
|
HabitCardView cardView = (HabitCardView) holder.itemView;
|
||||||
|
listView.bindCardView(cardView, habit, score, checkmarks, selected);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getItemId(int position)
|
public long getItemId(int position)
|
||||||
{
|
{
|
||||||
@@ -115,21 +142,21 @@ public class HabitCardListAdapter extends BaseAdapter
|
|||||||
return new LinkedList<>(selected);
|
return new LinkedList<>(selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// @Override
|
||||||
public View getView(int position,
|
// public View getView(int position,
|
||||||
@Nullable View view,
|
// @Nullable View view,
|
||||||
@Nullable ViewGroup parent)
|
// @Nullable ViewGroup parent)
|
||||||
{
|
// {
|
||||||
if (listView == null) return null;
|
// if (listView == null) return null;
|
||||||
|
//
|
||||||
Habit habit = cache.getHabitByPosition(position);
|
// Habit habit = cache.getHabitByPosition(position);
|
||||||
int score = cache.getScore(habit.getId());
|
// int score = cache.getScore(habit.getId());
|
||||||
int checkmarks[] = cache.getCheckmarks(habit.getId());
|
// int checkmarks[] = cache.getCheckmarks(habit.getId());
|
||||||
boolean selected = this.selected.contains(habit);
|
// boolean selected = this.selected.contains(habit);
|
||||||
|
//
|
||||||
return listView.buildCardView((HabitCardView) view, habit, score,
|
// return listView.buildCardView((HabitCardView) view, habit, score,
|
||||||
checkmarks, selected);
|
// checkmarks, selected);
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether list of selected items is empty.
|
* Returns whether list of selected items is empty.
|
||||||
@@ -203,12 +230,6 @@ public class HabitCardListAdapter extends BaseAdapter
|
|||||||
this.listView = listView;
|
this.listView = listView;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setShowArchived(boolean showArchived)
|
|
||||||
{
|
|
||||||
cache.setIncludeArchived(showArchived);
|
|
||||||
cache.refreshAllHabits(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selects or deselects the item at a given position.
|
* Selects or deselects the item at a given position.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -102,16 +102,6 @@ public class HabitCardListCache implements CommandRunner.Listener
|
|||||||
return data.habits.size();
|
return data.habits.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getIncludeArchived()
|
|
||||||
{
|
|
||||||
return includeArchived;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setIncludeArchived(boolean includeArchived)
|
|
||||||
{
|
|
||||||
this.includeArchived = includeArchived;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public Long getLastLoadTimestamp()
|
public Long getLastLoadTimestamp()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.ui.habits.list.model;
|
||||||
|
|
||||||
|
import android.support.v7.widget.*;
|
||||||
|
import android.view.*;
|
||||||
|
|
||||||
|
public class HabitCardViewHolder extends RecyclerView.ViewHolder
|
||||||
|
{
|
||||||
|
public HabitCardViewHolder(View itemView)
|
||||||
|
{
|
||||||
|
super(itemView);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,22 +19,19 @@
|
|||||||
|
|
||||||
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.support.annotation.Nullable;
|
import android.support.v7.widget.*;
|
||||||
import android.util.AttributeSet;
|
import android.util.*;
|
||||||
import android.view.View;
|
import android.view.*;
|
||||||
import android.widget.ListAdapter;
|
|
||||||
|
|
||||||
import com.mobeta.android.dslv.DragSortController;
|
import com.mobeta.android.dslv.*;
|
||||||
import com.mobeta.android.dslv.DragSortListView;
|
|
||||||
|
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.ui.habits.list.controllers.CheckmarkButtonController;
|
import org.isoron.uhabits.ui.habits.list.controllers.*;
|
||||||
import org.isoron.uhabits.ui.habits.list.controllers.HabitCardController;
|
import org.isoron.uhabits.ui.habits.list.model.*;
|
||||||
import org.isoron.uhabits.ui.habits.list.model.HabitCardListAdapter;
|
|
||||||
|
|
||||||
public class HabitCardListView extends DragSortListView
|
public class HabitCardListView extends RecyclerView
|
||||||
{
|
{
|
||||||
@Nullable
|
@Nullable
|
||||||
private HabitCardListAdapter adapter;
|
private HabitCardListAdapter adapter;
|
||||||
@@ -45,9 +42,11 @@ public class HabitCardListView extends DragSortListView
|
|||||||
public HabitCardListView(Context context, AttributeSet attrs)
|
public HabitCardListView(Context context, AttributeSet attrs)
|
||||||
{
|
{
|
||||||
super(context, attrs);
|
super(context, attrs);
|
||||||
setFloatViewManager(new ViewManager());
|
// setFloatViewManager(new ViewManager());
|
||||||
setDragEnabled(true);
|
// setDragEnabled(true);
|
||||||
setLongClickable(true);
|
setLongClickable(true);
|
||||||
|
setHasFixedSize(true);
|
||||||
|
setLayoutManager(new LinearLayoutManager(getContext()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,14 +62,12 @@ public class HabitCardListView extends DragSortListView
|
|||||||
* @param selected true if the card is selected, false otherwise
|
* @param selected true if the card is selected, false otherwise
|
||||||
* @return the HabitCardView generated
|
* @return the HabitCardView generated
|
||||||
*/
|
*/
|
||||||
public View buildCardView(@Nullable HabitCardView cardView,
|
public View bindCardView(@NonNull HabitCardView cardView,
|
||||||
@NonNull Habit habit,
|
@NonNull Habit habit,
|
||||||
int score,
|
int score,
|
||||||
int[] checkmarks,
|
int[] checkmarks,
|
||||||
boolean selected)
|
boolean selected)
|
||||||
{
|
{
|
||||||
if (cardView == null) cardView = new HabitCardView(getContext());
|
|
||||||
|
|
||||||
cardView.setHabit(habit);
|
cardView.setHabit(habit);
|
||||||
cardView.setSelected(selected);
|
cardView.setSelected(selected);
|
||||||
cardView.setCheckmarkValues(checkmarks);
|
cardView.setCheckmarkValues(checkmarks);
|
||||||
@@ -87,8 +84,13 @@ public class HabitCardListView extends DragSortListView
|
|||||||
return cardView;
|
return cardView;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public View createCardView()
|
||||||
|
{
|
||||||
|
return new HabitCardView(getContext());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setAdapter(ListAdapter adapter)
|
public void setAdapter(RecyclerView.Adapter adapter)
|
||||||
{
|
{
|
||||||
this.adapter = (HabitCardListAdapter) adapter;
|
this.adapter = (HabitCardListAdapter) adapter;
|
||||||
super.setAdapter(adapter);
|
super.setAdapter(adapter);
|
||||||
@@ -97,18 +99,18 @@ public class HabitCardListView extends DragSortListView
|
|||||||
public void setController(@Nullable Controller controller)
|
public void setController(@Nullable Controller controller)
|
||||||
{
|
{
|
||||||
this.controller = controller;
|
this.controller = controller;
|
||||||
setDropListener(controller);
|
// setDropListener(controller);
|
||||||
setDragListener(controller);
|
// setDragListener(controller);
|
||||||
setOnItemClickListener(null);
|
// setOnItemClickListener(null);
|
||||||
setOnLongClickListener(null);
|
setOnLongClickListener(null);
|
||||||
|
|
||||||
if (controller == null) return;
|
if (controller == null) return;
|
||||||
|
|
||||||
setOnItemClickListener((p, v, pos, id) -> controller.onItemClick(pos));
|
// setOnItemClickListener((p, v, pos, id) -> controller.onItemClick(pos));
|
||||||
setOnItemLongClickListener((p, v, pos, id) -> {
|
// setOnItemLongClickListener((p, v, pos, id) -> {
|
||||||
controller.onItemLongClick(pos);
|
// controller.onItemLongClick(pos);
|
||||||
return true;
|
// return true;
|
||||||
});
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -127,32 +129,32 @@ public class HabitCardListView extends DragSortListView
|
|||||||
|
|
||||||
public interface Controller extends CheckmarkButtonController.Listener,
|
public interface Controller extends CheckmarkButtonController.Listener,
|
||||||
HabitCardController.Listener,
|
HabitCardController.Listener,
|
||||||
DropListener,
|
DragSortListView.DropListener,
|
||||||
DragListener
|
DragSortListView.DragListener
|
||||||
{
|
{
|
||||||
void onItemClick(int pos);
|
void onItemClick(int pos);
|
||||||
|
|
||||||
void onItemLongClick(int pos);
|
void onItemLongClick(int pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ViewManager extends DragSortController
|
// private class ViewManager extends DragSortController
|
||||||
{
|
// {
|
||||||
public ViewManager()
|
// public ViewManager()
|
||||||
{
|
// {
|
||||||
super(HabitCardListView.this);
|
// super(HabitCardListView.this);
|
||||||
setRemoveEnabled(false);
|
// setRemoveEnabled(false);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public View onCreateFloatView(int position)
|
// public View onCreateFloatView(int position)
|
||||||
{
|
// {
|
||||||
if (adapter == null) return null;
|
// if (adapter == null) return null;
|
||||||
return adapter.getView(position, null, null);
|
// return adapter.getView(position, null, null);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
@Override
|
// @Override
|
||||||
public void onDestroyFloatView(View floatView)
|
// public void onDestroyFloatView(View floatView)
|
||||||
{
|
// {
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,6 +88,10 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user