如何使用Android GridView适配器仅加载一次项目

时间:2014-08-01 12:55:02

标签: android gridview android-adapter

我有一个自定义的GridView适配器(但这是无关紧要的,因为我已经看到了其他适配器的这种行为,但它从来没有给我这么多的烦恼),我使用项目列表设置为GridView

我调用的函数中的代码在Activity中创建我的用户界面:

gridviewAdapter = new MyGridViewAdapter(this, itemList);
gv.setAdapter(gridviewAdapter);

我的适配器类中的getView函数:

public View getView(int position, View convertView, ViewGroup parent)
    {
        MyGridViewItemData itemData = itemList.get(position);

        Log.d(TAG, "MyGridViewAdapter-> Load view for " + itemData.getCategoryTitle());

        View v = new MyGridViewItemView(this.context, itemData );

        v.setOnClickListener((View.OnClickListener) parent);

        v.setOnLongClickListener((View.OnLongClickListener) parent);
        v.setOnTouchListener ((View.OnTouchListener) parent);
        return v;
    }

从我的日志中我可以看到,我给适配器的项目列表中的相同项目被多次创建;不规则。我已经看到同样的项目被创建18次,48次!这不会连续发生 - 列表中的其他项也会在中间创建,也可以多次创建。

我的日志:

MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Fruit
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Fruit
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Fruit
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Fruit
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Fruit
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Fruit
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Fruit
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Fruit
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops
MyGridViewAdapter-> Load view for Crops

这是为什么?如果适配器尚未显示,我怎样才能让适配器只创建项目?

2 个答案:

答案 0 :(得分:0)

你应该在你的情况下使用Holder模式。

  

如果Android确定某行不再可见,则允许该行   适配器方法的getView()通过重用关联的视图   convertView参数。

     

适配器可以将新数据分配给视图中包含的视图   convertView的层次结构。这可以避免膨胀XML文件和   创建新的Java对象。

     

如果Android无法重复使用某行,则Android系统会将null传递给   convertView参数。因此适配器实现需要   检查一下。

有关详细信息,请参阅此tutorial

答案 1 :(得分:0)

您可以尝试:

View v;

if(convertView == null){

    MyGridViewItemData itemData = itemList.get(position);

    Log.d(TAG, "MyGridViewAdapter-> Load view for " + itemData.getCategoryTitle());

    v = new MyGridViewItemView(this.context, itemData );

    convertView.setTag(v);

} else{

    v = (View) convertView.getTag();

}
...