怎么在Android 中利用RecyclerView实现一个通用适配器功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
实现代码:
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 SparseArray<View> mViews; public RViewHolder(View itemView) { super(itemView); mConvertView = itemView; this.mViews = new SparseArray<View>(); } 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); } public <T extends View> T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mConvertView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } public ViewHolder setText(int viewId, String text) { TextView view = getView(viewId); view.setText(text); return this; } public ViewHolder setImageResource(int viewId, int drawableId) { ImageView view = getView(viewId); view.setImageResource(drawableId); return this; } 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 RBaseAdapter<T> extends Adapter<RViewHolder> { private Context mContext; private List<T> 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<T>(); } public RBaseAdapter(Context context, List<T> 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<T> 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<T>(); } public void setitemLayoutId(int itemLayoutId) { this.mItemLayoutId = itemLayoutId; } public List<T> getList() { return this.list; } public void appendList(List<T> list) { // TODO Auto-generated method stub this.list = list; notifyDataSetChanged(); } public void addList(List<T> 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<VirtualWinsBean>(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实现一个通用适配器功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网行业资讯频道,感谢各位的阅读!