具有多个视图的firebase Recycler适配器

时间:2017-01-26 22:54:51

标签: android firebase adapter

我正在尝试为聊天应用创建自己的自定义firebase回收器适配器。我想在适配器中膨胀两个视图,一个用于发送者,一个用于接收者,但不知道如何。

我该如何处理?

2 个答案:

答案 0 :(得分:0)

要在RecyclerView中膨胀不同的视图,您需要在适配器中实现此功能:

public int getItemViewType(int position);

此功能在onCreateViewHolder之前调用,并允许您根据要膨胀的视图类型返回int。你可以想象使用enum来实现这个目标:

public enum TYPE {
    SENDER,
    RECEIVER
}

这样您将根据类型返回一个int(使用type.ordinal())。

然后,您将获得onCreateViewHolder(ViewGroup parent, int viewType)功能的类型,您将能够匹配您再次键入TYPE.values()[viewType]。这样,您将根据类型返回膨胀布局。

最后在onBindViewHolder(TimelineItemViewHolder holder,int position)上,您将能够:

  • 致电getItemViewType(int position)了解如何投射和展示您的内容
  • 使用instanceof能够检测您正在查看的ViewHolder的类型
  • 使用抽象类,扩展两个ViewHolder共享的RecyclerView.ViewHolder,允许您调用常用函数,例如display(MyObject object)

答案 1 :(得分:-1)

private static final int RIGHT_MSG = 0;
private static final int LEFT_MSG = 1;
/*Viewholder for messages*/
public static class MessageViewHolder extends RecyclerView.ViewHolder {
public EmojiconTextView messageTextView;
public TextView messengerTextView;
public CircleImageView messengerImageView;
public ImageView sendimage;
public ImageView videoview;
public TextView messageTime;
public MessageViewHolder(View v) {
super(v);
messageTextView = (EmojiconTextView) itemView.findViewById(R.id.messageTextView);
messengerTextView = (TextView) itemView.findViewById(R.id.messengerTextView);
messengerImageView = (CircleImageView) itemView.findViewById(R.id.messengerImageView);
sendimage = (ImageView) itemView.findViewById(R.id.image);
videoview = (ImageView) itemView.findViewById(R.id.videoview);
messageTime = (TextView)itemView.findViewById(R.id.message_time);
}
/*Firebase recycler Adapter*/
mFirebaseAdapter = new FirebaseRecyclerAdapter < FriendlyMessage, MessageViewHolder > (
FriendlyMessage.class, R.layout.item_message, MessageViewHolder.class,
mFirebaseDatabaseReference.child(MESSAGES_CHILD)) {
@Override
protected void populateViewHolder(final MessageViewHolder viewHolder,     final FriendlyMessage friendlyMessage, int position) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
viewHolder.messageTextView.setText(friendlyMessage.getText());
viewHolder.messengerTextView.setText(friendlyMessage.getName());
viewHolder.messageTime.setText(DateFormat.format("MMM d, yyyy hh:mm a", friendlyMessage.getMessageTime()));
if (friendlyMessage.getPhotoUrl() == null) {
viewHolder.messengerImageView.setImageDrawable(ContextCompat.getDrawable(Chat.this,
R.drawable.ic_account_circle_black_36dp));
} else {
Glide.with(Chat.this).load(friendlyMessage.getPhotoUrl()).into(viewHolder.messengerImageView);
}
//for show send image
if (friendlyMessage.getImageUrl() == null) {
viewHolder.sendimage.setVisibility(View.GONE);
} else {
viewHolder.sendimage.setVisibility(View.VISIBLE);
Glide.with(Chat.this).load(friendlyMessage.getImageUrl()).into(viewHolder.sendimage);
}
//to show the videoattach in videoview
if (friendlyMessage.getVideoUrl() == null) {
Glide.with(Chat.this).load("").placeholder(R.drawable.videoiconcrop).into(viewHolder.videoview);
//viewHolder.videoview.setVisibility(View.GONE);
} else {
viewHolder.videoview.setVisibility(View.VISIBLE);
}
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == RIGHT_MSG) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_viewholder2, parent, false);
return new MessageViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_viewholder1, parent, false);
return new MessageViewHolder(view);
}
}
@Override
public int getItemViewType(int position) {
FriendlyMessage model = getItem(position);
if (model.getContactNumber().equals(UserDetails.contact)) {
return RIGHT_MSG;
} else {
return LEFT_MSG;
}
}
};
 /*Xml for messages i try to provide you one side try to make another side by using id same as this layout*/
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:emojicon="http://schemas.android.com/apk/res-auto">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:id="@+id/messengerImageView"
android:src="@drawable/ic_account_circle_black_36dp"/>
<FrameLayout
android:id="@+id/incoming_layout_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="60dp"
android:layout_alignParentLeft="true"
android:background="@drawable/balloon_incoming_normal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold|italic"
android:text="Receiver"
android:textSize="16dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="10dp"
android:paddingBottom="12dp"
android:layout_marginLeft="6dp"
android:layout_above="@+id/messageTextView"
android:id="@+id/messengerTextView"
android:layout_weight="0"/>
<hani.momanii.supernova_emoji_library.Helper.EmojiconTextView
android:id="@+id/messageTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:layout_marginTop="18dp"
android:layout_marginBottom="10dp"
android:autoLink="web"
android:text="Rahul Agrawal is a good boy but he does not know what he wants."
style="@style/chat_text_message_style"
emojicon:emojiconSize="30sp"/>
<TextView
android:id="@+id/message_time"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingRight="5dp"
android:text="11:15 PM"
android:layout_marginLeft="6dp"
style="@style/chat_timings"
android:layout_gravity="bottom|right"/>
<ImageView
android:id="@+id/image"
android:layout_width="150dp"
android:layout_height="160dp"
android:src="@drawable/ic_account_circle_black_36dp"
android:contentDescription="send_photo"
android:visibility="gone"
android:layout_marginTop="23dp"
android:layout_marginBottom="18dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_gravity="center"
android:scaleType="fitXY"/>
<ImageView
android:layout_width="150dp"
android:layout_height="120dp"
android:id="@+id/videoview"
android:contentDescription="send_video"
android:src="@drawable/videoiconcrop"
android:layout_marginTop="20dp"
android:layout_marginBottom="18dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_gravity="center"
android:visibility="gone"
/>
</FrameLayout>
</LinearLayout>

可能是此代码可以帮助您解决Happy Coding