GridView scrolling problem on Android

后端 未结 5 1667
走了就别回头了
走了就别回头了 2021-02-02 03:06

this must be somethng very simple I\'m overlooking, but I have the following problem (the post is rather lengthy, but I want to provide as much info as possible :) ).

I

5条回答
  •  天命终不由人
    2021-02-02 03:53

    Actually the problem here is that you set the content of the "convertView" within the if or else. Or you should always do that after instantiating the view if it is null and set the content only before returning the view.

    Consequently, you're sure the content of the view is always the right one, updated using the position and not a false recycled view.

    So your should generally speaking do the following:
    (based on the tutorial from Android Guide here)

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { 
            imageView = new ImageView(mContext);
            //just creating the view if not already present
        } else {
            imageView = (ImageView) convertView;
            //re-using if already here
        }
    
        //here is the tricky part : set the content of the view out of the if, else
        //just before returning the view
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    

提交回复
热议问题