0

I wrote an app with a bottom navigation und five tabs. It works without any issues. On my second tab I have a ListView and when the user clicks on it, it should open another Fragment/Layout.

1. to create the tabs' views, I created a MenuBase Fragment:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            string title = (string) Arguments.Get("title");
            View view = null;

            switch (title)
            {
                case "T1":
                    break;
                case "Tutorial":
                    FragActivity.Tutorial tutorial = new FragActivity.Tutorial();
                    view = tutorial.createTutorialAction(inflater, container, savedInstanceState);
                    break;
                case "T3":
                    break;
                case "T4":
                    break;
                case "T5":
                    break;
            }

            return view;
        }

2. Here is my CreateTutorial function:

public View createTutorialAction(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            List<TutorialItem> tutorialItems = new List<TutorialItem>();

            View view = inflater.Inflate(Resource.Layout.Tutorial, container, false);
            ListView listView = view.FindViewById<ListView>(Resource.Id.tutorialListView);

            int resID = (int)typeof(Resource.Drawable).GetField("eventPic").GetValue(null);
            for (int i = 0; i < 10; i++)
            {
                tutorialItems.Add(new TutorialItem()
                {
                    ImgName = resID,
                    Text = "Tutorial Nr " + i,
                });
            }

            listView.Adapter = new TutorialAdapter(this, tutorialItems, container, inflater);

            return view;
        }

3. To create ListView:

public class TutorialAdapter : BaseAdapter<TutorialItem>
    {
        List<TutorialItem> items;
        Activity context;
        ViewGroup container;
        LayoutInflater inflater;
        View view;

        public TutorialAdapter(Activity context, List<TutorialItem> items, ViewGroup container, LayoutInflater inflater) : base()
        {
            this.context = context;
            this.items = items;
            this.container = container;
            this.inflater = inflater;
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public override TutorialItem this[int position]
        {
            get { return items[position]; }
        }
        public override int Count
        {
            get { return items.Count; }
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];
            view = convertView;

            if (view == null) // no view to re-use, create new
                view = this.inflater.Inflate(Resource.Layout.Tutorial_Profile, this.container, false);
            view.FindViewById<ImageView>(Resource.Id.imgTutorialDescribe).SetImageResource(item.ImgName);
            view.FindViewById<TextView>(Resource.Id.txtTutorialDescribe).Text = item.Text;
LinearLayout profile = view.FindViewById<LinearLayout>(Resource.Id.tutorialProfileLayout);
            profile.Tag = item.Text;
            profile.Click += OnTutorialProfileClicked;
            return view;
        }
     }

Everything works till now... But I don't know what I should do to go to a new Layout/Fragment with Bottomnavigation, or if it's okay to load it from a BaseAdapter:

public void OnTutorialProfileClicked(object sender, EventArgs e)
        {
            //Load another Fragment
        }

If something is missing... Tell me, I can edit my post.

Milad
  • 377
  • 1
  • 3
  • 15
  • You can try to set the `listView.ItemClick += OnListItemClick;`, then create a intent, ` Intent intent = new Intent(this, typeof(xxxActivity)); StartActivity(intent);` – Leon Oct 30 '19 at 13:35
  • @LeonLu-MSFT it's not possible in BaseAdapter and with this method the bottom navigation will disappear. – Milad Oct 30 '19 at 14:17
  • If you want to keep the `BottomNavigationView`, when you click the Item in the listview, you should change the Fragment content(listview content) to show item content, when you achieve a back result, you still need to change the Fragment content – Leon Nov 01 '19 at 16:00
  • Use this code to open a new fragment:` FragmentManager.BeginTransaction().Replace(this.Id, new NewFragment(selectValue),null).AddToBackStack(null).Commit();` in your listview item click method. – Leon Nov 01 '19 at 16:02
  • @LeonLu-MSFT Thanks, but the FragmentManager returns always null. I get this error: System.NullReferenceException: 'Object reference not set to an instance of an object.' – Milad Nov 04 '19 at 09:08

1 Answers1

1

I wrote a demo about it. First of all, here is a running GIF.when I click the item in listview, it will open a new fragment to show the text of item.

enter image description here

Here is my demo(If you have some errors about cannot found the Resource.id.xxxx when you build this error, please ignore it). https://github.com/851265601/Fragment-and-Viewpager

I used a ViewPager and Fragement to achieve the result.

But we should change another new achievement for this adapter. if we want to have new Fragment, We should remove the old fragment, then give the new fragment and NotifyDataSetChanged for this fragment.

Here is a completely explanation about it. https://stackoverflow.com/a/9127423/10627299

Leon
  • 8,404
  • 2
  • 9
  • 52
  • Thanks alot... It works perfectly. But when I try to adjust the code for my project. I still have some problems with fragments. In this line: view = tutorial.LayoutInflater.Inflate(Resource.Layout.Tutorial_Profile, null); I get this error: Java.Lang.IllegalStateException: 'onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.' – Milad Nov 05 '19 at 14:32
  • Thanks again Leon, the problem was my app architecture. It works now! – Milad Nov 06 '19 at 10:42