我有一个gridview,里面有很多自定义视图。当我向上或向下滚动时,以前离开屏幕的视图现在被替换为不同的视图。你知道为什么会这样吗?

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    LayoutInflater inflater =  (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout rLayout = (RelativeLayout)inflater.inflate(R.layout.collection_item, parent, false);

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        ImageView movieImage = (ImageView)rLayout.findViewById(R.id.movieImage);
        TextView movieTitle = (TextView)rLayout.findViewById(R.id.movieTitle);
        TextView movieDescription = (TextView)rLayout.findViewById(R.id.movieDescription);

        movieImage.setImageResource(movies[position]);
        movieTitle.setText(movieNames[position]);
        movieDescription.setText(movieDescriptions[position]);
    } else {
        rLayout = (RelativeLayout) convertView;
    }
    return rLayout;
}

最佳答案

我没想到。只需将所有初始化代码放在if/else块之后的(convertview==null)部分和setter方法中。

07-24 13:37