Android 具有两种视图类型的recyclerview不更改视图类型

Android 具有两种视图类型的recyclerview不更改视图类型,android,firebase,android-recyclerview,firebase-authentication,Android,Firebase,Android Recyclerview,Firebase Authentication,在我的android firebase聊天应用程序中,我想使用recyclerview根据消息类型(即传入或传出)显示聊天列表 首先让我告诉你firebase的数据结构 <ConversationId> <MsgId> text :"Hello" timestamp :<timestamp> sender :<sender's uid> receiver :<receiver's

在我的android firebase聊天应用程序中,我想使用recyclerview根据消息类型(即传入或传出)显示聊天列表

首先让我告诉你firebase的数据结构

<ConversationId>
    <MsgId>
       text :"Hello"
       timestamp :<timestamp>
       sender :<sender's uid>
       receiver :<receiver's uid>
如果上述条件为真,则应为传出消息


我运行了上面的代码,但对所有消息没有任何更改,只有传入的消息布局正在应用

您需要使用
viewHolder.getItemViewType()
内部
onBindViewHolder

试试下面的代码

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

   SingleChatList chat=chatList.get(listPosition);
   switch (holder.getItemViewType()) {
      case 0:
          incoming incommindHolder= (incoming ) holder;
          //TODO Bind data view Holder
          incommindHolder.message.setText(chat.getMsg());
          incommindHolder.time.setText(String.valueOf(chat.getTimestamp()));
          break;

      case 1:
          outgoing outGoindHolder= (outgoing) holder;
          //TODO Bind data view Holder
          outGoindHolder.message.setText(chat.getMsg());
          outGoindHolder.time.setText(String.valueOf(chat.getTimestamp()));
          break;
   }
}

注意:请了解java命名约定mate:)

在您的代码中,您对每种情况都使用return。将一个值赋给局部变量,然后在最后返回该值被认为是一种良好的做法。具有多个出口的方法更难调试,也可能更难读取。此外,您在最后将null作为viewholder返回。请尝试此更新的代码并检查是否有效

public class MultiViewTypeAdapter extends RecyclerView.Adapter {

private List<SingleChatList> chatList;
int total_types;

public class incoming extends RecyclerView.ViewHolder {
    public TextView message, time;

    //public CircleImageView dp;
    public incoming(View view) {
        super(view);
        message = (TextView) view.findViewById(R.id.message_text);
        time = (TextView) view.findViewById(R.id.timestamp_text);
    }
}

public class outgoing extends RecyclerView.ViewHolder {
    public TextView message, time;
    //public CircleImageView dp;

    public outgoing(View view) {
        super(view);
        message = (TextView) view.findViewById(R.id.message_text_view);
        time = (TextView) view.findViewById(R.id.timestamp_text_view);
    }
}

public MultiViewTypeAdapter(List<SingleChatList> data) {
    this.chatList = data;
    total_types = chatList.size();
}


@Override
public int getItemViewType(int position) {
    int swi = 0;
    if (chatList.get(position).getSender() == FirebaseAuth.getCurrentUser().getUid()) {
        swi = 1;
    } else {
        swi = 0;
    }
    return swi;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder holder = null;
    View view;
    switch (viewType) {
        case 0:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_incoming_bubble, parent, false);
            holder = new incoming(view);
            break;
        case 1:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_outgoing_bubble, parent, false);
            holder = new outgoing(view);
            break;
    }
    return holder;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

    SingleChatList chat = chatList.get(listPosition);
    switch (holder.getItemViewType()) {
        case 0:
            if (chat != null) {
                ((incoming) holder).message.setText(chat.getMsg());
                ((incoming) holder).time.setText(String.valueOf(chat.getTimestamp()));
            }
            break;
        case 1:
            if (chat != null) {
                ((outgoing) holder).message.setText(chat.getMsg());
                ((incoming) holder).time.setText(String.valueOf(chat.getTimestamp()));
            }
            break;
    }
}

