-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命名約定的朋友:)