代码之家  ›  专栏  ›  技术社区  ›  Steve Haley

ListView:convertView/holder混淆

  •  0
  • Steve Haley  · 技术社区  · 16 年前

    有人能看到我在下面的代码中做错了什么吗?

    static LinearLayout.LayoutParams layoutParams;
    (...)
    
    public View getView(int position, View convertView, ViewGroup parent){
        ReferenceHolder holder;
    
        if (convertView == null){
            convertView = inflater.inflate(R.layout.pick_image_row, null);
            holder = new ReferenceHolder();
            holder.getIdsAndSetTag(convertView, position);
            if (data[position][ORIENTATION] == LANDSCAPE) {
                // Layout defaults to portrait settings, so ImageView size needs adjusting. 
                // layoutParams is modified here, with specific values for width, height, margins etc
                holder.image.setLayoutParams(layoutParams);
            }
            holder.orientation = data[position][ORIENTATION];
    
        } else {
            holder = (ReferenceHolder) convertView.getTag();
            if (holder.orientation != data[position][ORIENTATION]){  //This is the key if statement for my question
                switch (image[position][ORIENTATION]) {
                case PORTRAIT:
                    // layoutParams is reset to the Portrait settings
                    holder.orientation = data[position][ORIENTATION];
                    break;
    
                case LANDSCAPE:
                    // layoutParams is reset to the Landscape settings
                    holder.orientation = data[position][ORIENTATION];
                    break;
                }
                holder.image.setLayoutParams(layoutParams);
            }               
        }
    
        // and the row's image and text is set here, using holder.image.xxx
        // and holder.text.xxx
    
        return convertView;
    }
    
    static class ReferenceHolder {
        ImageView image;
        TextView text;
        int orientation;
    
        void getIdsAndSetTag(View v, int position){
            image = (ImageView) v.findViewById(R.id.pickImageImage);
            text = (TextView) v.findViewById(R.id.pickImageText);
            orientation = data[position][ORIENTATION];
            v.setTag(this);
        }
    }
    

    谢谢!

    1 回复  |  直到 16 年前
        1
  •  2
  •   CommonsWare    16 年前

    ReferenceHolder ,检查实际 LayoutParams ImageView 看看它的方位。这样,从定义上讲,你就不能不同步了。

    老实说,我被你的代码弄糊涂了,因为你似乎从未改变过 layoutParams layoutParamsPortrait layoutParamsLandscape 或者别的什么?在我看来,规则是:

    • 其他的一切都是风景,不管发生了什么 orientation 布局参数 ,大概是景观