Skip to content

Commit

Permalink
feat(0.9.1): 增加聊天界面demo,修复BaseItemAdapter中获取type参数问题
Browse files Browse the repository at this point in the history
  • Loading branch information
free46000 committed Mar 21, 2017
1 parent d255a51 commit a8a4d49
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 20 deletions.
32 changes: 20 additions & 12 deletions demo/src/main/java/com/freelib/multiitem/demo/ChatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<MessageBean>(new SendMessageManager(), new ReceiveMessageManager()) {
@Override
public int getViewHolderManagerIndex(MessageBean itemData) {
//根据message判断是否本人发送并返回对应ViewHolderManager的index值
return itemData.getSender().equals(uid) ? 0 : 1;
}
});
recyclerView.setAdapter(adapter);
List<Object> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ 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
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MessageBean> {


@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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* @author free46000 2017/03/17
*/
public class MessageViewManager extends BaseViewHolderManager<MessageBean> {
public class SendMessageManager extends BaseViewHolderManager<MessageBean> {


@Override
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/res/layout/item_chat_receive.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<TextView
android:id="@+id/text"
Expand Down
2 changes: 1 addition & 1 deletion demo/src/main/res/layout/item_chat_send.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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_marginLeft="60dp">

<TextView
android:id="@+id/text"
Expand Down
1 change: 1 addition & 0 deletions demo/src/main/res/layout/layout_recycler.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#eeeeee"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
Expand Down
1 change: 1 addition & 0 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<string name="main_title">MultiItem演示</string>
<string name="multi_item_title">多类型演示</string>
<string name="item_click_title">点击事件演示</string>
<string name="chat_title">聊天界面演示</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public <T, V extends BaseViewHolder> void register(@NonNull Class<T> cls, @NonNu
* @param <V> ViewHolder
* @see #register(Class, ViewHolderManager) 为数据源注册ViewHolder的管理类
*/
public <T, V extends BaseViewHolder> void register(@NonNull Class<T> cls, @NonNull ViewHolderManagerGroup group) {
public <T, V extends BaseViewHolder> void register(@NonNull Class<T> cls, @NonNull ViewHolderManagerGroup<T> group) {
itemTypeManager.register(cls, group);
}

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a8a4d49

Please sign in to comment.