@Override
public int getItemCount() {
    return chatList.size();
}
}
公共类MultiViewTypeAdapter扩展了RecyclerView.Adapter{ 私人列表聊天列表; int-total_类型; 公共类传入扩展RecyclerView.ViewHolder{ 公共文本查看消息、时间; //公众圈图像dp; 公共传入(视图){ 超级(视图); message=(TextView)view.findViewById(R.id.message\u text); time=(TextView)view.findViewById(R.id.timestamp\u text); } } 公共类传出扩展RecyclerView.ViewHolder{ 公共文本查看消息、时间; //公众圈图像dp; 公共传出(视图){ 超级(视图); message=(TextView)view.findViewById(R.id.message\u text\u view); time=(TextView)view.findViewById(R.id.timestamp\u text\u view); } } 公共多视图类型适配器(列表数据){ this.chatList=数据; total_types=chatList.size(); } @凌驾 public int getItemViewType(int位置){ int-swi=0; if(chatList.get(position.getSender()==FirebaseAuth.getCurrentUser().getUid()){ swi=1; }否则{ swi=0; } 返回swi; } @凌驾 public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){ RecyclerView.ViewHolder=null; 视图; 开关(视图类型){ 案例0: view=LayoutInflater.from(parent.getContext()).flate(R.layout.msg\u incoming\u bubble,parent,false); 支架=新引入(视图); 打破 案例1: view=LayoutInflater.from(parent.getContext()).flate(R.layout.msg\u outgoing\u bubble,parent,false); 持有者=新的输出(视图); 打破 } 报税表持有人; } @凌驾 public void onBindViewHolder(final RecyclerView.ViewHolder holder,final int listPosition){ SingleChatList chat=chatList.get(listPosition); 开关(holder.getItemViewType()){ 案例0: 如果(聊天!=null){ ((传入)holder.message.setText(chat.getMsg()); ((传入)holder.time.setText(String.valueOf(chat.getTimestamp()); } 打破 案例1: 如果(聊天!=null){ ((传出)holder.message.setText(chat.getMsg()); ((传入)holder.time.setText(String.valueOf(chat.getTimestamp()); } 打破 } } @凌驾 public int getItemCount(){ 返回chatList.size(); } }
onBindViewHolder函数使用holder.getItemViewType()获取viewtype
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

   SingleChatList chat=chatList.get(listPosition);
   switch (holder.getItemViewType()) {
      case 0:
          incoming incommindHolder= (incoming ) holder;
          //TODO Bind data view Holder
          incommindHolder.message.setText(chat.getMsg());
          incommindHolder.time.setText(String.valueOf(chat.getTimestamp()));
          break;

      case 1:
          outgoing outGoindHolder= (outgoing) holder;
          //TODO Bind data view Holder
          outGoindHolder.message.setText(chat.getMsg());
          outGoindHolder.time.setText(String.valueOf(chat.getTimestamp()));
          break;
   }
}
public class MultiViewTypeAdapter extends RecyclerView.Adapter {

private List<SingleChatList> chatList;
int total_types;

public class incoming extends RecyclerView.ViewHolder {
    public TextView message, time;

    //public CircleImageView dp;
    public incoming(View view) {
        super(view);
        message = (TextView) view.findViewById(R.id.message_text);
        time = (TextView) view.findViewById(R.id.timestamp_text);
    }
}

public class outgoing extends RecyclerView.ViewHolder {
    public TextView message, time;
    //public CircleImageView dp;

    public outgoing(View view) {
        super(view);
        message = (TextView) view.findViewById(R.id.message_text_view);
        time = (TextView) view.findViewById(R.id.timestamp_text_view);
    }
}

public MultiViewTypeAdapter(List<SingleChatList> data) {
    this.chatList = data;
    total_types = chatList.size();
}


@Override
public int getItemViewType(int position) {
    int swi = 0;
    if (chatList.get(position).getSender() == FirebaseAuth.getCurrentUser().getUid()) {
        swi = 1;
    } else {
        swi = 0;
    }
    return swi;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder holder = null;
    View view;
    switch (viewType) {
        case 0:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_incoming_bubble, parent, false);
            holder = new incoming(view);
            break;
        case 1:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_outgoing_bubble, parent, false);
            holder = new outgoing(view);
            break;
    }
    return holder;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

    SingleChatList chat = chatList.get(listPosition);
    switch (holder.getItemViewType()) {
        case 0:
            if (chat != null) {
                ((incoming) holder).message.setText(chat.getMsg());
                ((incoming) holder).time.setText(String.valueOf(chat.getTimestamp()));
            }
            break;
        case 1:
            if (chat != null) {
                ((outgoing) holder).message.setText(chat.getMsg());
                ((incoming) holder).time.setText(String.valueOf(chat.getTimestamp()));
            }
            break;
    }
}

@Override
public int getItemCount() {
    return chatList.size();
}
}