Android Adapter multiple getView

前端 未结 4 754
一生所求
一生所求 2021-02-15 15:45

i have read about the issue of getView called multiple times and all the answers. However, i don\'t find a solution for my problem.

I have a list where rows have two sta

4条回答
  •  北海茫月
    2021-02-15 16:45

    I had the same problem and I had no reference at all to "wrap_content" in the layouts attirbute. Although this is an old thread I couldn't figured it out how to solve the issue. Thus, I mitigated it by adding a List in the Adapter that holds the positions already drawn, as shown in the code below. I think that it is not the right away of doing that, but it worked for me.

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;
        private List usedPositions  = new ArrayList();
    
        public ImageAdapter(Context c, List imageUrls) {
            mContext = c;
            ...
        }
    
        ...
    
        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) { 
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }
    
            if (!usedPositions.contains(position)) {
                // Your code to fill the imageView object content
                usedPositions.add(position); // holds the used position
            }
    
            return imageView;
        }
    }
    

提交回复
热议问题