2016-08-01 97 views
0

enter image description here我在寫一個簡單的聊天界面。我正在使用九個補丁文件來完成這項工作。Android recyclerView項目佈局參數不起作用

當我第一次使用一些模擬數據初始化適配器時,它工作正常,但如果我在運行時添加了某些內容,recyclerView內部的佈局會中斷。

它不會拋出任何錯誤,雖然我用來設置對齊的佈局參數似乎不起作用。實際上,對齊工作,但它的大小變成match_parent而不是wrap_content。

這是我的適配器代碼。

public class Adapter_Chat extends RecyclerView.Adapter<Adapter_Chat.Adapter_Chat_ViewHolder> { 

private List<ChatMessageModel> data = Collections.EMPTY_LIST; 

public Adapter_Chat(List<ChatMessageModel> data) { 
    this.data = data; 
} 

@Override 
public Adapter_Chat_ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    return new Adapter_Chat_ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_chat_message, parent, false)); 
} 


public void addNewMessage(ChatMessageModel model) { 
    data.add(model); 
    notifyDataSetChanged(); 
} 

@Override 
public void onBindViewHolder(Adapter_Chat_ViewHolder holder, int position) { 
    ChatMessageModel item = data.get(position); 

    switch (item.getType()) { 
     case mine: { 
      holder.iv_chat_profileImage.setVisibility(View.INVISIBLE); 
      holder.tv_item_chat_message_nameText.setVisibility(View.INVISIBLE); 
      holder.tv_item_chat_message_contentText.setBackgroundResource(R.drawable.in_message_bg); 
      RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.tv_item_chat_message_contentText.getLayoutParams(); 
      params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
      params.height = RelativeLayout.LayoutParams.WRAP_CONTENT; 
      params.width = RelativeLayout.LayoutParams.WRAP_CONTENT; 
      holder.tv_item_chat_message_contentText.setLayoutParams(params); 

     } 
     break; 

     case others: { 
      holder.iv_chat_profileImage.setVisibility(View.VISIBLE); 
      holder.tv_item_chat_message_nameText.setVisibility(View.VISIBLE); 
      holder.tv_item_chat_message_contentText.setBackgroundResource(R.drawable.out_message_bg); 
      RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.tv_item_chat_message_contentText.getLayoutParams(); 
      params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
      params.height = RelativeLayout.LayoutParams.WRAP_CONTENT; 
      params.width = RelativeLayout.LayoutParams.WRAP_CONTENT; 
      holder.tv_item_chat_message_contentText.setLayoutParams(params); 

     } 
     break; 
    } 


    holder.tv_item_chat_message_contentText.setText(item.getMessage().trim()); 
    holder.tv_item_chat_message_nameText.setText(item.getSender().trim()); 
} 

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

public class Adapter_Chat_ViewHolder extends RecyclerView.ViewHolder { 

    public TextView tv_item_chat_message_nameText, tv_item_chat_message_contentText; 
    public ImageView iv_chat_profileImage; 

    public Adapter_Chat_ViewHolder(View itemView) { 
     super(itemView); 
     tv_item_chat_message_contentText = (TextView) itemView.findViewById(R.id.tv_item_chat_message_contentText); 
     tv_item_chat_message_nameText = (TextView) itemView.findViewById(R.id.tv_item_chat_message_nameText); 
     iv_chat_profileImage = (ImageView) itemView.findViewById(R.id.iv_chat_profileImage); 
    } 
} 
} 

而且佈局,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<ImageView 
    android:src="@drawable/bg" 
    android:scaleType="fitXY" 
    android:layout_margin="16dp" 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:id="@+id/iv_chat_profileImage" /> 

<TextView 
    android:id="@+id/tv_item_chat_message_nameText" 
    android:text="name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="64dp" 
    android:layout_marginTop="16dp" /> 

<TextView 
    android:layout_marginTop="32dp" 
    android:layout_marginLeft="64dp" 
    android:layout_marginRight="16dp" 
    android:gravity="right" 
    android:background="@drawable/out_message_bg" 
    android:id="@+id/tv_item_chat_message_contentText" 
    android:text="content" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

是不是有什麼毛病我onBindViewHolder方法?是不是要改變項目的路線應該是這樣?

回答

1

addRule()方法將保持添加到視圖的現有規則。因此,動態添加規則而不刪除現有的規則不會產生預期的效果。

更新下圖所示的開關盒。

switch (item.getType()) { 
    case mine: { 
     ---------- exisiting code ---------- 
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.tv_item_chat_message_contentText.getLayoutParams(); 
     params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
     params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); 
     params.height = RelativeLayout.LayoutParams.WRAP_CONTENT; 
     params.width = RelativeLayout.LayoutParams.WRAP_CONTENT; 
     holder.tv_item_chat_message_contentText.setLayoutParams(params); 

    } 
    break; 

    case others: { 
     ---------- exisiting code ----------   
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.tv_item_chat_message_contentText.getLayoutParams(); 
     params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
     params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 
     params.height = RelativeLayout.LayoutParams.WRAP_CONTENT; 
     params.width = RelativeLayout.LayoutParams.WRAP_CONTENT; 
     holder.tv_item_chat_message_contentText.setLayoutParams(params); 

    } 
    break; 
} 

對於API級別> = 17,一個新的方法是可用的,以消除任何現有的規則,而不是設置其爲0值我們可以使用

params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 

變得

params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
+0

太好了。我忘了刪除現有的規則。感謝您的回答。 – March3April4

0

Android佈局如ListView,Recycler View或gridView Android重用UI對象。

使用這種佈局時,應該總是記住處理if else條件,也就是說,如果您使用視圖在一個編碼中可見,則應該爲其提供其他視圖以使其不可見,所以當listview或recylerview重用視圖,它將始終按照條件顯示正確的視圖。

在這個問題中,您還添加了ALIGNT_PARENT_RIGHT和其他ALIGNT_PARENT_RIGHT項的規則,但是您忘了添加規則,不會將其顯示在我的LEFT和其他RIGHT上。

所以既可以使用addRule(ALIGN_PARENT_LEFT,0)或removeRule(ALIGN_PARENT_LEFT)(API> = JELLY_BEAN_MR1,i.e17),用於礦井和addRule(ALIGN_PARENT_RIGHT,0)或removeRule(ALIGN_PARENT_RIGHT)(API> = JELLY_BEAN_MR1,即17)。