jin*_*yin 8 layout android listview
我正在应用程序中的聊天模块,我希望来自两个参与者的消息反对对齐(其他用户左对齐和我自己的消息右对齐).现在,我的行布局通过静态布局xml传入(msg和avatar左对齐).有没有办法动态修改视图,或者有没有办法为UI系统传递备用行布局以便在运行时选择?
您可以在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;
}
Run Code Online (Sandbox Code Playgroud)
getView
每次ListView
修改时都会调用(例如滚动时或添加新元素时),因此您可以根据需要操作每一行.不要忘记将数组适配器设置ListView
为此类的实例.
listView.setListAdapter(new mYourAdapter);
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
1.您的Cusom Adapter类扩展了基础适配器.2.覆盖getItemViewType()和getViewTypeCount()3.根据getView()中的类型对视图进行膨胀
归档时间: |
|
查看次数: |
6953 次 |
最近记录: |