このコードを使用して、GridView のいくつかの項目を動的に非表示にしようとしました。
public class GridHelper extends ArrayAdapter<Object>
{
private Context context;
private int layoutResourceId;
private ArrayList<Object> mainlist = null;
private ArrayList<Object> sichtbar = null;
public GridHelper(Context context, int layoutResourceId, ArrayList<Object> mainlist)
{
super(context, layoutResourceId, mainlist);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.mainlist = mainlist;
this.sichtbar = ArrayList<Object>();
// that's important otherwith the items are doublicated but the items
// are inside the List. I think the add method is called somewhere
// in the super constructor
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, null);
Object t = sichtbar.get(position);
if(t != null)
{
row = (View) t;
}
}
return row;
}
@Override
public int getCount()
{
return sichtbar.size();
}
@Override
public void add(Object object)
{
super.add(object);
sichtbar.add(object);
}
public void show(int pos)
{
if(sichtbar.contains(mainlist.get(pos)) == false)
{
sichtbar.add(mainlist.get(pos));
notifyDataSetChanged();
}
}
public void hide(int pos)
{
if(sichtbar.contains(mainlist.get(pos)) == true)
{
sichtbar.remove(mainlist.get(pos));
notifyDataSetChanged();
}
}
}
ただし、検索機能は機能しません。show/hide メソッドの後、リストは適切なサイズになっていますが、最初の項目 ( mainlist .get(0)
) は常に表示されており、適切な項目をカバーしていると思います。getView
メソッドは常にリストのサイズよりも 1 回多く呼び出されることがわかりました。リストに 3 つの項目がある場合、getView
メソッドは 4 回呼び出されます。
2 つ目は、GridView に 3 つのアイテムがあり、2 つのアイテムに対して hide 関数を呼び出すと、getView
メソッドが 4 回 (古いサイズ + 1) 呼び出され、さらに 2 回 (新しいサイズ + 1) 呼び出されることです。とても奇妙ですね。
何故ですか?その背後にある論理は正しいと思いますね。