Search This Blog

Sunday 10 December 2017

Search View Example In xamarin android

MainActivity.cs:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using System.Linq;

namespace DemoG
{
    [Activity(Label = "DemoG", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        fragment_List newFragment;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
             SetContentView (Resource.Layout.Main);

            SearchView _search = FindViewById<SearchView>(Resource.Id.sv_name);
            _search.QueryTextChange += _search_QueryTextChange;

            newFragment = new fragment_List();
            var ft = FragmentManager.BeginTransaction();
            ft.Add(Resource.Id.fragment_container, newFragment);
            ft.Commit();
        }

        private void _search_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e)
        {
            var searchedNames = (from a in newFragment.objname where a.PersonName.ToString().Contains(e.NewText.ToString()) select a).ToList();
            newFragment.mAdapter.setLatestNames(searchedNames);
            newFragment.mAdapter.NotifyDataSetChanged();
        }
    }
}

========================================================================
fragment_List.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Support.V7.Widget;

namespace DemoG
{
    public class fragment_List : Fragment
    {
        public RecyclerView mRecyclerView;
        public RecyclerView.LayoutManager mLayoutManager;
        public PhotoAlbumAdapter mAdapter;
        public List<PersonModel> objname;
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate(Resource.Layout.recylerView_Fragment, container, false);
            mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView);
            objname = new List<PersonModel>();
            objname.Add(new PersonModel { PersonName = "chiru", PersonPhoneNumber = "123456789" });
            objname.Add(new PersonModel { PersonName = "Arun", PersonPhoneNumber = "123456789" });
            objname.Add(new PersonModel { PersonName = "Venkatrao", PersonPhoneNumber = "123456789" });
            objname.Add(new PersonModel { PersonName = "subbu", PersonPhoneNumber = "123456789" });
            objname.Add(new PersonModel { PersonName = "ramesh", PersonPhoneNumber = "123456789" });
            objname.Add(new PersonModel { PersonName = "123", PersonPhoneNumber = "123456789" });
            objname.Add(new PersonModel { PersonName = "old", PersonPhoneNumber = "123456789" });

            mAdapter = new PhotoAlbumAdapter(objname);
            // Get our RecyclerView layout:
            mLayoutManager = new LinearLayoutManager(this.Activity);
            mRecyclerView.SetLayoutManager(mLayoutManager);
            // Plug the adapter into the RecyclerView:
            mRecyclerView.SetAdapter(mAdapter);

            return view;
        }

        internal static fragment_List NewInstance()
        {
            return new fragment_List();
        }
    }
    public class PhotoViewHolder : RecyclerView.ViewHolder
    {
        public TextView Caption { get; private set; }
        public TextView _PhoneNumber { get; private set; }
        public PhotoViewHolder(View itemView, Action<int> listener): base(itemView)
        {
            Caption = itemView.FindViewById<TextView>(Resource.Id.tv_personName);
            _PhoneNumber = itemView.FindViewById<TextView>(Resource.Id.tv_personPhoneNumber);
            itemView.Click += (sender, e) => listener(base.LayoutPosition);
        }
    }

    public class PhotoAlbumAdapter : RecyclerView.Adapter
    {
        public event EventHandler<int> ItemClick;
        public List<PersonModel> mPhotoAlbum;

        public PhotoAlbumAdapter(List<PersonModel> photoAlbum)
        {
            mPhotoAlbum = photoAlbum;
        }

        public void setLatestNames(List<PersonModel> photoAlbum)
        {
            this.mPhotoAlbum = photoAlbum;
        }

        // Create a new photo CardView (invoked by the layout manager):
        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.details, parent, false);

            PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick);
            return vh;
        }

        // Fill in the contents of the photo card (invoked by the layout manager):
        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            PhotoViewHolder vh = holder as PhotoViewHolder;
            vh.Caption.Text = mPhotoAlbum[position].PersonName;
            vh._PhoneNumber.Text = mPhotoAlbum[position].PersonPhoneNumber;
        }

        // Return the number of photos available in the photo album:
        public override int ItemCount
        {
            get { return mPhotoAlbum.Count; }
        }

        // Raise an event when the item-click takes place:
        void OnClick(int position)
        {
            if (ItemClick != null)
                ItemClick(this, position);
        }
    }
}
=======================================================================
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <SearchView
    android:id="@+id/sv_name"
    android:queryHint="Search"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"/>
  <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  </FrameLayout>
</LinearLayout>
========================================================================
PersonModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace DemoG
{
   public class PersonModel
    {
        public string PersonName { set; get; }
        public string PersonPhoneNumber { set; get; }
    }
}
========================================================================
recylerView_Fragment.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:scrollbars="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>
========================================================================
details.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="name"
    android:layout_weight="1"
    android:id="@+id/tv_personName"
    android:textSize="18dp"/>
  <TextView
    android:layout_marginRight="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="name"
    android:id="@+id/tv_personPhoneNumber"
    android:textSize="18dp"/>
</LinearLayout>