0

このコードを使用して、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) 呼び出されることです。とても奇妙ですね。

何故ですか?その背後にある論理は正しいと思いますね。

4

2 に答える 2

0

これを行う最も簡単な方法は、アダプターで 2 つの個別のリストを維持することだと思います。1 つはすべての項目を含み、もう 1 つは目に見える項目のみを含みます。または、1 つは目に見えるアイテムを含み、もう 1 つは目に見えないアイテムを含みます。どちらも可能です。getCount()表示されているアイテムの数を返す必要があることに注意してください。

于 2013-05-24T23:12:50.987 に答える