Replace ListView with RecyclerView

pull/145/head
Alinson S. Xavier 9 years ago
parent dbe268b8e9
commit b33420cabb

@ -141,6 +141,6 @@ public class ListHabitsRootView extends BaseRootView
private void updateEmptyView()
{
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()
{
adapter.clearSelection();
view.setDragEnabled(true);
// view.setDragEnabled(true);
activeMode = new NormalMode();
if (selectionListener != null) selectionListener.onSelectionFinish();

@ -20,8 +20,8 @@
package org.isoron.uhabits.ui.habits.list.model;
import android.support.annotation.*;
import android.support.v7.widget.*;
import android.view.*;
import android.widget.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.models.*;
@ -35,7 +35,8 @@ import java.util.*;
* The data if fetched and cached by a {@link HabitCardListCache}. This adapter
* 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
{
@NonNull
@ -61,6 +62,8 @@ public class HabitCardListAdapter extends BaseAdapter
cache = new HabitCardListCache(allHabits);
cache.setListener(this);
cache.setCheckmarkCount(checkmarkCount);
setHasStableIds(true);
}
public void cancelRefresh()
@ -78,7 +81,7 @@ public class HabitCardListAdapter extends BaseAdapter
}
@Override
public int getCount()
public int getItemCount()
{
return cache.getHabitCount();
}
@ -90,13 +93,37 @@ public class HabitCardListAdapter extends BaseAdapter
* @return the item at given position
* @throws IndexOutOfBoundsException if position is not valid
*/
@Override
@Deprecated
@NonNull
public Habit getItem(int 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
public long getItemId(int position)
{
@ -115,21 +142,21 @@ public class HabitCardListAdapter extends BaseAdapter
return new LinkedList<>(selected);
}
@Override
public View getView(int position,
@Nullable View view,
@Nullable ViewGroup parent)
{
if (listView == null) return null;
Habit habit = cache.getHabitByPosition(position);
int score = cache.getScore(habit.getId());
int checkmarks[] = cache.getCheckmarks(habit.getId());
boolean selected = this.selected.contains(habit);
return listView.buildCardView((HabitCardView) view, habit, score,
checkmarks, selected);
}
// @Override
// public View getView(int position,
// @Nullable View view,
// @Nullable ViewGroup parent)
// {
// if (listView == null) return null;
//
// Habit habit = cache.getHabitByPosition(position);
// int score = cache.getScore(habit.getId());
// int checkmarks[] = cache.getCheckmarks(habit.getId());
// boolean selected = this.selected.contains(habit);
//
// return listView.buildCardView((HabitCardView) view, habit, score,
// checkmarks, selected);
// }
/**
* Returns whether list of selected items is empty.
@ -203,12 +230,6 @@ public class HabitCardListAdapter extends BaseAdapter
this.listView = listView;
}
public void setShowArchived(boolean showArchived)
{
cache.setIncludeArchived(showArchived);
cache.refreshAllHabits(true);
}
/**
* Selects or deselects the item at a given position.
*

@ -102,16 +102,6 @@ public class HabitCardListCache implements CommandRunner.Listener
return data.habits.size();
}
public boolean getIncludeArchived()
{
return includeArchived;
}
public void setIncludeArchived(boolean includeArchived)
{
this.includeArchived = includeArchived;
}
@Nullable
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;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListAdapter;
import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;
import org.isoron.uhabits.models.Habit;
import org.isoron.uhabits.ui.habits.list.controllers.CheckmarkButtonController;
import org.isoron.uhabits.ui.habits.list.controllers.HabitCardController;
import org.isoron.uhabits.ui.habits.list.model.HabitCardListAdapter;
public class HabitCardListView extends DragSortListView
import android.content.*;
import android.support.annotation.*;
import android.support.v7.widget.*;
import android.util.*;
import android.view.*;
import com.mobeta.android.dslv.*;
import org.isoron.uhabits.models.*;
import org.isoron.uhabits.ui.habits.list.controllers.*;
import org.isoron.uhabits.ui.habits.list.model.*;
public class HabitCardListView extends RecyclerView
{
@Nullable
private HabitCardListAdapter adapter;
@ -45,9 +42,11 @@ public class HabitCardListView extends DragSortListView
public HabitCardListView(Context context, AttributeSet attrs)
{
super(context, attrs);
setFloatViewManager(new ViewManager());
setDragEnabled(true);
// setFloatViewManager(new ViewManager());
// setDragEnabled(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
* @return the HabitCardView generated
*/
public View buildCardView(@Nullable HabitCardView cardView,
public View bindCardView(@NonNull HabitCardView cardView,
@NonNull Habit habit,
int score,
int[] checkmarks,
boolean selected)
{
if (cardView == null) cardView = new HabitCardView(getContext());
cardView.setHabit(habit);
cardView.setSelected(selected);
cardView.setCheckmarkValues(checkmarks);
@ -87,8 +84,13 @@ public class HabitCardListView extends DragSortListView
return cardView;
}
public View createCardView()
{
return new HabitCardView(getContext());
}
@Override
public void setAdapter(ListAdapter adapter)
public void setAdapter(RecyclerView.Adapter adapter)
{
this.adapter = (HabitCardListAdapter) adapter;
super.setAdapter(adapter);
@ -97,18 +99,18 @@ public class HabitCardListView extends DragSortListView
public void setController(@Nullable Controller controller)
{
this.controller = controller;
setDropListener(controller);
setDragListener(controller);
setOnItemClickListener(null);
// setDropListener(controller);
// setDragListener(controller);
// setOnItemClickListener(null);
setOnLongClickListener(null);
if (controller == null) return;
setOnItemClickListener((p, v, pos, id) -> controller.onItemClick(pos));
setOnItemLongClickListener((p, v, pos, id) -> {
controller.onItemLongClick(pos);
return true;
});
// setOnItemClickListener((p, v, pos, id) -> controller.onItemClick(pos));
// setOnItemLongClickListener((p, v, pos, id) -> {
// controller.onItemLongClick(pos);
// return true;
// });
}
@Override
@ -127,32 +129,32 @@ public class HabitCardListView extends DragSortListView
public interface Controller extends CheckmarkButtonController.Listener,
HabitCardController.Listener,
DropListener,
DragListener
DragSortListView.DropListener,
DragSortListView.DragListener
{
void onItemClick(int pos);
void onItemLongClick(int pos);
}
private class ViewManager extends DragSortController
{
public ViewManager()
{
super(HabitCardListView.this);
setRemoveEnabled(false);
}
@Override
public View onCreateFloatView(int position)
{
if (adapter == null) return null;
return adapter.getView(position, null, null);
}
@Override
public void onDestroyFloatView(View floatView)
{
}
}
// private class ViewManager extends DragSortController
// {
// public ViewManager()
// {
// super(HabitCardListView.this);
// setRemoveEnabled(false);
// }
//
// @Override
// public View onCreateFloatView(int position)
// {
// if (adapter == null) return null;
// return adapter.getView(position, null, null);
// }
//
// @Override
// public void onDestroyFloatView(View floatView)
// {
// }
// }
}

@ -88,6 +88,10 @@ public class HabitCardView extends FrameLayout
checkmarkPanel.setController(null);
if (controller == null) return;
setOnClickListener(v -> {
});
checkmarkPanel.setController(controller);
}

Loading…
Cancel
Save