Searching through RecyclerView using Search Icon - Xamarin.Droid

时间:2017-07-12 08:03:31

标签: c# android xamarin xamarin.android

I am able to search through my recyclerView but then what happens is, the search item when clicked is being affected by the layout postion. For instance i have

  • Mac Book Pro

  • Nintendo

  • Nintendo plus

When i search Nintendo, Nintendo comes only appears in the 1st position. When i click on Nintendo, it gives me the details of Mac Book Pro instead. and when i search for something like Nint, it will display Nintendo and Nintendo plus which is cool. But then, since Nintendo plus is apparently in the 2nd position now, when i click on it, it gives me the details of Nintendo. Why is the search not affecting the layout position please?

Activity

public override bool OnCreateOptionsMenu(IMenu menu)
        {

            MenuInflater.Inflate(Resource.Menu.top_menus, menu);

            var item = menu.FindItem(Resource.Id.search);

            var searchview = (Android.Support.V7.Widget.SearchView)MenuItemCompat.GetActionView(item);

            searchview.QueryTextChange += (s, e) => adapter.Filter.InvokeFilter(e.NewText);


            searchview.QueryTextSubmit += (s, e) =>

            {
                adapter.NotifyDataSetChanged();
                Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show();
                e.Handled = true;

            };

            MenuItemCompat.SetOnActionExpandListener(item, new SearchViewExpandListener(adapter));



            return base.OnCreateOptionsMenu(menu);

        }

        private class SearchViewExpandListener : Java.Lang.Object, MenuItemCompat.IOnActionExpandListener
        {
            private readonly IFilterable _adapter;

            public SearchViewExpandListener(IFilterable adapter)
            {
                _adapter = adapter;
            }

            public bool OnMenuItemActionCollapse(IMenuItem item)
            {
                _adapter.Filter.InvokeFilter("");
                                return true;
            }

            public bool OnMenuItemActionExpand(IMenuItem item)
            {
                return true;
            }
        }

Adapter

public class GameFilter : Filter
        {
            public readonly GameAdapter _adapter;
            public GameFilter(GameAdapter adapter)
            {
                _adapter = adapter;
            }

            protected override FilterResults PerformFiltering(ICharSequence constraint)
            {
                var returnObj = new FilterResults();
                var results = new List<Games>();
                if (_adapter._originalData == null)
                    _adapter._originalData = _adapter.games;

                if (constraint == null) return returnObj;

                if (_adapter._originalData != null && _adapter._originalData.Any())
                {

                    results.AddRange(
                        _adapter._originalData.Where(
                            games => games.Name.ToLower().Contains(constraint.ToString())));
                }

                returnObj.Values = FromArray(results.Select(r => r.ToJavaObject()).ToArray());
                returnObj.Count = results.Count;

                constraint.Dispose();

                return returnObj;


            }

            protected override void PublishResults(ICharSequence constraint, FilterResults results)
            {
                    using (var values = results.Values)
                    _adapter.games = values.ToArray<Java.Lang.Object>()
                        .Select(r => r.ToNetObject <Games>()).ToList();

                _adapter.NotifyDataSetChanged();

                constraint.Dispose();
                results.Dispose();           
            }
        }

update

  void OnItemClick(object sender, int position)
            {
 position = rv.GetChildAdapterPosition((Android.Views.View)sender);
 Intent peaceHeroDetail = new Intent(this, typeof(DetailActivity));

 Detail.PutExtra(PeaceHeroDetailActivity.EXTRA_NAME, test[position].Name);

       StartActivity(Detail);


                }

1 个答案:

答案 0 :(得分:0)

在您的RecyclerView.Adapter代码中,您需要取消订阅并订阅点击事件,以便您点击某个项目。然后,要执行操作,您需要调用RecyclerView(而不是Adapter)方法GetChildAdapterPosition。

示例:

public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
    Item item = this.myItems[position];
    ((ViewHolderItem)holder).Name.Text = item.Name;
    ((ViewHolderItem)holder).Picture.SetImageBitmap(item.Picture);

    // Unsubscribe and subscribe the method, to avoid setting multiple times.
    ((ViewHolderItem)holder).Item.Click -= Item_Click;
    ((ViewHolderItem)holder).Item.Click += Item_Click;
}

private void Item_Click(object sender, EventArgs e)
{
    int position = this.recyclerView.GetChildAdapterPosition((View)sender);
    Item itemClicked = this.myItems[position];
    if(itemClicked.Name == "Nintendo"){
        Toast.MakeText(this.context, "Nintendo", ToastLength.Long).Show();
    }
}