开发者

Android 2.3: GridView out of order after scrolling

开发者 https://www.devze.com 2023-03-19 20:17 出处:网络
I have a GridView loaded with a bunch of custom views.When I scroll up or down, the views that went off screen b开发者_JAVA技巧efore are now replaced with different views.Any ideas why this happens?

I have a GridView loaded with a bunch of custom views. When I scroll up or down, the views that went off screen b开发者_JAVA技巧efore 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;
}


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.


This might be becouse of image loading try using Picasso library

Solution is instead of using bitmap to load image directly use a awesome Library called Picasso its just super fast i know you really love this you can do this like this

Add picasso jar file to your project (Download picasso jar file here) Use picasso to load the Image like this

Picasso.with(context).load(new File(title)).centerCrop()
        .resize(150, 150).error(R.drawable.ic_launcher).into(image);  

where title is the image path which you want to load. Crop,resize, error are optional.

0

精彩评论

暂无评论...
验证码 换一张
取 消