我正在开发一个应用程序中的聊天模块,我希望来自两个参与者的消息反对对齐(其他用户左对齐和我自己的msg右对齐)。现在,我的行布局通过静态布局xml传入(msg和avatar左对齐)。有没有办法动态修改视图,或者有没有办法为UI系统传递备用行布局以便在运行时选择?
答案 0 :(得分: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);
答案 1 :(得分: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类扩展了基础适配器。 2.覆盖getItemViewType()和getViewTypeCount() 3.根据getView()
中的类型对视图进行膨胀答案 2 :(得分:2)
我已经证明了这样做的一种方式(我不确定它是否是最佳实践!)是在一个XML文件中同时拥有两个视图。在运行时,您可以获得对每个视图的引用(使用findViewById)并将visible属性设置为您不想要的属性。
如果不清楚,请尝试找一些示例代码?
答案 3 :(得分: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);
}
}
}