我正在應用程序中的聊天模塊上工作,我希望來自兩名參與者的反向對齊消息(其他用戶左對齊,而我自己的消息右對齊)。現在,我的行佈局通過靜態佈局xml傳遞(msg和avatar左對齊)。有沒有辦法動態地修改視圖,還是有辦法通過UI系統的替代行佈局在運行時選擇?動態更改ListView的行佈局
8
A
回答
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)並將可見屬性設置爲不想要的視圖屬性。
我試着找到一些示例代碼,如果不清楚?
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);
}
}
}
相關問題
- 1. 動態更改佈局
- 2. 運行時動態佈局更改
- 3. 如何動態更改ListView的項目佈局
- 4. 動態更改活動的佈局
- 5. ListView - 修改行佈局
- 6. Android的更改佈局動態
- 7. ListView動態數據行更改滾動
- 8. 更改ListView中下一項的佈局
- 9. 如何動態更改與另一個佈局的佈局
- 10. 動態更改Javascript D3佈局模擬
- 11. 如何動態更改子佈局
- 12. 動態更改佈局背景
- 13. 在DialogFragment中動態更改佈局
- 14. Android:動態佈局更改對話框
- 15. 在Android中動態更改佈局
- 16. 動態更改鍵盤佈局
- 17. iOS 6 CollectionView動態更改佈局
- 18. 如何動態更改佈局背景
- 19. Android動態更改佈局大小
- 20. 動態佈局修改Android
- 21. 動態創建佈局(類似於listview)
- 22. 動態更改ListView BaseAdapter
- 23. 動態更改ListView內容
- 24. 如何更改沒有佈局的listview的行屬性?
- 25. 在更改ListView控件的佈局時保留滾動位置
- 26. 更改活動佈局
- 27. Android - 保存狀態的動態更改佈局
- 28. 動態修改佈局比靜態佈局
- 29. 如何在Android中拖動佈局時動態更改佈局背景?
- 30. Android ListView使用自定義行佈局更改特定元素
哦,這是可行的?我最終做了一個自定義的simplecursoradapter,在那裏我傳遞了一個int [] []而不是int []作爲視圖ID數組,因爲我之前一直使用simplecursoradapter。 – jingyin