Reputation: 91
I have a GridView loaded with a bunch of custom views. When I scroll up or down, the views that went off screen before are now replaced with different views. Any ideas why this happens?
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;
}
Upvotes: 3
Views: 1065
Reputation: 91
Nevermind I figured it out. Just put all the initialization code into the (convertView == null) part and the setter methods after the if/else block.
Upvotes: 3