Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

NavigationDrawer scenario, errors related to overriding of OnItemClickListener interface methods ---

Hello.
I'm trying this one just needed some help cleaning out the errors. Basically I am attempting a Navigation Drawer code in android app. using Xamarin. There is an example/tutorial available on Xamarin resources on Navigation Drawer. I tried to follow that one only. In that example are planets, in mine, just some names of items. That’s all.

The first code posting is a custom adapter, ‘DockItemAdapter’:::
`public class DockItemAdapter : RecyclerView.Adapter
{
private String[] mDataset;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void OnClick(View view, int position);
}

    public class ViewHolder : RecyclerView.ViewHolder{
        public readonly TextView textView;
        public ViewHolder(TextView v) : base(v){
            textView = v;
        }
    }

    public DockItemAdapter(string[] myDataSet, OnItemClickListener listener)
    {
        mDataset = myDataSet;
        mListener = listener;
    }

    public override RecyclerView.ViewHolder OnCreateViewHolder (ViewGroup parent, int viewType)
    {
        var vi = LayoutInflater.From (parent.Context);
        var v = vi.Inflate (Resource.Layout.drawer_list_item, parent, false);
        var tv = v.FindViewById<TextView> (Android.Resource.Id.Text1);
        return new ViewHolder (tv);
    }

    public override void OnBindViewHolder (RecyclerView.ViewHolder holderRaw, int position)
    {
        var holder = (ViewHolder)holderRaw;
        holder.textView.Text = mDataset [position];
        holder.textView.Click += (object sender, EventArgs args) => {
            mListener.OnClick((View) sender, position);
        };


    }

    public override int ItemCount {
        get {
            return mDataset.Length;
        }
    }
}`

The second posting is the ‘NavigationDrawerActivity’:::
`public class NavigationDrawerActivity : Activity, DockItemAdapter.OnItemClickListener
{
private DrawerLayout mDrawerLayout;
private RecyclerView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

    private string mDrawerTitle;
    private String[] mDockItemTitles;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.activity_navigation_drawer);


        mDrawerTitle = this.Title;
        mDockItemTitles = this.Resources.GetStringArray(Resource.Array.dockitems_array);
        mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        mDrawerList = FindViewById<RecyclerView>(Resource.Id.left_drawer);


        mDrawerLayout.SetDrawerShadow(Resource.Drawable.drawer_shadow, GravityCompat.Start);
        // improve performance by indicating the list if fixed size.
        mDrawerList.HasFixedSize = true;
        mDrawerList.SetLayoutManager(new LinearLayoutManager(this));

        mDrawerList.SetAdapter(new DockItemAdapter(mDockItemTitles, this));
        // enable ActionBar app icon to behave as action to toggle nav drawer
        this.ActionBar.SetDisplayHomeAsUpEnabled(true);
        this.ActionBar.SetHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon

        mDrawerToggle = new MyActionBarDrawerToggle(this, mDrawerLayout,
            Resource.Drawable.ic_drawer,
            Resource.String.drawer_open,
            Resource.String.drawer_close);

        mDrawerLayout.SetDrawerListener(mDrawerToggle);
        if (bundle == null) //first launch
            selectItem(0);
        // Create your application here
    }

    internal class MyActionBarDrawerToggle : ActionBarDrawerToggle
    {
        NavigationDrawerActivity owner;
        public MyActionBarDrawerToggle(NavigationDrawerActivity activity, DrawerLayout layout, int imgRes, int openRes, int closeRes)
            : base(activity, layout, imgRes, openRes, closeRes)
        {
            owner = activity;
        }

        public override void OnDrawerClosed(View drawerView)
        {
            owner.ActionBar.Title = owner.Title;
            owner.InvalidateOptionsMenu();
        }

        public override void OnDrawerOpened(View drawerView)
        {
            owner.ActionBar.Title = owner.mDrawerTitle;
            owner.InvalidateOptionsMenu();
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            // Inflate the menu; this adds items to the action bar if it is present.
            this.MenuInflater.Inflate(Resource.Menu.navigation_drawer, menu);
            return true;
        }

        /* Called whenever we call invalidateOptionsMenu() */
        public override bool OnPrepareOptionsMenu(IMenu menu)
        {
            // If the nav drawer is open, hide action items related to the content view
            bool drawerOpen = mDrawerLayout.IsDrawerOpen(mDrawerList);
            menu.FindItem(Resource.Id.action_websearch).SetVisible(!drawerOpen);
            return base.OnPrepareOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            // The action bar home/up action should open or close the drawer.
            // ActionBarDrawerToggle will take care of this.
            if (mDrawerToggle.OnOptionsItemSelected(item))
            {
                return true;
            }
            // Handle action buttons
            switch (item.ItemId)
            {
                case Resource.Id.action_websearch:
                    // create intent to perform web search for this planet
                    Intent intent = new Intent(Intent.ActionWebSearch);
                    intent.PutExtra(SearchManager.Query, this.ActionBar.Title);
                    // catch event that there's no activity to handle intent
                    if (intent.ResolveActivity(this.PackageManager) != null)
                    {
                        StartActivity(intent);
                    }
                    else
                    {
                        Toast.MakeText(this, Resource.String.app_not_available, ToastLength.Long).Show();
                    }
                    return true;
                default:
                    return base.OnOptionsItemSelected(item);
            }
        }

        public void OnClick(View view, int position)
        {
            selectItem(position);
        }

        private void selectItem(int position)
        {
            // update the main content by replacing fragments
            var fragment = PlanetFragment.NewInstance(position);

            var fragmentManager = this.FragmentManager;
            var ft = fragmentManager.BeginTransaction();
            ft.Replace(Resource.Id.content_frame, fragment);
            ft.Commit();

            // update selected item title, then close the drawer
            Title = mDockItemTitles[position];
            mDrawerLayout.CloseDrawer(mDrawerList);
        }

        protected override void OnTitleChanged(Java.Lang.ICharSequence title, Android.Graphics.Color color)
        {
            //base.OnTitleChanged (title, color);
            this.ActionBar.Title = title.ToString();
        }

        /**
         * When using the ActionBarDrawerToggle, you must call it during
         * onPostCreate() and onConfigurationChanged()...
         */

        protected override void OnPostCreate(Bundle bundle)
        {
            base.OnPostCreate(bundle);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            mDrawerToggle.SyncState();
        }

        public override void OnConfigurationChanged(Configuration newConfig)
        {
            base.OnConfigurationChanged(newConfig);
            // Pass any configuration change to the drawer toggls
            mDrawerToggle.OnConfigurationChanged(newConfig);
        }

        internal class DockItemFragment : Fragment
        {
            public const string ARG_ITEM_NUMBER = "item_number";

            public DockItemFragment()
            {
                // Empty constructor required for fragment subclasses
            }

            public static Fragment NewInstance(int position)
            {
                Fragment fragment = new DockItemFragment();
                Bundle args = new Bundle();
                args.PutInt(DockItemFragment.ARG_ITEM_NUMBER, position);
                fragment.Arguments = args;
                return fragment;
            }

            public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
                                               Bundle savedInstanceState)
            {
                View rootView = inflater.Inflate(Resource.Layout.fragment_planet, container, false);
                var i = this.Arguments.GetInt(ARG_ITEM_NUMBER);
                var planet = this.Resources.GetStringArray(Resource.Array.dockitems_array)[i];
                var imgId = this.Resources.GetIdentifier(planet.ToLower(),
                                "drawable", this.Activity.PackageName);
                var iv = rootView.FindViewById<ImageView>(Resource.Id.image);
                iv.SetImageResource(imgId);
                this.Activity.Title = planet;
                return rootView;
            }
        }
    }
}`

The Errors:::
`Error CS0115: 'TestALLApp.NavigationDrawerActivity.MyActionBarDrawerToggle.OnCreateOptionsMenu(Android.Views.IMenu)': no suitable method found to override (CS0115) (TestALLApp)

Error CS0115: 'TestALLApp.NavigationDrawerActivity.MyActionBarDrawerToggle.OnPrepareOptionsMenu(Android.Views.IMenu)': no suitable method found to override (CS0115) (TestALLApp)

Error CS0115: 'TestALLApp.NavigationDrawerActivity.MyActionBarDrawerToggle.OnTitleChanged(Java.Lang.ICharSequence, Android.Graphics.Color)': no suitable method found to override (CS0115) (TestALLApp)

Error CS0115: 'TestALLApp.NavigationDrawerActivity.MyActionBarDrawerToggle.OnPostCreate(Android.OS.Bundle)': no suitable method found to override (CS0115) (TestALLApp)

Error CS0535: 'TestALLApp.NavigationDrawerActivity' does not implement interface member 'TestALLApp.DockItemAdapter.OnItemClickListener.OnClick(Android.Views.View, int)' (CS0535) (TestALLApp)
`

From the sample code, I tried to map my parts, refactoring little by little. Maybe I missed out some, or made some error while doing this, or a problem in basic understanding/laying out of the code concepts themselves. Could you please tell me where the error is.
I see most of the errors are either ‘no suitable method found to override’ or ‘does not implement interface member’. Which interface, the ‘OnItemClickListener’ of DockItemAdapter?
How do I do this? Please assist me a little how to do it appropriately, where and how to put the right code.

Thanks.

Sign In or Register to comment.