3

GridViewのフッタービューを追加したいと思います。

ドキュメントで、GridViewには2つの継承されたaddView(View child)メソッドがあることがわかりました。

From class android.widgetAdapterView

void addView(View child)

This method is not supported and throws an UnsupportedOperationException when called.

From class android.view.ViewGroup

void addView(View child)

Adds a child view.

後者を使うべきだと思います。しかし、どうすればこの特定の継承されたメソッドを呼び出すことができますか?

4

2 に答える 2

3

あなたはそうしない。UnsupportedOperationExceptionそれは..まあ..サポートされていないので、それはオリジナルをで上書きします。

代わりにアダプタを編集する必要があります。実装に応じて、これは異なって見えます。ただし、アダプタにデータを追加し、アダプタを呼び出すだけ.notifyDataSetChanged()GridView、ビューが自動的に追加されます。

Viewフッタービューは、の後に個別にするGridView必要があります。そうでない場合は、新しいアイテムを追加するたびに常に最後になるように、アダプターのリスト内での位置を維持する必要があります。

于 2012-11-04T04:07:31.580 に答える
0

Ericsソリューションの例を示すと、アダプターは「フッター」の位置を追跡するための2つの追加メンバーと、そのイベントハンドラーを維持できます。

class ImageViewGridAdapter : ArrayAdapter<int>
{
    private readonly List<int> images;

    public int EventHandlerPosition { get; set; }
    public EventHandler AddNewImageEventHandler { get; set; }

    public ImageViewGridAdapter(Context context, int textViewResourceId, List<int> images)
        : base(context, textViewResourceId, images)
    {
        this.images = images;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        ImageView v = (ImageView)convertView;

        if (v == null)
        {
            LayoutInflater li = (LayoutInflater)this.Context.GetSystemService(Context.LayoutInflaterService);
            v = (ImageView)li.Inflate(Resource.Layout.GridItem_Image, null);

            // ** Need to assign event handler in here, since GetView 
            // is called an arbitrary # of times, and the += incrementor
            // will result in multiple event fires

            // Technique 1 - More flexisble, more maintenance ////////////////////
            if (position == EventHandlerPosition)            
                v.Click += AddNewImageEventHandler;

            // Technique 2 - less flexible, less maintenance /////////////////////
            if (position == images.Count)
                v.Click += AddNewImageEventHandler;
        }

        if (images[position] != null)
        {
            v.SetBackgroundResource(images[position]);
        }

        return v;
    }
}

次に、アダプタをグリッドビューに割り当てる前に、これらの値を割り当てるだけです(位置は最後である必要はありませんが、フッターの場合はそうする必要があります)。

List<int> images = new List<int> { 
    Resource.Drawable.image1, Resource.Drawable.image2, Resource.Drawable.image_footer 
};

ImageViewGridAdapter recordAttachmentsAdapter = new ImageViewGridAdapter(Activity, 0, images);

recordAttachmentsAdapter.EventHandlerPosition = images.Count;
recordAttachmentsAdapter.AddNewImageEventHandler += NewAttachmentClickHandler;

_recordAttachmentsGrid.Adapter = recordAttachmentsAdapter;
于 2013-06-20T15:54:26.137 に答える