您好,登錄后才能下訂單哦!
怎么在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); } /** * 通過控件的Id獲取對于的控件,如果沒有則加入views * * @param viewId * @return */ 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; } /** * 為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 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實現一個通用適配器功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。