Created
January 8, 2014 03:49
-
-
Save john990/8311480 to your computer and use it in GitHub Desktop.
test
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.demo.view; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
public class AutoLoadAdapter<T> extends BaseAdapter { | |
private List<T[]> data; | |
private List<T[]> cache = new ArrayList<T[]>(); | |
private int count; | |
/** 每次加载的行数 */ | |
private int limit; | |
public AutoLoadAdapter(List<T[]> data, int limit) { | |
this.limit = limit; | |
int size = data.size(); | |
if (size > limit) { | |
cache.addAll(data.subList(limit, size)); | |
this.data = data.subList(0, limit); | |
} else { | |
this.data = data; | |
} | |
count = this.data.size(); | |
} | |
@Override | |
public int getCount() { | |
return count; | |
} | |
public int cacheSize() { | |
return cache.size(); | |
} | |
public void showCache() { | |
if (cache.size() <= limit) { | |
data.addAll(cache); | |
} else { | |
data.addAll(cache.subList(0, limit)); | |
List<T[]> tmpCache = new ArrayList<T[]>(); | |
tmpCache.addAll(cache); | |
cache.clear(); | |
cache.addAll(tmpCache.subList(limit, tmpCache.size())); | |
tmpCache.clear(); | |
} | |
count = data.size(); | |
} | |
public void addAll(List<T[]> add) { | |
if (add.size() <= limit) { | |
data.addAll(add); | |
} else { | |
cache.addAll(add.subList(limit, add.size())); | |
data.addAll(add.subList(0, limit)); | |
} | |
} | |
public void refresh() { | |
count = data.size(); | |
notifyDataSetChanged(); | |
} | |
public void addCache(List<T[]> add) { | |
cache.addAll(add); | |
} | |
@Override | |
public Object getItem(int position) { | |
return data.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
return convertView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment