1

I am wondering if it's possible to differentiate between a swipe and a click on the BottomNavigationView in Xamarin.Android.

I've implemented:

    void NavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
    {
        if (_viewPager.CurrentItem == 0)
        {
            _fm1.Pop2Root();
        }

        _viewPager.SetCurrentItem(e.Item.Order, true);


    }

but there is no differentiation between a swipe and a click. I want to keep the current page loaded if the user swipes, but pop to the root if the user has clicked on the currently selected BottomNavigationView tab.

And here's what my Pop2Root method looks like (not that it really matters):

    public void Pop2Root()
    {
        _wv.LoadUrl("https://www.bitchute.com/");
    }

I just want a separate event for click versus swipe.

I'm not looking for anyone to do my work. I will post the full solution (as always) once I've figured it out. What I'm looking for is a yes or no answer whether or not it's possible; then I'll take care of the rest. I've implemented a click listener on the TabHost before, but that's a completely different UI element:

https://github.com/hexag0d/BitChute_Mobile_Android_a2/blob/2.7641/Activities/ClickListeners.cs

If you would like more context on the complete project, here's the MainActivity.cs then you can back into the rest:

https://github.com/hexag0d/BitChute_Mobile_Android_BottomNav/blob/master/MainActivity.cs

Thanks, in advance

hexagod
  • 449
  • 3
  • 15
  • Which control's swiping event you want to listen to? BottomNavigationView or Entire layout? Click listener is differ from the GestureListener, you can refer to this link.https://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures – Leon Apr 17 '19 at 06:23
  • @LeonLu-MSFT ok that's a useful resource. I will go that route as a last resort. But my swipes are already working fine, I'd rather implement an IOnClickListener to listen for clicks on the BottomNavigationView children. If I go the route posted above, it will require a ton of extra code. I do appreciate the response and I can probably do if I fail at OnClick implementation. – hexagod Apr 17 '19 at 23:46
  • Ok, can I post this comment as an answer? – Leon Apr 18 '19 at 07:49
  • void NavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e) { if (_menu != null && (_menu.ItemId == _viewPager.CurrentItem)) { _fm1.WebViewGoBack(); } _viewPager.SetCurrentItem(e.Item.Order, true); } private void ViewPager_PageSelected(object sender, ViewPager.PageSelectedEventArgs e) { _menu = _navigationView.Menu.GetItem(e.Position); _navigationView.SelectedItemId = _menu.ItemId; } – hexagod Apr 27 '19 at 01:52
  • ^^^ but WebViewGoBack() is not called. – hexagod Apr 27 '19 at 01:52
  • @LeonLu-MSFT no, don't mark it as the answer just yet. I'm getting pretty close. I know there is a specific event or object sender fired when a tab is pressed. If you download my project and compile it, when an IMenuItem is already selected and you press it again, android makes a sound. Also, I've successfully disconnected the tabs from swipe by commenting out a line inside void NavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e) .. while your link is useful.. that is a huge heap of code; I don't believe it is most efficient – hexagod Apr 27 '19 at 12:25

1 Answers1

0

The answer to this question is yes. The ViewPager_PageSelected method is invoked when user swipes. The NavigationView_NavigationItemSelected is invoked on a tab press. Interestingly, if ViewPager_PageSelected method is put before NavigationView_NavigationItemSelected method, ViewPager_PageSelected won't be invoked when the user presses the a tab until after this method is called:

_viewPager.SetCurrentItem(e.Item.Order, true);

After that happens, the ViewPager_PageSelected method is invoked and NavigationView_NavigationItemSelected gets invoked again. So I decided to do the order like this and set a custom int. This way, both methods are only called once per user interaction, and there is differentiation.

(Note events BottomNavigationView.NavigationItemSelectedEventArgs & ViewPager.PageSelectedEventArgs)

//put all of this inside your MainActivity.cs 
int _tabSelected;

void NavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
     if (_tabSelected == e.Item.Order)
     {
         switch (_viewPager.CurrentItem)
         {
             case 0:
                 _fm1.Pop2Root();
                 break;
             case 1:
                 _fm2.Pop2Root();
                 break;
             case 2:
                 _fm3.Pop2Root();
                 break;
             case 3:
                 _fm4.Pop2Root();
                 break;
             case 4:
                 _fm5.Pop2Root();
                 break;
          }
     }
     else
     {
         _viewPager.SetCurrentItem(e.Item.Order, true);
     }
}

private void ViewPager_PageSelected(object sender, ViewPager.PageSelectedEventArgs e)
{
   _menu = _navigationView.Menu.GetItem(e.Position);
   _navigationView.SelectedItemId = _menu.ItemId;

   _tabSelected = _viewPager.CurrentItem;
}
hexagod
  • 449
  • 3
  • 15