Skip to content

Commit

Permalink
Merge pull request #16 from ribot/refactoring_base_easyadapter
Browse files Browse the repository at this point in the history
Refactoring EasyAdapter
  • Loading branch information
Ivan Carballo committed Sep 11, 2014
2 parents 59bb42a + 608104c commit 856082b
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 54 deletions.
2 changes: 1 addition & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "19.1.0"

defaultConfig {
minSdkVersion 8
minSdkVersion 14
targetSdkVersion 19
versionCode 2
versionName "1.1"
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
77 changes: 77 additions & 0 deletions library/src/main/java/uk/co/ribot/easyadapter/BaseEasyAdapter.java
Original file line number Diff line number Diff line change
@@ -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 <a href="http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder">"view holder"</a> 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 <T> Data type for items
*/
public abstract class BaseEasyAdapter<T> extends BaseAdapter {

private Class<? extends ItemViewHolder> 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<? extends ItemViewHolder> itemViewHolderClass) {
init(context, itemViewHolderClass);
}

private void init(Context context, Class<? extends ItemViewHolder> 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<T> 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;
}
}
52 changes: 8 additions & 44 deletions library/src/main/java/uk/co/ribot/easyadapter/EasyAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>do the tedious work for you.</b>
* You just have to implement an {@link uk.co.ribot.easyadapter.ItemViewHolder} and pass it into the constructor of this class.
* </p>
* It implements the <a href="http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder">"view holder"</a> 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 <b>do the tedious work for you.</b>
* You only have to implement an {@link uk.co.ribot.easyadapter.ItemViewHolder} and pass it into the constructor of this class.
* @param <T> Data type for items
*/
public class EasyAdapter<T> extends BaseAdapter {
public class EasyAdapter<T> extends BaseEasyAdapter<T> {

private List<T> mListItems;
private Class<? extends ItemViewHolder> 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.
Expand All @@ -45,8 +38,8 @@ public class EasyAdapter<T> extends BaseAdapter {
* @param listItems the list of items to load in the Adapter
*/
public EasyAdapter(Context context, Class<? extends ItemViewHolder> itemViewHolderClass, List<T> listItems) {
super(context, itemViewHolderClass);
setItems(listItems);
init(context, itemViewHolderClass);
}

/**
Expand All @@ -56,14 +49,8 @@ public EasyAdapter(Context context, Class<? extends ItemViewHolder> itemViewHold
* @param itemViewHolderClass your {@link uk.co.ribot.easyadapter.ItemViewHolder} implementation class
*/
public EasyAdapter(Context context, Class<? extends ItemViewHolder> itemViewHolderClass) {
super(context, itemViewHolderClass);
mListItems = new ArrayList<T>();
init(context, itemViewHolderClass);
}

private void init(Context context, Class<? extends ItemViewHolder> itemViewHolderClass) {
mItemViewHolderClass = itemViewHolderClass;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mItemLayoutId = EasyAdapterUtil.parseItemLayoutId(itemViewHolderClass);
}

/**
Expand Down Expand Up @@ -93,6 +80,7 @@ public void addItem(T item) {
*/
public void addItems(List<T> listItems) {
mListItems.addAll(listItems);
notifyDataSetChanged();
}

@Override
Expand All @@ -110,28 +98,4 @@ public T getItem(int position) {
return mListItems.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemViewHolder<T> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,4 @@ public static Integer parseItemLayoutId(Class<? extends ItemViewHolder> 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;
}

}

0 comments on commit 856082b

Please sign in to comment.