2016-10-03 104 views
1

我正在android中使用xmpp.My實現聊天應用聊天工作正常,但是當我爲發件人和接收者在聊天中設置佈局時,inflater正在更改佈局所有現有的列表項目。 下面是截圖,當用戶發送「你好」 enter image description hereRecycler視圖改變所有列表項目的佈局不適用於單個列表項目

但是,當接收器發送「你好」,那麼這聊天消息給正在添加左側位置,但在發送郵件時它也採取其他消息向左現在的位置和相同.. 下面是截圖,當用戶在發送時,「你好嗎」 enter image description here

我不其中ia接受「你好」

enter image description here

,隨後由該錯了

我回收視圖發件人佈局

chat_layout_self.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:layout_gravity="right|bottom" 
    android:gravity="right|bottom" 
    > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/chatsymbolself"> 

     <TextView 
      android:id="@+id/cltv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="hi" 
      android:textSize="20sp" 
      android:textColor="#fff" 
      android:padding="10dp" 
      android:layout_gravity="left|bottom"/> 

    </LinearLayout> 


</LinearLayout> 

chat_layout_other.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:layout_gravity="left|bottom" 
    android:gravity="left|bottom" 
    > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/chatsymbolother"> 

     <TextView 
      android:id="@+id/cltv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="hi" 
      android:textSize="20sp" 
      android:textColor="#fff" 
      android:padding="10dp" 
      android:layout_gravity="left"/> 

    </LinearLayout> 


</LinearLayout> 

MessageAdapter.java

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MyViewHolder> { 

    private String user="[email protected]/Android"; 
    private LayoutInflater inflater; 
    List<Message> data= Collections.emptyList(); 
    public MessageAdapter(Context context,List<Message> data){ 
     inflater=LayoutInflater.from(context); 
     this.data=data; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     Message message = data.get(position); 
     Log.e("MAdapter", "getItemViewType: from "+message.from); 
     if (message.from.equals(user)){ 
      return 0; 
     } 
     else { 
      return 1; 
     } 
    } 

    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View view; 
     MyViewHolder holder; 
     if(viewType==0){ 
      Log.e("MAdapter", "onCreateViewHolder: SEFL"); 
      view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_layout_self,parent,false); 

     } 
     else{ 
      Log.e("MAdapter", "onCreateViewHolder: OTHER"); 
      view=LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_layout_other,parent,false); 
     } 
     return new MyViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 

     final int itemType = getItemViewType(position); 

     Message current=data.get(position); 
     holder.chat.setText(current.msg); 
    } 

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

    class MyViewHolder extends RecyclerView.ViewHolder{ 

     TextView chat; 

     public MyViewHolder(View itemView) { 
      super(itemView); 
      chat=(TextView)itemView.findViewById(R.id.cltv); 
     } 
    } 
} 
+0

你可以發佈日誌嗎?特別是Log.e(「MAdapter」,「getItemViewType:from」+ message.from);'應該提供一些洞察力 –

+0

你也可以在你處理一個新的'Message'的地方發佈代碼嗎?即如何將新對象添加到「列表數據」以及如何向MessageAdapter通知更改。 –

+0

我認爲沒有什麼與日誌...這些只是在logcat顯示用戶名.. @Mr。凱文托馬斯 –

回答

0

我認爲,它BEC使用具有圓形佈局大小的佈局。您應該將TextView寬度更改爲wrap_content而不是match_parent

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/cltv" 
     android:background="@drawable/chatsymbolother" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="hi" 
     android:textSize="20sp" 
     android:textColor="#fff" 
     android:padding="10dp" 
     android:layout_gravity="left"/> 
</LinearLayout> 
  1. 讓你viewholder靜態
  2. 你並不需要創建XML深佈局,如果簡單的TextView就足夠了。
+0

謝謝你,但那也行不通 –