diff --git a/demo/build.gradle b/demo/build.gradle index 50dc3fe..ae921e8 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -5,7 +5,7 @@ android { buildToolsVersion "19.1.0" defaultConfig { - minSdkVersion 8 + minSdkVersion 14 targetSdkVersion 19 versionCode 2 versionName "1.1" diff --git a/library/build.gradle b/library/build.gradle index 397510c..b66a336 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -14,7 +14,7 @@ apply plugin: 'maven' apply plugin: 'signing' apply plugin: 'android-test' -version = "1.1.0" +version = "1.2.0" group = "uk.co.ribot" dependencies { diff --git a/library/src/main/java/uk/co/ribot/easyadapter/BaseEasyAdapter.java b/library/src/main/java/uk/co/ribot/easyadapter/BaseEasyAdapter.java new file mode 100644 index 0000000..def12c8 --- /dev/null +++ b/library/src/main/java/uk/co/ribot/easyadapter/BaseEasyAdapter.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2014 Ribot Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.co.ribot.easyadapter; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; + +/** + * Abstract extension of BaseAdapter that implements the "view holder" design pattern. + * You should extend this class if your Adapter requires a data structure different to a List or it needs to provide some extra functionality + * to handle the data, i.e if you need a method to add items at the beginning of the list. If not simply use the provided implementation {@link uk.co.ribot.easyadapter.EasyAdapter} + * @param Data type for items + */ +public abstract class BaseEasyAdapter extends BaseAdapter { + + private Class mItemViewHolderClass; + private LayoutInflater mInflater; + private Integer mItemLayoutId; + + /** + * Constructs and EasyAdapter with a Context and an {@link uk.co.ribot.easyadapter.ItemViewHolder} class. + * + * @param context a valid Context + * @param itemViewHolderClass your {@link uk.co.ribot.easyadapter.ItemViewHolder} implementation class + */ + public BaseEasyAdapter(Context context, Class itemViewHolderClass) { + init(context, itemViewHolderClass); + } + + private void init(Context context, Class itemViewHolderClass) { + mItemViewHolderClass = itemViewHolderClass; + mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + mItemLayoutId = EasyAdapterUtil.parseItemLayoutId(itemViewHolderClass); + } + + @Override + public abstract T getItem(int position); + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ItemViewHolder holder; + if (convertView == null) { + convertView = mInflater.inflate(mItemLayoutId, parent, false); + //Create a new view holder using reflection + holder = EasyAdapterUtil.createViewHolder(convertView, mItemViewHolderClass); + holder.onSetListeners(); + if (convertView != null) convertView.setTag(holder); + } else { + //Reuse the view holder + holder = (ItemViewHolder) convertView.getTag(); + } + + T item = getItem(position); + holder.setItem(item); + PositionInfo positionInfo = new PositionInfo(position, position == 0, position == getCount() - 1); + holder.onSetValues(item, positionInfo); + + return convertView; + } +} diff --git a/library/src/main/java/uk/co/ribot/easyadapter/EasyAdapter.java b/library/src/main/java/uk/co/ribot/easyadapter/EasyAdapter.java index 9ad496a..d913fd9 100644 --- a/library/src/main/java/uk/co/ribot/easyadapter/EasyAdapter.java +++ b/library/src/main/java/uk/co/ribot/easyadapter/EasyAdapter.java @@ -16,26 +16,19 @@ package uk.co.ribot.easyadapter; import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; import java.util.ArrayList; import java.util.List; /** - * Extension of BaseAdapter. Don't worry about implementing your own Adapter, the EasyAdapter will do the tedious work for you. - * You just have to implement an {@link uk.co.ribot.easyadapter.ItemViewHolder} and pass it into the constructor of this class. - *

- * It implements the "view holder" design pattern so your ListView will scroll smoothly. + * Extension of BaseEasyAdapter that uses a List as data structure and provide methods to set a new list of items or add them to the existing List. + * Don't worry about implementing your own Adapter, the EasyAdapter will do the tedious work for you. + * You only have to implement an {@link uk.co.ribot.easyadapter.ItemViewHolder} and pass it into the constructor of this class. + * @param Data type for items */ -public class EasyAdapter extends BaseAdapter { +public class EasyAdapter extends BaseEasyAdapter { private List mListItems; - private Class mItemViewHolderClass; - private LayoutInflater mInflater; - private Integer mItemLayoutId; /** * Constructs and EasyAdapter with a Context, an {@link uk.co.ribot.easyadapter.ItemViewHolder} class, and list of items. @@ -45,8 +38,8 @@ public class EasyAdapter extends BaseAdapter { * @param listItems the list of items to load in the Adapter */ public EasyAdapter(Context context, Class itemViewHolderClass, List listItems) { + super(context, itemViewHolderClass); setItems(listItems); - init(context, itemViewHolderClass); } /** @@ -56,14 +49,8 @@ public EasyAdapter(Context context, Class itemViewHold * @param itemViewHolderClass your {@link uk.co.ribot.easyadapter.ItemViewHolder} implementation class */ public EasyAdapter(Context context, Class itemViewHolderClass) { + super(context, itemViewHolderClass); mListItems = new ArrayList(); - init(context, itemViewHolderClass); - } - - private void init(Context context, Class itemViewHolderClass) { - mItemViewHolderClass = itemViewHolderClass; - mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - mItemLayoutId = EasyAdapterUtil.parseItemLayoutId(itemViewHolderClass); } /** @@ -93,6 +80,7 @@ public void addItem(T item) { */ public void addItems(List listItems) { mListItems.addAll(listItems); + notifyDataSetChanged(); } @Override @@ -110,28 +98,4 @@ public T getItem(int position) { return mListItems.get(position); } - @Override - public View getView(int position, View convertView, ViewGroup parent) { - ItemViewHolder holder; - if (convertView == null) { - convertView = mInflater.inflate(mItemLayoutId, parent, false); - //Create a new view holder using reflection - holder = EasyAdapterUtil.createViewHolder(convertView, mItemViewHolderClass); - holder.onSetListeners(); - if (convertView != null) - convertView.setTag(holder); - } else { - //Reuse the view holder - holder = (ItemViewHolder) convertView.getTag(); - } - - T item = getItem(position); - holder.setItem(item); - PositionInfo positionInfo = new PositionInfo(position, EasyAdapterUtil.isFirst(position), EasyAdapterUtil.isLast(position, mListItems)); - holder.onSetValues(item, positionInfo); - - - return convertView; - } - } diff --git a/library/src/main/java/uk/co/ribot/easyadapter/EasyAdapterUtil.java b/library/src/main/java/uk/co/ribot/easyadapter/EasyAdapterUtil.java index ec8d1da..a6798c4 100644 --- a/library/src/main/java/uk/co/ribot/easyadapter/EasyAdapterUtil.java +++ b/library/src/main/java/uk/co/ribot/easyadapter/EasyAdapterUtil.java @@ -35,12 +35,4 @@ public static Integer parseItemLayoutId(Class itemView return itemLayoutId; } - public static boolean isLast(int position, List listItems) { - return position == listItems.size() - 1; - } - - public static boolean isFirst(int position) { - return position == 0; - } - }