對RecycleView的多種item布局的封裝


本文是借鑒bingoogolapple寫得BGAAdapter-Android而產生的,對此表示感謝。

效果

image

1.Adapter的使用

1.繼承BaseAdapter

這里是我的adapter

public class RecyclerChatAdapter extends BaseAdapter<ChatModel> { public Context context; protected ADUholder holder; public RecyclerChatAdapter(Context context) { super(); this.context = context; } @Override public int getItemViewType(int position) { return mDatas.get(position).type; } @Override protected BaseViewHolder addViewHolder(ViewGroup parent, int viewType) { View view = null; switch (viewType) { case Type.chat: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat, parent, false); return new ChatHolder(view); case Type.other: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false); return new ADUholder(view); default://防止空指針 view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat, parent, false); return new ChatHolder(view); } } @Override protected void fillData(RecyclerView.ViewHolder holder, int position, ChatModel model) { if (holder instanceof ChatHolder) { ChatHolder chatHolder = (ChatHolder) holder; chatHolder.fillData(context, mDatas, position); } else if (holder instanceof ADUholder) { ADUholder adUholder = (ADUholder) holder; adUholder.fillData(context, mDatas, position); } } }

注意這里要實現多item布局就要重寫getItemViewType()方法,我們可以在item中的屬性中增加一個類型,如我這里是在ChatModel中增加了一個int type;在getItemViewType()獲取它。

2.重寫addViewHolder()

根據參數viewType來加載不同的item布局,我這里僅僅寫了兩種,你可以繼續增加。

@Override protected BaseViewHolder addViewHolder(ViewGroup parent, int viewType) { View view = null; switch (viewType) { case Type.chat: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat, parent, false); return new ChatHolder(view); case Type.other: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false); return new ADUholder(view); } return null; }

3.重寫getItemViewType()

代碼如下:

@Override public int getItemViewType(int position) { return mDatas.get(position).type; }

這里根據你每個item中的type來返回,對應addViewHolder()方法中的參數viewType。

4.重寫fillData()

這里就是填充數據了

@Override protected void fillData(RecyclerView.ViewHolder holder, int position, ChatModel model) { if (holder instanceof ChatHolder) { ChatHolder chatHolder = (ChatHolder) holder; chatHolder.fillData(context, mDatas, position); } else if (holder instanceof ADUholder) { ADUholder adUholder = (ADUholder) holder; adUholder.fillData(context, mDatas, position); } }

adapter這里我們就做完了,剩下了就是ViewHolder。

2.ViewHolder的使用

1.繼承BaseViewHolder

我的代碼如下:

public class ChatHolder extends BaseViewHolder<ChatModel> { protected RelativeLayout rl_item_chat_to; protected RelativeLayout rl_item_chat_from; protected TextView tv_item_chat_from_msg; protected TextView tv_item_chat_to_msg; public ChatHolder(View view) { super(view); rl_item_chat_to = findViewById(R.id.rl_item_chat_to); rl_item_chat_from = findViewById(R.id.rl_item_chat_from); tv_item_chat_from_msg = findViewById(R.id.tv_item_chat_from_msg); tv_item_chat_to_msg = findViewById(R.id.tv_item_chat_to_msg); } @Override public void fillData(Context context, List<ChatModel> datas, int position) { ChatModel model = datas.get(position); if (model.mUserType == ChatModel.UserType.From) { rl_item_chat_to.setVisibility(View.GONE); rl_item_chat_from.setVisibility(View.VISIBLE); String msg = String.format(mContent.getString(R.string.color_msg_from), model.mMsg); Spanned htmlMsg = Html.fromHtml(msg); tv_item_chat_from_msg.setText(htmlMsg,TextView.BufferType.SPANNABLE); } else { rl_item_chat_to.setVisibility(View.VISIBLE); rl_item_chat_from.setVisibility(View.GONE); String msg = String.format(mContent.getString(R.string.color_msg_from), model.mMsg); Spanned htmlMsg = Html.fromHtml(msg); tv_item_chat_to_msg.setText(htmlMsg,TextView.BufferType.SPANNABLE); } } }

2.構造方法

你要實現一個帶有View參數的構造方法,我們可以在此做控件綁定。

3.重寫fillData()

這里是你真正控件填充數據的地方,對每個item項。

4.另一個ViewHolder

public class ADUholder extends BaseViewHolder<ChatModel> { public ImageView imageView; public TextView title; public TextView name; public ADUholder(View itemView) { super(itemView); imageView = findViewById(R.id.imageView); title = findViewById(R.id.tv_title); name = findViewById(R.id.tv_name); } @Override public void fillData(Context context, List<ChatModel> datas, int position) { ChatModel model = datas.get(position); title.setText(model.name + position); name.setText(model.mMsg); } }

是不是感覺簡單了很多,當然這是我自己的看法。

好了,在此附上github源碼,喜歡的請start、fork。 https://github.com/DyncKathline/TestRecyclerView

轉載請注明出處,謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM