十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
怎么在Android 中利用RecyclerView实现一个通用适配器功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
10年积累的做网站、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有王益免费网站建设让你可以放心的选择与我们合作。
实现代码:
RViewHolder
import android.content.Context; import android.graphics.Bitmap; import android.support.v7.widget.RecyclerView.ViewHolder; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class RViewHolder extends ViewHolder { private Context mContext; private View mConvertView; private SparseArraymViews; public RViewHolder(View itemView) { super(itemView); mConvertView = itemView; this.mViews = new SparseArray (); } public static RViewHolder get(Context context, ViewGroup parent, int layoutId, int position) { View view = LayoutInflater.from(context).inflate(layoutId, parent, false); return new RViewHolder(view); } /** * 通过控件的Id获取对于的控件,如果没有则加入views * * @param viewId * @return */ public T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } /** * 为TextView设置字符�? * * @param viewId * @param text * @return */ public ViewHolder setText(int viewId, String text) { TextView view = getView(viewId); view.setText(text); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageResource(int viewId, int drawableId) { ImageView view = getView(viewId); view.setImageResource(drawableId); return this; } /** * 为ImageView设置图片 * * @param viewId * @param drawableId * @return */ public ViewHolder setImageBitmap(int viewId, Bitmap bm) { ImageView view = getView(viewId); view.setImageBitmap(bm); return this; } public View getConvertView() { return mConvertView; } }
这里是RBaseAdapter
import java.util.ArrayList; import java.util.Collection; import java.util.List; import android.content.Context; import android.support.v7.widget.RecyclerView.Adapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; public abstract class RBaseAdapterextends Adapter { private Context mContext; private List list; protected LayoutInflater mInflater; private int mItemLayoutId; public RBaseAdapter(Context context) { // TODO Auto-generated constructor stub this.mContext = context; this.mInflater = LayoutInflater.from(mContext); this.mItemLayoutId = new LinearLayout(mContext).getId(); this.list = new ArrayList (); } public RBaseAdapter(Context context, List list) { // TODO Auto-generated constructor stub this.mContext = context; this.mInflater = LayoutInflater.from(mContext); this.mItemLayoutId = new LinearLayout(mContext).getId(); this.list = list; } public RBaseAdapter(Context context, List list, int itemLayoutId) { this.mContext = context; this.mInflater = LayoutInflater.from(mContext); this.mItemLayoutId = itemLayoutId; this.list = list; } public RBaseAdapter(Context context, int itemLayoutId) { this.mContext = context; this.mInflater = LayoutInflater.from(mContext); this.mItemLayoutId = itemLayoutId; this.list = new ArrayList (); } public void setitemLayoutId(int itemLayoutId) { this.mItemLayoutId = itemLayoutId; } public List getList() { return this.list; } public void appendList(List list) { // TODO Auto-generated method stub this.list = list; notifyDataSetChanged(); } public void addList(List list2) { // TODO Auto-generated method stub this.list.addAll((Collection extends T>) list2); notifyDataSetChanged(); } @Override public int getItemCount() { return list.size(); } boolean hasHeader = false; boolean hasFooter = false; View headerView; View footerView; public void setHeaderView(View headerView) { hasHeader=true; this.headerView = headerView; } public void setFooterView(View footerView) { hasFooter = true; this.footerView = footerView; } public View getHeaderView() { return headerView; } public View getFooterView() { return footerView; } @Override public void onBindViewHolder(RViewHolder holder, int position) { if (hasHeader && position == 0) { return; } else if (hasFooter && position == (list.size() + (hasHeader ? 1 : 0))) { return; } else convert(holder, (T) list.get(position)); } @Override public RViewHolder onCreateViewHolder(ViewGroup parent, int position) { if (hasHeader && position == 0) { return new RViewHolder(headerView); } else if (hasFooter && position == (list.size() + (hasHeader ? 1 : 0))) { return new RViewHolder(footerView); } else return RViewHolder.get(mContext, parent, mItemLayoutId, position); } //这里定义抽象方法,我们在匿名内部类实现的时候实现此方法来调用控件 public abstract void convert(RViewHolder holder, T item); }
对于RBaseAdapter稍微讲解下,首先是泛型,这样任何对象类型都可以使用,再来就是前面提到的headerview和footerview的解决,可以看到RBaseAdapter里面定义了几个方法,通过position的不同来加载不同的布局的思想来添加headerview和footerview。
Activity里面调用
recyclerView.setAdapter(new RBaseAdapter(mContext, R.layout.virtual_win_users_list_item) { @Override public void convert(RViewHolder holder, VirtualWinsBean item) { if (Util.checkNULL(item.getNick_name())) { holder.setText(R.id.name, Util.HidePhone(item.getPhone() + "")); } else { holder.setText(R.id.name, item.getNick_name()); } RoundImageView networkImageView = holder.getView(R.id.photo); networkImageView.setLoadingImage(R.drawable.header_def); networkImageView.setDefultImage(R.drawable.header_def); networkImageView.LoadUrl(U.g(item.getFile_url())); } });
看完上述内容,你们掌握怎么在Android 中利用RecyclerView实现一个通用适配器功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!