大佬教程收集整理的这篇文章主要介绍了android – 自定义ListView适配器,奇怪的ImageView行为,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的适配器的相关方法:
public View getView( int position,View convertView,ViewGroup parent ) { View v = convertView; if( v == null ) { LayoutInflater vi = (LayoutInflater)getContext().getSystemservice(Context.LAYOUT_INFLATER_serviCE); v = vi.inflate( R.layout.generic_row,null ); } // find the image ImageView favImage = (ImageView)v.findViewById( R.id.toggle_favorite ); // when clicked... favImage.setOnClickListener( new OnClickListener() { @Override public void onClick( View v ) { // make the gray star a yellow one int newImage = R.drawable.ic_star_yellow_embossed; ((ImageView)v).setImageBitmap(BitmapFactory.decoderesource(getContext().getresources(),newImagE)); } }); return v; }
首先,您应该存储ImageView的状态.我使用了ArrayList< Boolean>作为自定义适配器的成员,如果此列表中的位置(对应于列中行的位置)为false,则图像是默认值,否则如果为真,则用户单击它,我们应该放置新的图片:
private ArrayList<Boolean> imageStatus = new ArrayList<Boolean>();
在自定义适配器构造函数中初始化此列表.例如,如果您在适配器中放入了某个列表,那么您应该使imageStatus与该列表一样大并填充false(默认/开始状态):
//... initialize the imageStatus,objects is the list on which your adapter is based for (int i = 0; i < objects.size(); i++) { imageStatus.add(false); }
View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) getContext() .getSystemservice(Context.LAYOUT_INFLATER_serviCE); v = vi.inflate(R.layout.adapters_adapter_with_images,null); } // find the image ImageView favImage = (ImageView) v .findViewById(R.id.toggle_favoritE); // Set the image bitmap. If the imageStatus flag for this position is TRUE then we // show the new image because it was prevIoUsly clicked by the user if (imageStatus.get(position)) { int newImage = R.drawable.ic_star_yellow_embossed; favImage.setImageBitmap(BitmapFactory.decoderesource( getContext().getresources(),newImagE)); } else { // If the imageStatus is falSE then we explicitly set the image // BACk to the default because we Could be dealing with a // recycled ImageView that has the new image set(there is no need to set a default drawable in the xml layout) int newImage = R.drawable.basket_empty; //the default image favImage.setImageBitmap(BitmapFactory.decoderesource( getContext().getresources(),newImagE)); } // when clicked... we get the real position of the row and set to TRUE // that position in the imageStatus flags list. We also call notifyDataSetChanged //on the adapter object to let it kNow that something has changed and to update! favImage.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Integer realPosition = (Integer) v.getTag(); //get the position from the view's tag imageStatus.set(realPosition,truE); //this position has been clicked be the user adapter.notifyDataSetChanged(); //notify the adapter } }); // set the position to the favImage as a tag,we later retrieve it // in the onClick method favImage.setTag(new Integer(position)); return v; }
如果您不打算动态修改列表(删除/添加行),这应该可以正常工作,否则您还必须注意修改imageStatus列表以反映更改.您没有说明您的行数据是什么,另一种方法(如果您计划在用户单击该图像时执行某些操作(除了更改它之外),则说明正确的方法)是将图像的状态合并到行的数据模型中.关于这一点,这里有一些教程:
Android ListView Advanced Interactive
或Commonsware-Android Excerpt(交互式行)
以上是大佬教程为你收集整理的android – 自定义ListView适配器,奇怪的ImageView行为全部内容,希望文章能够帮你解决android – 自定义ListView适配器,奇怪的ImageView行为所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。