BaseVH and BaseAdapter finished
This commit is contained in:
parent
402255b4a0
commit
454a84ca72
@ -1,25 +1,100 @@
|
||||
package com.philliphsu.clock2;
|
||||
|
||||
import android.support.v7.util.SortedList;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.util.SortedListAdapterCallback;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Phillip Hsu on 5/31/2016.
|
||||
*/
|
||||
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder<T>> {
|
||||
public abstract class BaseAdapter<T, VH extends BaseViewHolder<T>> extends RecyclerView.Adapter<VH> {
|
||||
|
||||
private final OnListItemInteractionListener<T> mListener;
|
||||
private final SortedList<T> mItems;
|
||||
|
||||
protected BaseAdapter(Class<T> cls, List<T> items, OnListItemInteractionListener<T> listener) {
|
||||
mItems = new SortedList<>(cls, new SortedListAdapterCallback<T>(this) {
|
||||
@Override
|
||||
public BaseViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
return null;
|
||||
public int compare(T o1, T o2) {
|
||||
//return BaseAdapter.this.<T, VH>*compare(o1, o2); // *: See note below
|
||||
return BaseAdapter.this.compare(o1, o2);
|
||||
// Only need to specify the type when calling a _generic_ method
|
||||
// that defines _its own type parameters_ in its signature, but even
|
||||
// then, it's not actually necessary because the compiler can
|
||||
// infer the type.
|
||||
// This is just an _abstract_ method that takes params of type T.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(BaseViewHolder<T> holder, int position) {
|
||||
|
||||
public boolean areContentsTheSame(T oldItem, T newItem) {
|
||||
return BaseAdapter.this.areContentsTheSame(oldItem, newItem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return 0;
|
||||
public boolean areItemsTheSame(T item1, T item2) {
|
||||
return BaseAdapter.this.areItemsTheSame(item1, item2);
|
||||
}
|
||||
});
|
||||
mListener = listener;
|
||||
mItems.addAll(items);
|
||||
}
|
||||
|
||||
protected abstract VH onCreateViewHolder(ViewGroup parent, OnListItemInteractionListener<T> listener);
|
||||
|
||||
protected abstract int compare(T o1, T o2);
|
||||
|
||||
protected abstract boolean areContentsTheSame(T oldItem, T newItem);
|
||||
|
||||
protected abstract boolean areItemsTheSame(T item1, T item2);
|
||||
|
||||
@Override // not final to allow subclasses to use the viewType if needed
|
||||
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
return onCreateViewHolder(parent, mListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void onBindViewHolder(VH holder, int position) {
|
||||
holder.onBind(mItems.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int getItemCount() {
|
||||
return mItems.size();
|
||||
}
|
||||
|
||||
public final void replaceData(List<T> items) {
|
||||
mItems.clear();
|
||||
mItems.addAll(items);
|
||||
}
|
||||
|
||||
protected final T getItem(int position) {
|
||||
return mItems.get(position);
|
||||
}
|
||||
|
||||
public final int addItem(T item) {
|
||||
return mItems.add(item);
|
||||
}
|
||||
|
||||
public final boolean removeItem(T item) {
|
||||
return mItems.remove(item);
|
||||
}
|
||||
|
||||
public final void updateItem(T oldItem, T newItem) {
|
||||
// SortedList finds the index of an item by using its callback's compare() method.
|
||||
// We can describe our current item update process is as follows:
|
||||
// * An item's fields are modified
|
||||
// * The changes are saved to the repository
|
||||
// * A item update callback is fired to the RV
|
||||
// * The RV calls its adapter's updateItem(), passing the instance of the modified item as both arguments
|
||||
// (because modifying an item keeps the same instance of the item)
|
||||
// * The SortedList tries to find the index of the param oldItem, but since its fields are changed,
|
||||
// the search may end up failing because compare() could return the wrong index.
|
||||
// A workaround is to copy construct the original item instance BEFORE you modify the fields.
|
||||
// Then, oldItem should point to the copied instance.
|
||||
// Alternatively, a better approach is to make items immutable.
|
||||
mItems.updateItemAt(mItems.indexOf(oldItem), newItem);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.philliphsu.clock2;
|
||||
|
||||
import android.content.Context;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.annotation.LayoutRes;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
@ -14,27 +15,36 @@ import butterknife.ButterKnife;
|
||||
*/
|
||||
public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
|
||||
private final OnClickListener<T> mOnClickListener;
|
||||
private final Context mContext;
|
||||
private final OnListItemInteractionListener<T> mListener;
|
||||
private T mItem;
|
||||
|
||||
public BaseViewHolder(ViewGroup parent, @LayoutRes int layoutRes, OnClickListener<T> listener) {
|
||||
public BaseViewHolder(ViewGroup parent, @LayoutRes int layoutRes, OnListItemInteractionListener<T> listener) {
|
||||
super(LayoutInflater.from(parent.getContext())
|
||||
.inflate(layoutRes, parent, false));
|
||||
ButterKnife.bind(this, itemView);
|
||||
mOnClickListener = listener;
|
||||
mContext = parent.getContext();
|
||||
mListener = listener;
|
||||
itemView.setOnClickListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call to super must be the first line in the overridden implementation,
|
||||
* so that the base class can keep a reference to the item parameter.
|
||||
*/
|
||||
@CallSuper
|
||||
public void onBind(T item) {
|
||||
mItem = item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void onClick(View v) {
|
||||
mOnClickListener.onClick(mItem);
|
||||
public final Context getContext() {
|
||||
return mContext;
|
||||
}
|
||||
|
||||
public interface OnClickListener<T> {
|
||||
void onClick(T item);
|
||||
@Override
|
||||
public final void onClick(View v) {
|
||||
if (mListener != null) {
|
||||
mListener.onListItemInteraction(mItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ import android.widget.TextView;
|
||||
import com.philliphsu.clock2.alarms.AlarmsFragment;
|
||||
import com.philliphsu.clock2.ringtone.RingtoneActivity;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements AlarmsFragment.OnListFragmentInteractionListener {
|
||||
public class MainActivity extends AppCompatActivity implements AlarmsFragment.OnAlarmInteractionListener {
|
||||
|
||||
/**
|
||||
* The {@link android.support.v4.view.PagerAdapter} that will provide
|
||||
@ -179,7 +179,7 @@ public class MainActivity extends AppCompatActivity implements AlarmsFragment.On
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onListFragmentInteraction(Alarm item) {
|
||||
public void onListItemInteraction(Alarm item) {
|
||||
// TODO react to click
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
package com.philliphsu.clock2;
|
||||
|
||||
/**
|
||||
* Created by Phillip Hsu on 5/31/2016.
|
||||
* This interface MUST be extended by Fragments that display a RecyclerView as a list.
|
||||
* The reason for this is Fragments need to do an instanceof check on their host Context
|
||||
* to see if it implements this interface, and instanceof cannot be used with generic type
|
||||
* parameters. Why not just define this interface as a member of the Fragment class?
|
||||
* Because the Fragment's BaseAdapter needs a reference to this interface, and we don't want
|
||||
* to couple the BaseAdapter
|
||||
* to the Fragment. By keeping this interface as generic as possible, the BaseAdapter can
|
||||
* be easily adapted to not just Fragments, but also custom Views, Activities, etc.
|
||||
*/
|
||||
public interface OnListItemInteractionListener<T> {
|
||||
void onListItemInteraction(T item);
|
||||
}
|
||||
@ -1,27 +1,25 @@
|
||||
package com.philliphsu.clock2.alarms;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.CallSuper;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.SwitchCompat;
|
||||
import android.text.SpannableString;
|
||||
import android.text.Spanned;
|
||||
import android.text.format.DateFormat;
|
||||
import android.text.style.RelativeSizeSpan;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.BaseViewHolder;
|
||||
import com.philliphsu.clock2.DaysOfWeek;
|
||||
import com.philliphsu.clock2.OnListItemInteractionListener;
|
||||
import com.philliphsu.clock2.R;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
@ -30,13 +28,9 @@ import static com.philliphsu.clock2.DaysOfWeek.NUM_DAYS;
|
||||
/**
|
||||
* Created by Phillip Hsu on 5/31/2016.
|
||||
*/
|
||||
public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
|
||||
public class AlarmViewHolder extends BaseViewHolder<Alarm> {
|
||||
private static final RelativeSizeSpan AMPM_SIZE_SPAN = new RelativeSizeSpan(0.5f);
|
||||
|
||||
private final Context mContext;
|
||||
private final AlarmsFragment.OnListFragmentInteractionListener mListener;
|
||||
private Alarm mItem;
|
||||
|
||||
@Bind(R.id.time) TextView mTime;
|
||||
@Bind(R.id.on_off_switch) SwitchCompat mSwitch;
|
||||
@Bind(R.id.label) TextView mLabel;
|
||||
@ -44,27 +38,15 @@ public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
@Bind(R.id.recurring_days) TextView mDays;
|
||||
@Bind(R.id.dismiss) Button mDismissButton;
|
||||
|
||||
/*public AlarmViewHolder(ViewGroup parent, BaseViewHolder.OnClickListener<Alarm> listener) {
|
||||
|
||||
}*/
|
||||
|
||||
public AlarmViewHolder(View view, AlarmsFragment.OnListFragmentInteractionListener listener) {
|
||||
super(view);
|
||||
ButterKnife.bind(this, view);
|
||||
mContext = view.getContext();
|
||||
mListener = listener;
|
||||
view.setOnClickListener(this);
|
||||
public AlarmViewHolder(ViewGroup parent, OnListItemInteractionListener<Alarm> listener) {
|
||||
super(parent, R.layout.item_alarm, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call to super must be the first line in the overridden implementation,
|
||||
* so that the base class can keep a reference to the item parameter.
|
||||
*/
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onBind(Alarm alarm) {
|
||||
mItem = alarm;
|
||||
String time = DateFormat.getTimeFormat(mContext).format(new Date(alarm.ringsAt()));
|
||||
if (DateFormat.is24HourFormat(mContext)) {
|
||||
super.onBind(alarm);
|
||||
String time = DateFormat.getTimeFormat(getContext()).format(new Date(alarm.ringsAt()));
|
||||
if (DateFormat.is24HourFormat(getContext())) {
|
||||
mTime.setText(time);
|
||||
} else {
|
||||
// No way around having to construct this on binding
|
||||
@ -78,7 +60,7 @@ public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
//TODO:mCountdown.showAsText(alarm.ringsIn());
|
||||
mCountdown.setVisibility(VISIBLE);
|
||||
//todo:mCountdown.getTickHandler().startTicking(true)
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
// how many hours before alarm is considered upcoming
|
||||
// TODO: shared prefs
|
||||
/*int hoursBeforeUpcoming = Integer.parseInt(prefs.getString(
|
||||
@ -110,14 +92,14 @@ public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
if (numRecurringDays > 0) {
|
||||
String text;
|
||||
if (numRecurringDays == NUM_DAYS) {
|
||||
text = mContext.getString(R.string.every_day);
|
||||
text = getContext().getString(R.string.every_day);
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; // ordinal number, i.e. the position in the week, not an actual day!
|
||||
i < NUM_DAYS; i++) {
|
||||
if (alarm.isRecurring(i)) { // Is the i-th day in the week recurring?
|
||||
// This is the actual day at the i-th position in the week.
|
||||
int weekDay = DaysOfWeek.getInstance(mContext).weekDay(i);
|
||||
int weekDay = DaysOfWeek.getInstance(getContext()).weekDay(i);
|
||||
sb.append(DaysOfWeek.getLabel(weekDay)).append(", ");
|
||||
}
|
||||
}
|
||||
@ -131,11 +113,4 @@ public class AlarmViewHolder extends RecyclerView.ViewHolder implements View.OnC
|
||||
mDays.setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (mListener != null) {
|
||||
mListener.onListFragmentInteraction(mItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,29 +1,25 @@
|
||||
package com.philliphsu.clock2.alarms;
|
||||
|
||||
import android.support.v7.util.SortedList;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.util.SortedListAdapterCallback;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.R;
|
||||
import com.philliphsu.clock2.BaseAdapter;
|
||||
import com.philliphsu.clock2.OnListItemInteractionListener;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link RecyclerView.Adapter} that can display a {@link Alarm} and makes a call to the
|
||||
* specified {@link AlarmsFragment.OnListFragmentInteractionListener}.
|
||||
*/
|
||||
public class AlarmsAdapter extends RecyclerView.Adapter<AlarmViewHolder> {
|
||||
public class AlarmsAdapter extends BaseAdapter<Alarm, AlarmViewHolder> {
|
||||
|
||||
private final SortedList<Alarm> mItems;
|
||||
private final AlarmsFragment.OnListFragmentInteractionListener mListener;
|
||||
public AlarmsAdapter(List<Alarm> alarms, OnListItemInteractionListener<Alarm> listener) {
|
||||
super(Alarm.class, alarms, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AlarmViewHolder onCreateViewHolder(ViewGroup parent, OnListItemInteractionListener<Alarm> listener) {
|
||||
return new AlarmViewHolder(parent, listener);
|
||||
}
|
||||
|
||||
public AlarmsAdapter(List<Alarm> alarms, AlarmsFragment.OnListFragmentInteractionListener listener) {
|
||||
mItems = new SortedList<>(Alarm.class, new SortedListAdapterCallback<Alarm>(this) {
|
||||
@Override
|
||||
public int compare(Alarm o1, Alarm o2) {
|
||||
return Long.compare(o1.ringsAt(), o2.ringsAt());
|
||||
@ -44,28 +40,4 @@ public class AlarmsAdapter extends RecyclerView.Adapter<AlarmViewHolder> {
|
||||
public boolean areItemsTheSame(Alarm item1, Alarm item2) {
|
||||
return item1.id() == item2.id();
|
||||
}
|
||||
});
|
||||
mItems.addAll(alarms);
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AlarmViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
// TODO: Move this to the BaseAdapter.
|
||||
View view = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_alarm, parent, false);
|
||||
return new AlarmViewHolder(view, mListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(final AlarmViewHolder holder, int position) {
|
||||
// TODO: Move this to the BaseAdapter.
|
||||
holder.onBind(mItems.get(position));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
// TODO: Move this to the BaseAdapter.
|
||||
return mItems.size();
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,13 +11,14 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import com.philliphsu.clock2.Alarm;
|
||||
import com.philliphsu.clock2.OnListItemInteractionListener;
|
||||
import com.philliphsu.clock2.R;
|
||||
import com.philliphsu.clock2.alarms.dummy.DummyContent;
|
||||
|
||||
/**
|
||||
* A fragment representing a list of Items.
|
||||
* <p/>
|
||||
* Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener}
|
||||
* Activities containing this fragment MUST implement the {@link OnAlarmInteractionListener}
|
||||
* interface.
|
||||
*/
|
||||
public class AlarmsFragment extends Fragment {
|
||||
@ -26,7 +27,7 @@ public class AlarmsFragment extends Fragment {
|
||||
private static final String ARG_COLUMN_COUNT = "column-count";
|
||||
// TODO: Customize parameters
|
||||
private int mColumnCount = 1;
|
||||
private OnListFragmentInteractionListener mListener;
|
||||
private OnAlarmInteractionListener mListener;
|
||||
|
||||
/**
|
||||
* Mandatory empty constructor for the fragment manager to instantiate the
|
||||
@ -76,11 +77,11 @@ public class AlarmsFragment extends Fragment {
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
if (context instanceof OnListFragmentInteractionListener) {
|
||||
mListener = (OnListFragmentInteractionListener) context;
|
||||
if (context instanceof OnAlarmInteractionListener) {
|
||||
mListener = (OnAlarmInteractionListener) context;
|
||||
} else {
|
||||
throw new RuntimeException(context.toString()
|
||||
+ " must implement OnListFragmentInteractionListener");
|
||||
+ " must implement OnAlarmInteractionListener");
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,7 +101,5 @@ public class AlarmsFragment extends Fragment {
|
||||
* "http://developer.android.com/training/basics/fragments/communicating.html"
|
||||
* >Communicating with Other Fragments</a> for more information.
|
||||
*/
|
||||
public interface OnListFragmentInteractionListener {
|
||||
void onListFragmentInteraction(Alarm item);
|
||||
}
|
||||
public interface OnAlarmInteractionListener extends OnListItemInteractionListener<Alarm> {}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user