Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ItemClick event not firing in ListView with custom adapter #9

Open
wesfincher opened this issue Jan 24, 2014 · 0 comments
Open

ItemClick event not firing in ListView with custom adapter #9

wesfincher opened this issue Jan 24, 2014 · 0 comments

Comments

@wesfincher
Copy link

I've created a custom adapter, ExistingGameAdapter, that holds a collection of IListViewItems. IListViewItems is implemented by either HeaderListViewItem or SelectableListViewItems. When a SelectableListViewItem is selected, I need to hook into the ItemClick event for that custom adapter. It appears as if the OnInterceptTouchEvent is suppressing that event. I mucked around with that method but couldn't seem to figure it out...

Below is my layout:
<pulltorefresharp.android.views.ViewWrapper
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_below="@id/prestart_tv_Question"
android:layout_weight="2.5"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:background="@layout/listviewbackground">
<pulltorefresharp.android.widget.ListView
android:id="@+id/gameselect_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@drawable/listview_itembackground"
android:padding="2dp"
android:cacheColorHint="#003334"
android:background="@layout/listviewbackground"
android:minHeight="?android:attr/listPreferredItemHeight" />
</pulltorefresharp.android.views.ViewWrapper>

Below is my custom adapter:
///

    /// A basic adapter for the existing games list view.
    /// </summary>


    public class ExistingGameAdapter : ArrayAdapter<IListViewItem>{
    private readonly List<IListViewItem> _listViewItems;
    private readonly LayoutInflater _inflater;
    public ExistingGameAdapter(Context context, List<IListViewItem> items) : base(context, 0, items)
    {
        _inflater = LayoutInflater.From(context);
        _listViewItems = items;
    }

    /// <summary>
    /// Gets the number of different view types that are possible.
    /// </summary>
    public override int ViewTypeCount
    {
        get { return Enum.GetNames(typeof(ListViewType)).Length; }
    }

    /// <summary>
    /// Gets the value that corresponds to which view to present.
    /// </summary>
    /// <param name="position"></param>
    /// <returns></returns>
    public override int GetItemViewType(int position)
    {
        return (int) GetItem(position).GetViewType();
    }

    /// <summary>
    /// Removes an item at the given position.
    /// </summary>
    /// <param name="position">The position.</param>
    public void Remove(int position)
    {
        if (_listViewItems != null && _listViewItems.ElementAt(position) != null)
        {
            Remove(_listViewItems[position]);
        }
    }

    public void Remove(GameDO game)
    {
        var toRemove = _listViewItems.First(x => x.Game == game);
        Remove(toRemove);
    }

    /// <summary>
    /// Gets the game associated with the given position.
    /// </summary>
    /// <param name="position">The position.</param>
    /// <returns>The game that for this position</returns>
    /// <remarks>If the item is a <see cref="HeaderListViewItem"/>, then no game will be returned.</remarks>
    public GameDO GetGame(int position)
    {
        if (_listViewItems != null 
            && _listViewItems.ElementAt(position) != null 
            && !(_listViewItems[position] is HeaderListViewItem))
        {
            return _listViewItems[position].Game;
        }

        return null;
    }

    /// <summary>
    /// Gets whether or not the item at the given position is selectable.
    /// </summary>
    /// <param name="position">The position to test against.</param>
    /// <returns>Whether or not the item at the position is selectable.</returns>
    public override bool IsEnabled(int position)
    {
        return !(_listViewItems[position] is HeaderListViewItem);
    }

    public int GetNumberHeaders(int searchEnd)
    {
        var collection = _listViewItems.Where(lvi => _listViewItems.IndexOf(lvi) <= searchEnd ).Where (lvi => lvi is HeaderListViewItem);
        return collection == null ? 0 : collection.Count ();
    }

    /// <summary>
    /// Gets the associated view for the list view item.
    /// </summary>
    /// <param name="position">The position.</param>
    /// <param name="convertView">The view to convert, or keep the same.</param>
    /// <param name="parent">The container for the current convertView.</param>
    /// <returns>The final, potentionally converted.</returns>
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        return GetItem(position).GetView(_inflater, convertView);
    }
}

Below is my usage of the adapter:

_currentGames = (PullToRefresharp.Android.Widget.ListView)FindViewById (Resource.Id.gameselect_listview);
_currentGames.RefreshActivated += HandleRefreshActivated;

//listViewItems is populated...

_existingGamesAdapter = new ExistingGameAdapter (this, listViewItems);
_currentGames.Adapter = _existingGamesAdapter;
_currentGames.ChoiceMode = ChoiceMode.Single;

_currentGames.ItemClick += OnItemClick;
_currentGames.ItemLongClick += OnItemLongClick;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant