-1

在我的Android火力聊天应用程序,我想用recyclerview根据传入或传出消息的类型即recyclerview两个viewtypes不chaning viewtype

让我先告诉你的火力点数据结构显示chatlist

<ConversationId> 
    <MsgId> 
     text :"Hello" 
     timestamp :<timestamp> 
     sender :<sender's uid> 
     receiver :<receiver's uid> 

以下是我的适配器类

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 RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

      View view; 
      switch (viewType) { 
       case 0: 
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_incoming_bubble, parent, false); 
        return new incoming(view); 
       case 1: 
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_outgoing_bubble, parent, false); 
        return new outgoing(view); 

      } 
      return null; 
     } 
     @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 void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) { 

      SingleChatList chat=chatList.get(listPosition); 

      if (chat != null) { 
       int swi=0; 
       if(chat.getSender()== FirebaseAuth.getCurrentUser().getUid()) 
       { 
        swi=1; 
       } 
       else 
       { 
        swi=0; 
       } 
       switch (swi) { 
        case 0: 
         ((incoming) holder).message.setText(chat.getMsg()); 
         ((incoming) holder).time.setText(String.valueOf(chat.getTimestamp())); 

         break; 
        case 1: 
         ((outgoing) holder).message.setText(chat.getMsg()); 
         ((incoming) holder).time.setText(String.valueOf(chat.getTimestamp())); 
         break; 

       } 
      } 
     } 


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

在这里,我使用下面的逻辑

0决定viewtype
senderuid== FirebaseAuth.getCurrentUser().getUid(); 

如果上述条件为真,那么它应该是一个传出消息。

我运行上面的代码,但所有的消息只能进入味精布局应用

+0

onBindViewHolder函数使用holder.getItemViewType()获取视图类型 – Nidhin

回答

0

在你的代码中,你正在为每个case使用return。将一个值赋给一个局部变量,然后在最后返回它被认为是一个好习惯。具有多个出口的方法更难以调试,并且可能难以阅读。您最后还是以视图持有者的形式回应空。尝试更新的代码并检查此作品。

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(); 
} 
} 
0

你需要下面的代码使用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命名约定的朋友:)