com.joanzapata.android.QuickAdapter Maven / Gradle / Ivy
/**
* Copyright 2013 Joan Zapata
*
* 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 com.joanzapata.android;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import java.util.ArrayList;
import java.util.List;
import static com.joanzapata.android.BaseAdapterHelper.get;
/**
* Abstraction class of a BaseAdapter in which you only need
* to provide the convert() implementation.
* Using the provided BaseAdapterHelper, your code is minimalist.
* @param The type of the items in the list.
*/
public abstract class QuickAdapter extends BaseAdapter {
private static final String TAG = QuickAdapter.class.getSimpleName();
private final Context context;
private final int layoutResId;
private final List data;
private boolean displayIndeterminateProgress = false;
/**
* Create a QuickAdapter.
* @param context The context.
* @param layoutResId The layout resource id of each item.
*/
public QuickAdapter(Context context, int layoutResId) {
this(context, layoutResId, null);
}
/**
* Same as QuickAdapter#QuickAdapter(Context,int) but with
* some initialization data.
* @param context The context.
* @param layoutResId The layout resource id of each item.
* @param data A new list is created out of this one to avoid mutable list
*/
public QuickAdapter(Context context, int layoutResId, List data) {
this.data = data == null ? new ArrayList() : new ArrayList(data);
this.context = context;
this.layoutResId = layoutResId;
}
@Override
public int getCount() {
int extra = displayIndeterminateProgress ? 1 : 0;
return data.size() + extra;
}
@Override
public T getItem(int position) {
if (position >= data.size()) return null;
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position >= data.size() ? 1 : 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == 0) {
final BaseAdapterHelper helper = get(context, convertView, parent, layoutResId);
convert(helper, getItem(position));
return helper.getView();
}
return createIndeterminateProgressView(convertView, parent);
}
private View createIndeterminateProgressView(View convertView, ViewGroup parent) {
if (convertView == null) {
FrameLayout container = new FrameLayout(context);
container.setForegroundGravity(Gravity.CENTER);
ProgressBar progress = new ProgressBar(context);
container.addView(progress);
convertView = container;
}
return convertView;
}
@Override
public boolean isEnabled(int position) {
return position < data.size();
}
public void add(T elem) {
data.add(elem);
notifyDataSetChanged();
}
public void addAll(List elem) {
data.addAll(elem);
notifyDataSetChanged();
}
public void showIndeterminateProgress(boolean display) {
if (display == displayIndeterminateProgress) return;
displayIndeterminateProgress = display;
notifyDataSetChanged();
}
/**
* Implement this method and use the helper to adapt the view to the given item.
* @param helper A fully initialized helper.
* @param item The item that needs to be displayed.
*/
protected abstract void convert(BaseAdapterHelper helper, T item);
}