Created
August 21, 2014 20:35
-
-
Save jakewilson801/30f876082a7565c9aeb0 to your computer and use it in GitHub Desktop.
generic adapter android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.itv.util; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.BaseAdapter; | |
| public class DataDelegateAdapter<T> extends BaseAdapter { | |
| private DataAdapterDelegate<T> delegate; | |
| private boolean shouldLoadMoreData = true; | |
| private boolean isLoadingData = false; | |
| private int page; | |
| public DataDelegateAdapter(DataAdapterDelegate<T> delegate) { | |
| this.delegate = delegate; | |
| this.page = 0; | |
| } | |
| @Override | |
| public long getItemId(int position) { | |
| return 0; | |
| } | |
| @Override | |
| public View getView(int position, View convertView, ViewGroup parent) { | |
| if (position == getCount() - 1 && shouldLoadMoreData && !isLoadingData) { | |
| isLoadingData = true; | |
| page++; | |
| delegate.loadMoreData(page); | |
| } | |
| if(convertView == null) convertView = delegate.inflateView(); | |
| delegate.populateView(getItem(position), convertView); | |
| return convertView; | |
| } | |
| @Override | |
| public void notifyDataSetChanged() { | |
| super.notifyDataSetChanged(); | |
| isLoadingData = false; | |
| } | |
| public void notifyDataSetChanged(boolean shouldLoadMoreData) { | |
| super.notifyDataSetChanged(); | |
| this.isLoadingData = false; | |
| this.shouldLoadMoreData = shouldLoadMoreData; | |
| } | |
| @Override | |
| public void notifyDataSetInvalidated() { | |
| super.notifyDataSetInvalidated(); | |
| shouldLoadMoreData = true; | |
| isLoadingData = false; | |
| } | |
| @Override | |
| public int getCount() { | |
| return delegate.getCount(); | |
| } | |
| @Override | |
| public T getItem(int position) { | |
| return delegate.getItem(position); | |
| } | |
| public int getPage() { | |
| return page; | |
| } | |
| public interface DataAdapterDelegate<T> { | |
| int getCount(); | |
| T getItem(int position); | |
| void populateView(T item, View convertView); | |
| View inflateView(); | |
| void loadMoreData(int page); | |
| } | |
| public static abstract class DataAdapterDelegateImp<T> implements DataAdapterDelegate<T>{ | |
| @Override | |
| public abstract int getCount(); | |
| @Override | |
| public abstract T getItem(int position); | |
| @Override | |
| public abstract void populateView(T item, View convertView); | |
| @Override | |
| public abstract View inflateView(); | |
| @Override | |
| public void loadMoreData(int page) { } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment