Android GridView滚动问题

问题描述 投票:0回答:1

我面临的一个问题是,当我滚动到第四行或超过2秒后,我的GridView会“卡住” /“出现故障”。我得到58个项目,这些项目通过适配器加载到GridView中。一个项目包含一个文件名和一个项目的图像(缩略图)。每个缩略图的宽度和高度为100dp,并通过“ Glide”框架加载到ImageButton中,而无需调整大小,裁剪或其他任何操作。简单Glide.load(ressource).into(imageButton)

请参阅附件中的图像,以按照我的进一步说明进行操作。滚动后,我希望我的项目像之前的前15-19个项目一样对齐。不幸的是,它仅滚动GridView中第四行的“最后一个项目”。这就是说,如果我滚动查看其他所有项目,则在第2点(图片中的红色数字)会在短时间内出现。After Scrolling, the GridView starts to get weird (see Point 2)

进一步滚动整个GridView和Scrollbar之后,将其“销毁”,仅出现少量Item,最后出现1或不显示任何Item。我可以看到,滚动后滚动条的下降非常快。Completly weird behavior after scrolling for 2 seconds

GridView xml属性(在main_activity.xml中):

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

GridViewAdapter代码:

@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;
}

感谢您提供任何有用的建议。

android gridview android-gridview
1个回答
0
投票

尝试一下:

@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;
}
© www.soinside.com 2019 - 2024. All rights reserved.