2016-08-01 9 views
0

enter image description here私は簡単なチャットUIを書いています。私はこの作業に9パッチファイルを使用しています。Androidのリサイクルアイテムのレイアウトパラメータが正しく表示されない

モックデータでアダプタを初期化するとうまくいきますが、実行時に何かを追加すると、recyclerView内のレイアウトが壊れます。

アライメントの設定に使用したレイアウトパラメータは機能していませんが、エラーは発生しません。実際には、aligmentは動作しますが、サイズはwrap_contentではなくmatch_parentになります。

ここに私のアダプタコードです。

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()メソッドは、既存のルールをビューに追加したままにします。したがって、既存のルールを削除せずにルールを動的に追加しても、目的の効果は得られません。

以下のようにスイッチケースを更新してください。 APIレベルについては

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; 
} 

> = 17、新しい方法は、既存のルールを削除するのではなく0としてその値を設定することが可能です我々は

params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 

params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
となって使用することができます
+0

私は既存のルールを削除するのを忘れてしまった。ご回答有難うございます。 – March3April4

0

リストビュー、リサイクルビュー、またはgridView Android再利用UIオブジェクトのようなAndroidレイアウト。

この種のレイアウトを使用している間は、常にif else条件を処理することを覚えておく必要があります。つまり、あるコーディネートで表示されるビューを使用している場合、そのビューを表示できないようにする必要があります。再利用ビュー再利用ビューは、条件ごとに常に正しいビューを表示します。

この質問にも、私のためにALIGNT_PARENT_RIGHTに、他にはALIGN_PARENT_LEFTにルールが追加されていますが、私はLEFTでそれを表示しないようにルールを追加するのを忘れました。

ですから、鉱山および追加ルール(ALIGN_PARENT_RIGHT、0)またはremoveRule(ALIGN_PARENT_RIGHT)(API> = JELLY_BEAN_MR1、のために追加ルール(ALIGN_PARENT_LEFT、0)またはremoveRule(ALIGN_PARENT_LEFT)(API> = JELLY_BEAN_MR1、i.e17)を使用しますかすなわち、私のために17)。

関連する問題