Last active
August 29, 2015 14:05
-
-
Save fei-ke/87810fbaa3bee52fb8a5 to your computer and use it in GitHub Desktop.
适用于ItemView的列表适配器
This file contains 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
/** | |
* 适用于ItemView的列表适配器 | |
* Created by fei-ke on 2014/8/19. | |
*/ | |
public class AbsItemAdapter<T extends IItemView, K extends IItemBean> extends BaseAdapter { | |
private List<K> mData; | |
private Class<T> itemViewClass; | |
public AbsItemAdapter(Class<T> itemViewClass) { | |
this.itemViewClass = itemViewClass; | |
} | |
@Override | |
public int getCount() { | |
return mData == null ? 0 : mData.size(); | |
} | |
@Override | |
public K getItem(int position) { | |
return mData.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
T itemView = null; | |
if (convertView == null) { | |
itemView = getItemViewInstance(parent.getContext()); | |
} else { | |
itemView = (T) convertView; | |
} | |
K item = getItem(position); | |
itemView.bindValue(item); | |
return itemView.getView(); | |
} | |
private T getItemViewInstance(Context context) { | |
T itemView = null; | |
try { | |
Method methodNewInstance = itemViewClass.getMethod("newInstance", Context.class); | |
itemView = (T) methodNewInstance.invoke(itemViewClass, context); | |
} catch (NoSuchMethodException e) { | |
e.printStackTrace(); | |
} catch (InvocationTargetException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
return itemView; | |
} | |
public void update(List<K> beans) { | |
if (mData == null) { | |
mData = new ArrayList<K>(); | |
} | |
mData.clear(); | |
loadMore(beans); | |
} | |
public void loadMore(List<K> beans) { | |
if (mData == null) { | |
mData = new ArrayList<K>(); | |
} | |
mData.addAll(beans); | |
notifyDataSetChanged(); | |
} | |
} |
This file contains 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
/** | |
* Created by fei-ke on 2014/8/19. | |
*/ | |
public interface IItemBean { | |
} |
This file contains 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
/** | |
* 同于AbsItemAdapter的ItemView | |
* Created by fei-ke on 2014/8/19. | |
*/ | |
public interface IItemView<T extends IItemBean> { | |
public void bindValue(T itemBean); | |
public View getView(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment