2011-08-15 137 views
8

我正在應用程序中的聊天模塊上工作,我希望來自兩名參與者的反向對齊消息(其他用戶左對齊,而我自己的消息右對齊)。現在,我的行佈局通過靜態佈局xml傳遞(msg和avatar左對齊)。有沒有辦法動態地修改視圖,還是有辦法通過UI系統的替代行佈局在運行時選擇?動態更改ListView的行佈局

回答

5

您可以在ArrayAdapter類的getView()方法中執行此操作(假設您正在定義自己的ArrayAdapter)。

你可能有這樣的事情:

private class YourAdapter extends ArrayAdapter<Message> { 
     private final LayoutInflater mLayoutInflater; 

    YourAdapter(YourListActivity activity) { 
     super(mContext, 0); 
     mLayoutInflater = LayoutInflater.from(activity); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      // Inflate your view 
      convertView = mLayoutInflater.inflate(R.layout.list_view_item, parent, false); 
      mViewHolder = new ViewHolder(); 
      mViewHolder.avatar = (ImageView) convertView.findViewById(R.id.placeholder); 
      mViewHolder.message = (TextView) convertView.findViewById(R.id.message); 

      convertView.setTag(mViewHolder); 
     } else { 
      mViewHolder = (ViewHolder) convertView.getTag(); 
     } 

     final Message message = getItem(position); 

     mViewHolder.message.setText(message.getMessage()); 
     // etc. Manipulate your views as you wish 


     return convertView; 
    } 
} 


    private static class ViewHolder { 
     TextView message; 
     ImageView avatar; 
    } 

getView會得到您每次ListView被修改時(當您滾動或當新的元素添加到它像)叫,所以你可以操縱的每一行 如你所願。 不要忘記將ListView的陣列適配器設置爲此類的一個實例。

listView.setListAdapter(new mYourAdapter); 
2

我已經顯示出這樣做的一種方式(林不知道,如果它的最佳實踐或不!)是在一個XML文件,這兩種觀點。在運行時,您可以獲取對每個視圖的引用(使用findViewById)並將可見屬性設置爲不想要的視圖屬性。

我試着找到一些示例代碼,如果不清楚?

+0

哦,這是可行的?我最終做了一個自定義的simplecursoradapter,在那裏我傳遞了一個int [] []而不是int []作爲視圖ID數組,因爲我之前一直使用simplecursoradapter。 – jingyin

5
private LayoutInflater mInflater; 
private static final int TYPE_ITEM1 = 0; 
private static final int TYPE_ITEM2 = 1; 
ArrayList<String> s= new ArrayList<String>(); 
int time; 
String names[]={"raghu","pavan","rakesh","raghu","pavan"}; 
Context c; 

@Override 
public int getItemViewType(int position) { 
    if((position%2)==0) 
    { 
     return 0; 
    } 
    return 1;  
} 

@Override 
public int getViewTypeCount() { 
    return 2; 
} 

public Customlistadapter(CustomListView customListView, int time) { 
    // TODO Auto-generated constructor stub 
    for(int i=0;i<=10;i++) 
    { 
     s.add("Raghu"); 
    } 
    this.mInflater = LayoutInflater.from(customListView); 
    c=customListView; 
    this.time=time; 
} 
public int getCount() { 
    return s.size(); 
} 

public Object getItem(int arg0) { 
    return s.get(arg0); 
} 

public long getItemId(int arg0) { 
return 0; 
} 

@Override 
public void notifyDataSetChanged() { 
    super.notifyDataSetChanged(); 
    if(CustomListView.chk==true) 
    { 
    s.add("Raghu"); 
    } 
    else if(CustomListView.chk==false) 
    { 
    s.remove(s.size()-1); 
    }  
} 
@Override 
public void notifyDataSetInvalidated() { 
    super.notifyDataSetInvalidated(); 
} 
public View getView(final int arg0, View arg1, ViewGroup arg2) { 
    ViewHolder vh; 
    vh= new ViewHolder(); 
    int type = getItemViewType(arg0); 
    System.out.println("getView " + arg0 + " type = "+type); 
    if(arg1==null) 
    { 
     switch (type) { 
     case TYPE_ITEM1: 
      arg1=mInflater.inflate(R.layout.listview, arg2,false);    
      vh.tv= (TextView)arg1.findViewById(R.id.textView1); 
      vh.tv1= (TextView)arg1.findViewById(R.id.textView2); 
      vh.tv2=(TextView)arg1.findViewById(R.id.textView3); 
      vh.tv.setText(s.get(arg0)); 
      vh.tv1.setText(s.get(arg0)); 
      vh.tv2.setText(Integer.toString(time)); 
      break; 
     case TYPE_ITEM2: 
      arg1=mInflater.inflate(R.layout.listviewimg, arg2,false); 
      vh= new ViewHolder(); 
      vh.iv1= (ImageView)arg1.findViewById(R.id.iv1); 
      vh.iv2= (ImageView)arg1.findViewById(R.id.iv2); 
      vh.iv1.setBackgroundResource(R.drawable.ic_launcher); 
      vh.iv2.setBackgroundResource(R.drawable.ic_launcher); 
      break; 
    } 
     arg1.setTag(vh); 
    } 
    else 
    { 
     vh= (ViewHolder) arg1.getTag(); 
    } 
    return arg1; 
} 

1.您的Cusom Adapter類擴展了Base Adapter。 2.覆蓋getItemViewType()和getViewTypeCount() 3.充氣取決於類型getView()視圖

0

我的代碼:

private void setAlignment(ViewHolder holder, boolean isOutgoing, QBChatMessage chatMessage) { 

    if (!isOutgoing) { 
     LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams(); 
     layoutParams.gravity = Gravity.LEFT; 
     holder.contentWithBG.setLayoutParams(layoutParams); 

     RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content.getLayoutParams(); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     holder.content.setLayoutParams(lp); 

     layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams(); 
     layoutParams.gravity = Gravity.RIGHT; 
     holder.txtInfo.setLayoutParams(layoutParams); 

     if (holder.txtMessage != null) { 
      holder.contentWithBG.setBackgroundResource(R.drawable.bubblevioletcopy); 
      layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams(); 
      layoutParams.gravity = Gravity.RIGHT; 
      holder.txtMessage.setLayoutParams(layoutParams); 
      holder.lnr_image.setLayoutParams(layoutParams); 
     } else { 
      holder.contentWithBG.setBackgroundResource(android.R.color.transparent); 
     } 
    } else { 
     LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.contentWithBG.getLayoutParams(); 
     layoutParams.gravity = Gravity.LEFT; 
     holder.contentWithBG.setLayoutParams(layoutParams); 

     RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.content.getLayoutParams(); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     holder.content.setLayoutParams(lp); 

     layoutParams = (LinearLayout.LayoutParams) holder.txtInfo.getLayoutParams(); 
     layoutParams.gravity = Gravity.RIGHT; 
     holder.txtInfo.setLayoutParams(layoutParams); 

     if (holder.txtMessage != null) { 
      holder.contentWithBG.setBackgroundResource(R.drawable.bubblegraycopy); 
      layoutParams = (LinearLayout.LayoutParams) holder.txtMessage.getLayoutParams(); 
      layoutParams.gravity = Gravity.RIGHT; 
      holder.txtMessage.setLayoutParams(layoutParams); 
     } else { 
      holder.contentWithBG.setBackgroundResource(android.R.color.transparent); 
     } 
    } 
} 
+0

僅限於聊天模塊 –

+0

除了代碼之外,請提供一些解釋。 – Jan

+0

我在聊天模塊上工作,當我發送消息,然後不同的佈局或更改視圖和另一個人發送消息,然後顯示不同的設計 –