Android 不同元素/项目的列表视图
我试图实现一个Android 不同元素/项目的列表视图,android,listview,Android,Listview,我试图实现一个列表视图,在第一行有一个图像,在其余项目上有标题,但它不起作用。有什么解决办法吗 这是我的密码 在适配器类中不使用视图 @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_S
列表视图
,在第一行有一个图像,在其余项目上有标题,但它不起作用。有什么解决办法吗
这是我的密码
在适配器类中不使用视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
imageView.setImageResource(R.drawable.phote);
return rowView;
}
用这个
if (position % 2 == 1) {
inflater= (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.profileimage,parent,false);
ImageView drawerprofile=(ImageView) findViewById(R.id.imageViewdrawer);
drawerprofile.setImageResource(R.drawable.camera);
} else {
inflater= (LayoutInflater) c.getSystemService(c.LAYOUT_INFLATER_SERVICE);
View v=inflater.inflate(R.layout.listviewitems,parent,false);
TextView tv1=(TextView) findViewById(R.id.textView1);
tv1.setText(titles[position]);
}
ListView有一个标题。您可以在Header中添加元素,在other中添加TextView
您需要重用这些视图。首先检查converView是否为空,只有当它为空时才充气。您没有返回正确的视图。在v变量中对每个视图进行膨胀,然后返回视图 因此,请替换:
View v=inflater.inflate(R.layout.listviewitems,parent,false);
借
此外,您还必须在膨胀视图中查找视图,而不是活动/片段视图。我的意思是使用这个:
ImageView drawerprofile=(ImageView) view.findViewById(R.id.imageViewdrawer);
而不是:
ImageView drawerprofile=(ImageView) findViewById(R.id.imageViewdrawer);
但是这里的最佳实践是为ListView使用一个标题,在这个标题中可以放置ImageView,其余的行将在getView方法中返回。此外,您必须在此链接中使用ViewHolder模式,您可以看到如何实现它:有什么问题??
ImageView drawerprofile=(ImageView) view.findViewById(R.id.imageViewdrawer);
ImageView drawerprofile=(ImageView) findViewById(R.id.imageViewdrawer);