Search code examples
androidgridviewandroid-gridview

Android GridView Scrolling issue


I'm facing a Problem, that my GridView "clips"/"glitches" after I scroll to the 4th row or longer than 2 seconds.
I got 58 Items, which get loaded via a Adapter into the GridView. An Item consists of a filename and an Image of the Item (Thumbnail). Each Thumbnail has a width and height of 100dp and is loaded into a ImageButton via the Framework "Glide" without resizing, crop or anything else. Simple Glide.load(ressource).into(imageButton).

Please see the attached images to follow my further explanation.
After Scrolling I would expect, that my Items are Aligned like the first 15-19 Items before. Unfortunately it is scrolling only the "last Item" of the 4th row from the GridView. That mean's that at Point 2 (red digit within the Picture) all the other items appearing for a short period if I scroll through them. After Scrolling, the GridView starts to get weird (see Point 2)

After scrolling further the whole GridView and Scrollbar get's "destroyed" and only a small amount of Item's appear and lastly 1 or none item's appear. I can see, that the Scrollbar is decreasing very fast, after scrolling. Completly weird behavior after scrolling for 2 seconds

GridView xml Properties (within main_activity.xml):

android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:verticalSpacing="20dp"
android:visibility="visible"

GridViewAdapter Code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    _layoutInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        _view = new View(_context);
        _view = _layoutInflater.inflate(R.layout.single_item, null);
        TextView textView = _view.findViewById(R.id.textView);
        final Item item = _items.get(position);
        textView.setText(item.getName());
        ImageButton imageButton = _view.findViewById(R.id.imageButton);
        Glide.with(_context).load(item.getDrawableRessource()).into(imageButton);
        imageButton.setOnClickListener(click -> {
            _iOnItemClickListener.onClick(item);
        });
    }
    return _view;
}

Thanks for any helpful advice.


Solution

  • Try this:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        _view = convertView;
        if (convertView == null) {
            _layoutInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            _view = _layoutInflater.inflate(R.layout.single_item, null);
        }
        TextView textView = _view.findViewById(R.id.textView);
        final Item item = _items.get(position);
        textView.setText(item.getName());
        ImageButton imageButton = _view.findViewById(R.id.imageButton);
        Glide.with(_context).load(item.getDrawableRessource()).into(imageButton);
        imageButton.setOnClickListener(click -> {
            _iOnItemClickListener.onClick(item);
        });
        return _view;
    }