简体   繁体   English

GridView重复图像但不重复Android中的文本

[英]Gridview repeating images but not the text in android

In ImageView , my images are being shuffled but not the text. ImageView ,我的图像(而不是文本)被重新排列。 This is my getView() method: 这是我的getView()方法:

public View getView(int position, View convertView, ViewGroup parent) {
    View v;

    if (convertView == null) {  
        LayoutInflater inflater = (LayoutInflater) mainActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    v = inflater.inflate(R.layout.item_people, parent, false);
    } else {
        v = (View) convertView;
    }

    TextView textView = (TextView) v.findViewById(R.id.name);
    ImageView imageView = (ImageView) v.findViewById(R.id.image);

    if (list.get(position).getClass() == SearchData.Video.class) {
        SearchData.Video video = (SearchData.Video) list.get(position);
        textView.setText(video.getVideoName());

        if (video.getCoverPicture().length > 0)
            imageView.setBackground(mainActivity.Base64toImage(video.getCoverPicture()[0].getImg()));
    } else if (list.get(position).getClass() == SearchData.Actor.class) {
        SearchData.Actor actor = (SearchData.Actor) list.get(position);
        textView.setText(actor.getFirstName());

        if (actor.getPicture().length > 0)
            imageView.setBackground(mainActivity.Base64toImage(actor.getPicture()[0].getImg()));
    }
    return v;
}

I am setting image and text of actors. 我正在设定演员的形象和文字。 When I scroll down then up the images have shuffled, but not text. 当我向下滚动然后向上滚动时,图像已经打乱了,但是没有文字。 Why? 为什么?

Did you tried the same with ViewHolder 您是否尝试过使用ViewHolder进行相同操作

Normally when ever getView() method is called, the gridview/ listview will automatically notified as a change, So that is the reason for your image shuffling s / changing in your gridview. 通常,当调用getView()方法时,gridview / listview会作为更改自动通知,因此这就是在gridview中图像改组/更改的原因。

So, try to implement your Gridview with ViewHolder pattern and avoid those shuffling s / re-orderings 因此,请尝试使用ViewHolder模式实现Gridview并避免那些拖尾的s /重新排序

here is the sample code for view holder pattern implementation. 这是视图持有者模式实现的示例代码。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolderItem viewHolder;


      // The convertView argument is essentially a "ScrapView" as described is Lucas post 
      // http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
      // It will have a non-null value when ListView is asking you recycle the row layout. 
      // So, when convertView is not null, you should simply update its contents instead of inflating a new row    layout.

    if(convertView==null){

        // inflate the layout
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId, parent, false);

        // well set up the ViewHolder
        viewHolder = new ViewHolderItem();
        viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);

        // store the holder with the view.
        convertView.setTag(viewHolder);

    }else{
        // we've just avoided calling findViewById() on resource everytime
        // just use the viewHolder
        viewHolder = (ViewHolderItem) convertView.getTag();
    }

    // object item based on the position
    ObjectItem objectItem = data[position];

    // assign values if the object is not null
    if(objectItem != null) {
        // get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
        viewHolder.textViewItem.setText(objectItem.itemName);
        viewHolder.textViewItem.setTag(objectItem.itemId);
    }

    return convertView;

}

your ViewHodler should be like this 您的ViewHodler应该是这样的

// ViewHolder.
// caches our TextView
static class ViewHolderItem {
    TextView textViewItem;
}

This is not the exactly same to your question, but you can edit the above logic for your way. 这与您的问题并不完全相同,但是您可以按照自己的方式编辑以上逻辑。

Hope it Helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM