标签:万能适配器
转载请申明:http://blog.csdn.net/yoyo_newbie/article/details/48130813
没次当我们用RecyclerView是时候,不得不想到要添加点击事件, 头部,尾部,甚至分组繁琐代码烦恼,于是本人就无奈写了共用的adapter。
使用说明:
如,item资源为
R.layout.fragment_jiongtu_item_of_indexfregment
item里面有个imageview id为
R.id.iv_photo
private class JionTuAdapter extends BaseRecyclerViewAdapter { public JionTuAdapter(Context context) { super(context); } @Override protected int getContentResourse() { return R.layout.fragment_jiongtu_item_of_indexfregment; } @Override protected int[] setContentViewsIds() { return new int[]{R.id.iv_photo}; } int i = 0; @Override protected void onContentViewUpdate(BaseRecyclerViewAdapter adapter, View view, Object object) { //这个object就是ItemType.data ImageView photoView = (ImageView) adapter.getContentViewItemViewById(R.id.iv_photo); photoView.setImageResource((Integer) object); } }
这样,我就可以轻松实现了这个适配器!
然后,测试
recyclerView.setAdapter(adapter = new JionTuAdapter(getActivity()));//添加内容点击事件 adapter.setOnContentViewListener(new BaseRecyclerViewAdapter.OnContentViewListener() { @Override public void onClicked(View view, BaseRecyclerViewAdapter adapter, Object object) { Snackbar.make(view,"呵呵",Snackbar.LENGTH_SHORT).show(); } });List<JionTuAdapter.ItemType> list = new ArrayList<JionTuAdapter.ItemType>(); for(int i =0; i < 10;i++) { //把数据都添加到内容列表list.add(new BaseRecyclerViewAdapter.ItemType(BaseRecyclerViewAdapter.ItemType.TYPE_CONTENT,R.drawable.ic_launcher));} adapter.update(list, false);//false代表不需要提前清空列表
如果要添加头部,类似上面的adapter,分别实现
getHeaderResourse
setHeaderViewsIds
onHeaderViewUpdate
三个方法。如果没有id,记得提供null
如
http://blog.csdn.net/yoyo_newbie/article/details/48108417
/** * 首页的囧图 * @author Sam * * 下午3:17:39 */ public class JiongTuFragmentOfIndexFragment extends Fragment { public View convertView; private PullToRefreshStaggeredGridLayout videosList; private JionTuAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { convertView = inflater.inflate(R.layout.fragment_jiongtu_of_indexfregment, container, false); init(convertView); return convertView; } private void init(View convertView) { videosList = (PullToRefreshStaggeredGridLayout) convertView.findViewById(R.id.lv_index_jiongtu); } @Override public void onResume() { super.onResume(); RecyclerView recyclerView = videosList.getRefreshableView(); recyclerView.setAdapter(adapter = new JionTuAdapter(getActivity())); adapter.setOnContentViewListener(new BaseRecyclerViewAdapter.OnContentViewListener() { @Override public void onClicked(View view, BaseRecyclerViewAdapter adapter, Object object) { Snackbar.make(view,"呵呵",Snackbar.LENGTH_SHORT).show(); } }); List<JionTuAdapter.ItemType> list = new ArrayList<JionTuAdapter.ItemType>(); for(int i =0; i < 10;i++) { //把数据都添加到内容列表 list.add(new BaseRecyclerViewAdapter.ItemType(BaseRecyclerViewAdapter.ItemType.TYPE_CONTENT,null)); } adapter.update(list, false);//false代表不需要提前清空列表 } private class JionTuAdapter extends BaseRecyclerViewAdapter { public JionTuAdapter(Context context) { super(context); } @Override protected int getContentResourse() { return R.layout.fragment_jiongtu_item_of_indexfregment; } @Override protected int[] setContentViewsIds() { return new int[]{R.id.iv_photo}; } int i = 0; @Override protected void onContentViewUpdate(BaseRecyclerViewAdapter adapter, View view, Object object) { //这个object就是ItemType.data ImageView photoView = (ImageView) adapter.getContentViewItemViewById(R.id.iv_photo); photoView.setImageResource(i++%2==0?R.mipmap.dota2_meizi1:R.mipmap.dota2_meizi2); } } }
万能适配器
/** * 自定義RecyelerView Adapter */ public abstract class BaseRecyclerViewAdapter extends RecyclerView.Adapter { private Context context; public BaseRecyclerViewAdapter(Context context) { this.context = context; initHeaderViewsIds(); initFooterViewsIds(); initContentViewsIds(); initContentViewsGroupIds(); } private List<ItemType> itemTypes = new ArrayList<ItemType>(); protected int getFooterResourse() { return -1; } protected int getHeaderResourse() { return -1; } protected abstract int getContentResourse(); protected int getContentGroupResourse() { return -1; } protected int[] setHeaderViewsIds() { return null; } protected int[] setFooterViewsIds() { return null; } protected abstract int[] setContentViewsIds(); protected int[] setContentViewsGroupIds() { return null; } protected abstract void onContentViewUpdate(BaseRecyclerViewAdapter adapter, View view,Object object); private Map<Integer, View> headerViews = new HashMap<Integer, View>(); private Map<Integer, View> contentViews = new HashMap<Integer, View>(); private Map<Integer, View> contentViewGroups = new HashMap<Integer, View>(); private Map<Integer, View> footViews = new HashMap<Integer, View>(); public void update( List<ItemType> itemTypes, boolean isClear ) { if(itemTypes.isEmpty()) return; if(isClear) itemTypes.clear(); this.itemTypes.addAll(itemTypes); notifyDataSetChanged(); } public List<ItemType> getItemTypes() { return itemTypes; } private void initHeaderViewsIds() { int [] ids = setHeaderViewsIds(); if(null == ids) return; for (int id : ids) { headerViews.put(id, null); } } private void initFooterViewsIds() { int[] ids = setFooterViewsIds(); if(null == ids) return; for (int id : ids) { footViews.put(id, null); } } private void initContentViewsIds() { int[] ids = setContentViewsIds(); if(null == ids) return; for (int id : ids) { contentViews.put(id, null); } } private void initContentViewsGroupIds() { int[] ids = setContentViewsGroupIds(); if(null == ids) return; for (int id : ids) { contentViewGroups.put(id, null); } } private class HeaderViewHolder extends RecyclerView.ViewHolder{ public HeaderViewHolder(View itemView) { super(itemView); loadViews(itemView, headerViews); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(null != BaseRecyclerViewAdapter.this.headerViewViewListener) BaseRecyclerViewAdapter.this.headerViewViewListener.onClicked(v, BaseRecyclerViewAdapter.this, v.getTag()); } }); } } private class FooterViewHolder extends RecyclerView.ViewHolder{ public FooterViewHolder(View itemView) { super(itemView); loadViews(itemView, footViews); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(null != BaseRecyclerViewAdapter.this.footerViewListener) BaseRecyclerViewAdapter.this.footerViewListener.onClicked(v, BaseRecyclerViewAdapter.this, v.getTag()); } }); } } protected void onHeaderViewUpdate(BaseRecyclerViewAdapter adapter, View view, Object object) { } protected void onFooterViewUpdate(BaseRecyclerViewAdapter adapter, View view,Object object) { } protected void onContentViewGroupUpdate(BaseRecyclerViewAdapter adapter, View view,Object object) { } private class ContentViewHolder extends RecyclerView.ViewHolder{ public ContentViewHolder(View itemView) { super(itemView); loadViews(itemView, contentViews); if(itemView instanceof AdapterView) return; itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(null != BaseRecyclerViewAdapter.this.contentViewListener) BaseRecyclerViewAdapter.this.contentViewListener.onClicked(v, BaseRecyclerViewAdapter.this, v.getTag()); } }); } } private class ContentViewGroupHolder extends RecyclerView.ViewHolder{ public ContentViewGroupHolder(View itemView) { super(itemView); loadViews(itemView, contentViewGroups); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(null != BaseRecyclerViewAdapter.this.contentGroupViewListener) BaseRecyclerViewAdapter.this.contentGroupViewListener.onClicked(v, BaseRecyclerViewAdapter.this, v.getTag()); } }); } } private void loadViews(View itemView, Map<Integer, View> views) { if(!views.isEmpty()) { Set<Map.Entry<Integer, View>> set = views.entrySet(); for(Map.Entry<Integer, View> integerEntry : set) { views.put(integerEntry.getKey(),itemView.findViewById(integerEntry.getKey())); } } } public interface OnFooterViewListener { void onClicked(View view, BaseRecyclerViewAdapter adapter, Object object); } public interface OnHeaderViewViewListener { void onClicked(View view, BaseRecyclerViewAdapter adapter, Object object); } public interface OnContentViewListener { void onClicked(View view, BaseRecyclerViewAdapter adapter, Object object); } public interface OnContentGroupViewListener { void onClicked(View view, BaseRecyclerViewAdapter adapter, Object object); } public OnFooterViewListener footerViewListener; public void setOnFooterViewListener(OnFooterViewListener footerViewListener) { this.footerViewListener = footerViewListener; } public OnHeaderViewViewListener headerViewViewListener; public void setOnHeaderViewViewListener(OnHeaderViewViewListener headerViewViewListener) { this.headerViewViewListener = headerViewViewListener; } public OnContentViewListener contentViewListener; public void setOnContentViewListener (OnContentViewListener contentViewListener) { this.contentViewListener = contentViewListener; } public OnContentGroupViewListener contentGroupViewListener; public void setOnContentGroupViewListener(OnContentGroupViewListener contentGroupViewListener) { this.contentGroupViewListener = contentGroupViewListener; } public View getHeaderItemViewById(int id) { return headerViews.get(id); } public View getContentViewItemViewById(int id) { return contentViews.get(id); } public View getContentViewGroupViewById(int id) { return contentViewGroups.get(id); } public View getFooterViewById(int id) { return footViews.get(id); } @Override public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int type) { switch (type) { case ItemType.TYPE_HEADER: { return new HeaderViewHolder(LayoutInflater.from(this.context).inflate(getHeaderResourse(), viewGroup, false)); } case ItemType.TYPE_CONTENT: { View view = LayoutInflater.from(this.context).inflate(getContentResourse(), viewGroup, false); return new ContentViewHolder(LayoutInflater.from(this.context).inflate(getContentResourse(), viewGroup, false)); } case ItemType.TYPE_CONTENT_GROUP: { return new ContentViewGroupHolder(LayoutInflater.from(this.context).inflate(getContentGroupResourse(), viewGroup, false)); } case ItemType.TYPE_FOOTER: { return new FooterViewHolder(LayoutInflater.from(this.context).inflate(getFooterResourse(), viewGroup, false)); } } return null; } @Override public final void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { ItemType itemType = itemTypes.get(position); if(null != itemType.data) viewHolder.itemView.setTag(itemType.data); switch (itemType.type) { case ItemType.TYPE_HEADER: { onHeaderViewUpdate(this, viewHolder.itemView,itemType.data); }break; case ItemType.TYPE_CONTENT: { onContentViewUpdate(this,viewHolder.itemView,itemType.data); }break; case ItemType.TYPE_CONTENT_GROUP: { onContentViewGroupUpdate(this,viewHolder.itemView,itemType.data); }break; case ItemType.TYPE_FOOTER: { onFooterViewUpdate(this, viewHolder.itemView,itemType.data); }break; } } @Override public final int getItemCount() { return itemTypes.size(); } @Override public final int getItemViewType(int position) { return itemTypes.get(position).type; } public final static class ItemType { public ItemType(int type) { this.type = type; } public ItemType(int type, Object data) { this.type = type; this.data = data; } public int type; public Object data; /** * 头部 */ public static final int TYPE_HEADER = 0x01; /** * 列表内容 */ public static final int TYPE_CONTENT = 0x02; /** * 尾部 */ public static final int TYPE_FOOTER = 0x03; /** * 分组 */ public static final int TYPE_CONTENT_GROUP = 0x04; } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:万能适配器
原文地址:http://blog.csdn.net/yoyo_newbie/article/details/48130813