From a8a4d49d627cd88d1e09821ae67900d9d9098e16 Mon Sep 17 00:00:00 2001 From: free46000 Date: Wed, 22 Mar 2017 00:16:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(0.9.1):=20=E5=A2=9E=E5=8A=A0=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E7=95=8C=E9=9D=A2demo=EF=BC=8C=E4=BF=AE=E5=A4=8DBaseI?= =?UTF-8?q?temAdapter=E4=B8=AD=E8=8E=B7=E5=8F=96type=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../freelib/multiitem/demo/ChatActivity.java | 32 ++++++++++++------- .../freelib/multiitem/demo/MainActivity.java | 8 +++-- .../multiitem/demo/bean/MessageBean.java | 5 +++ .../viewholder/ReceiveMessageManager.java | 28 ++++++++++++++++ ...ewManager.java => SendMessageManager.java} | 2 +- .../src/main/res/layout/item_chat_receive.xml | 2 +- demo/src/main/res/layout/item_chat_send.xml | 2 +- demo/src/main/res/layout/layout_recycler.xml | 1 + demo/src/main/res/values/strings.xml | 1 + .../multiitem/adapter/BaseItemAdapter.java | 4 +-- .../adapter/type/ItemTypeManager.java | 1 + 11 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 demo/src/main/java/com/freelib/multiitem/demo/viewholder/ReceiveMessageManager.java rename demo/src/main/java/com/freelib/multiitem/demo/viewholder/{MessageViewManager.java => SendMessageManager.java} (91%) diff --git a/demo/src/main/java/com/freelib/multiitem/demo/ChatActivity.java b/demo/src/main/java/com/freelib/multiitem/demo/ChatActivity.java index 62ee567..9ea1acc 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/ChatActivity.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/ChatActivity.java @@ -6,14 +6,13 @@ import android.support.v7.widget.RecyclerView; import com.freelib.multiitem.adapter.BaseItemAdapter; +import com.freelib.multiitem.adapter.holder.ViewHolderManagerGroup; import com.freelib.multiitem.demo.bean.ImageBean; import com.freelib.multiitem.demo.bean.ImageTextBean; import com.freelib.multiitem.demo.bean.MessageBean; import com.freelib.multiitem.demo.bean.TextBean; -import com.freelib.multiitem.demo.viewholder.ImageAndTextManager; -import com.freelib.multiitem.demo.viewholder.ImageViewManager; -import com.freelib.multiitem.demo.viewholder.MessageViewManager; -import com.freelib.multiitem.demo.viewholder.TextViewManager; +import com.freelib.multiitem.demo.viewholder.ReceiveMessageManager; +import com.freelib.multiitem.demo.viewholder.SendMessageManager; import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.EActivity; @@ -22,8 +21,12 @@ import java.util.ArrayList; import java.util.List; +import static android.R.id.list; + @EActivity(R.layout.layout_recycler) public class ChatActivity extends AppCompatActivity { + public static final String uid = "1"; + public static final String other = "2"; @ViewById(R.id.recyclerView) protected RecyclerView recyclerView; @@ -33,20 +36,25 @@ public static void startChatActivity(Context context) { @AfterViews protected void initViews() { - setTitle(R.string.multi_item_title); + setTitle(R.string.chat_title); recyclerView.setLayoutManager(new LinearLayoutManager(this)); //初始化adapter BaseItemAdapter adapter = new BaseItemAdapter(); - //为XXBean数据源注册XXManager管理类 - adapter.register(MessageBean.class, new MessageViewManager()); + //为XXBean数据源注册XXManager管理类组合 + adapter.register(MessageBean.class, new ViewHolderManagerGroup(new SendMessageManager(), new ReceiveMessageManager()) { + @Override + public int getViewHolderManagerIndex(MessageBean itemData) { + //根据message判断是否本人发送并返回对应ViewHolderManager的index值 + return itemData.getSender().equals(uid) ? 0 : 1; + } + }); recyclerView.setAdapter(adapter); List list = new ArrayList<>(); - for (int i = 0; i < 20; i++) { - list.add(new TextBean("AAA" + i)); - list.add(new ImageBean(R.drawable.img1)); - list.add(new ImageTextBean(R.drawable.img2, "BBB" + i)); - } + list.add(new MessageBean("在吗?", other)); + list.add(new MessageBean("在啊啊啊啊啊啊啊!", uid)); + list.add(new MessageBean("目前展示的是聊天界面中一个消息对应两种布局的情况,看看效果如何?", other)); + list.add(new MessageBean("不错!", uid)); adapter.setDataItems(list); } diff --git a/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java b/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java index cc4a204..681a2ff 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/MainActivity.java @@ -29,8 +29,9 @@ protected void initViews() { //为TextBean数据源注册TextViewManager管理类 adapter.register(TextBean.class, new TextViewManager()); recyclerView.setAdapter(adapter); - adapter.addDataItem(new TextBean(getString(R.string.multi_item_title))); adapter.addDataItem(new TextBean(getString(R.string.item_click_title))); + adapter.addDataItem(new TextBean(getString(R.string.multi_item_title))); + adapter.addDataItem(new TextBean(getString(R.string.chat_title))); adapter.setOnItemClickListener(new OnItemClickListener() { @Override @@ -38,12 +39,13 @@ public void onItemClick(BaseViewHolder viewHolder) { //通过viewHolder获取需要的数据 switch (viewHolder.getAdapterPosition()) { case 0: - MultiItemActivity.startMultiItemActivity(MainActivity.this); + ItemClickActivity.startItemClickActivity(MainActivity.this); break; case 1: - ItemClickActivity.startItemClickActivity(MainActivity.this); + MultiItemActivity.startMultiItemActivity(MainActivity.this); break; default: + ChatActivity.startChatActivity(MainActivity.this); break; } diff --git a/demo/src/main/java/com/freelib/multiitem/demo/bean/MessageBean.java b/demo/src/main/java/com/freelib/multiitem/demo/bean/MessageBean.java index 0aa8910..552ef16 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/bean/MessageBean.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/bean/MessageBean.java @@ -8,6 +8,11 @@ public class MessageBean { private String message; private String sender; + public MessageBean(String message, String sender) { + this.message = message; + this.sender = sender; + } + public String getMessage() { return message; } diff --git a/demo/src/main/java/com/freelib/multiitem/demo/viewholder/ReceiveMessageManager.java b/demo/src/main/java/com/freelib/multiitem/demo/viewholder/ReceiveMessageManager.java new file mode 100644 index 0000000..28046d0 --- /dev/null +++ b/demo/src/main/java/com/freelib/multiitem/demo/viewholder/ReceiveMessageManager.java @@ -0,0 +1,28 @@ +package com.freelib.multiitem.demo.viewholder; + +import android.support.annotation.NonNull; +import android.widget.TextView; + +import com.freelib.multiitem.adapter.holder.BaseViewHolder; +import com.freelib.multiitem.adapter.holder.BaseViewHolderManager; +import com.freelib.multiitem.demo.R; +import com.freelib.multiitem.demo.bean.MessageBean; + +/** + * @author free46000 2017/03/17 + */ +public class ReceiveMessageManager extends BaseViewHolderManager { + + + @Override + public void onBindViewHolder(@NonNull BaseViewHolder holder, @NonNull MessageBean data) { + TextView textView = getView(holder, R.id.text); + textView.setText(data.getMessage()); + } + + @Override + protected int getItemLayoutId() { + return R.layout.item_chat_receive; + } + +} diff --git a/demo/src/main/java/com/freelib/multiitem/demo/viewholder/MessageViewManager.java b/demo/src/main/java/com/freelib/multiitem/demo/viewholder/SendMessageManager.java similarity index 91% rename from demo/src/main/java/com/freelib/multiitem/demo/viewholder/MessageViewManager.java rename to demo/src/main/java/com/freelib/multiitem/demo/viewholder/SendMessageManager.java index 363bc40..ffddc7b 100644 --- a/demo/src/main/java/com/freelib/multiitem/demo/viewholder/MessageViewManager.java +++ b/demo/src/main/java/com/freelib/multiitem/demo/viewholder/SendMessageManager.java @@ -11,7 +11,7 @@ /** * @author free46000 2017/03/17 */ -public class MessageViewManager extends BaseViewHolderManager { +public class SendMessageManager extends BaseViewHolderManager { @Override diff --git a/demo/src/main/res/layout/item_chat_receive.xml b/demo/src/main/res/layout/item_chat_receive.xml index 37a7585..ba85b0d 100644 --- a/demo/src/main/res/layout/item_chat_receive.xml +++ b/demo/src/main/res/layout/item_chat_receive.xml @@ -3,7 +3,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_margin="5dp"> + android:layout_marginRight="60dp"> + android:layout_marginLeft="60dp"> MultiItem演示 多类型演示 点击事件演示 + 聊天界面演示 diff --git a/library/src/main/java/com/freelib/multiitem/adapter/BaseItemAdapter.java b/library/src/main/java/com/freelib/multiitem/adapter/BaseItemAdapter.java index 43bc9db..30dc348 100644 --- a/library/src/main/java/com/freelib/multiitem/adapter/BaseItemAdapter.java +++ b/library/src/main/java/com/freelib/multiitem/adapter/BaseItemAdapter.java @@ -63,7 +63,7 @@ public void register(@NonNull Class cls, @NonNu * @param ViewHolder * @see #register(Class, ViewHolderManager) 为数据源注册ViewHolder的管理类 */ - public void register(@NonNull Class cls, @NonNull ViewHolderManagerGroup group) { + public void register(@NonNull Class cls, @NonNull ViewHolderManagerGroup group) { itemTypeManager.register(cls, group); } @@ -186,7 +186,7 @@ public void onBindViewHolder(BaseViewHolder holder, int position) { @Override public int getItemViewType(int position) { - int type = itemTypeManager.getItemType(getItem(position).getClass()); + int type = itemTypeManager.getItemType(getItem(position)); if (type < 0) throw new RuntimeException("没有为" + getItem(position).getClass() + "找到对应的item view provider,是否注册了?"); return type; diff --git a/library/src/main/java/com/freelib/multiitem/adapter/type/ItemTypeManager.java b/library/src/main/java/com/freelib/multiitem/adapter/type/ItemTypeManager.java index 0660517..dfd1f13 100644 --- a/library/src/main/java/com/freelib/multiitem/adapter/type/ItemTypeManager.java +++ b/library/src/main/java/com/freelib/multiitem/adapter/type/ItemTypeManager.java @@ -65,6 +65,7 @@ private void register(String className, ViewHolderManager manager) { */ public int getItemType(@NonNull Object itemData) { String key = getClassName(itemData.getClass()); + //如果含有证明此className注册了组合的对应关系,需要取出实际的className if (itemClassNameGroupMap.containsKey(key)) { ViewHolderManager manager = itemClassNameGroupMap.get(key).getViewHolderManager(itemData); key = getClassNameFromGroup(itemData.getClass(), itemClassNameGroupMap.get(key), manager);