Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 自定义ListView适配器,奇怪的ImageView行为大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个自定义ListView适配器,我正在为列表创建行.但问题是,它似乎并没有区分 ImageViews.当我向上和向下滚动时,它似乎是随机挑选ImageViews卡到位.文本信息(在此代码段中省略)不会中断.它可以像人们期望的那样工作.

这是我的适配器的相关方法

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;
  }

解决方法

出现这种情况是因为当您向上和向下滚动列表时,ListView会回收行视图,因此您可以获用户操作的行(图像已更改),图像应该是未修改的.为避免这种情况,您必须以某种方式保持列表中每一行的ImageView状态,并使用此状态在getView()方法中设置正确的图像.因为您没有说明您是如何实现适配器的,所以我将向您展示一个简单的示例.

首先,您应该存储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);
}

然后在你的getView()方法中:

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(交互式行)

@H_618_41@

大佬总结

以上是大佬教程为你收集整理的android – 自定义ListView适配器,奇怪的ImageView行为全部内容,希望文章能够帮你解决android – 自定义ListView适配器,奇怪的ImageView行为所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。