android


BottomNavigationView set optionselected with ViewPager Xamarin


I am building a App Where I use A BottomNavigationView and display the fragments with a ViewPager.
When I push or touch the buttoms of the Bottom Navigation, I dont have problems, the fragment displays correctly in the viewpager.
The problem is that I can slide just the viewPager and when I do it, the bottom navigation does not change to the correct option.
I saw that I need to add ViewPager.IOnPageChangeListener and create three methods for manage the view pager, but I dont now How to set the BottomNavigation item selected when the user slide the viewpager
For example, I touch the buttom 4 of the BottomNavigationBar and I saw Automatically the fragment 4 in the view pager(where I display the text "Fragment4"),,, and then I slide the ViewPager until the Fragment1, but the BottomNavigation is the option4 still selected,, it does not change when I slide the viewpager u.u
I hope you can help me, thanks
public class MainActivity : AppCompatActivity, ViewPager.IOnPageChangeListener
{
private DrawerLayout drawerLayout;
ViewPager viewPager;
BottomNavigationView bottomNavigation;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
bottomNavigation.NavigationItemSelected += Navigation_NavigationItemSelected;
SupportToolbar toolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
SupportActionbar actionBar = SupportActionBar;
actionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
actionBar.SetDisplayHomeAsUpEnabled(true);
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawerLayout);
NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
if (navigationView != null)
{
setUpDrawerContent(navigationView);
}
//TabLayout tabs = FindViewById<TabLayout>(Resource.Id.tabs);
viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
setUpViewPager(viewPager);
//tabs.SetupWithViewPager(viewPager);
}
private void Navigation_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
switch (e.Item.ItemId)
{
case Resource.Id.navigation_myState:
viewPager.SetCurrentItem(0, false);
break;
case Resource.Id.navigation_study:
viewPager.SetCurrentItem(1, false);
break;
case Resource.Id.navigation_flashcards:
viewPager.SetCurrentItem(2, false);
break;
case Resource.Id.navigation_test:
viewPager.SetCurrentItem(3, false); ;
break;
}
}
private void setUpViewPager(ViewPager viewPager)
{
ViewPagerAdapter adapter = new ViewPagerAdapter(SupportFragmentManager);
adapter.AddFragment(new Fragment1(), "Fragment1");
adapter.AddFragment(new Fragment2(), "Fragment2");
adapter.AddFragment(new Fragment3(), "Fragment3");
adapter.AddFragment(new Fragment4(), "Fragment4");
viewPager.Adapter = adapter;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
drawerLayout.OpenDrawer((int)GravityFlags.Left);
return true;
default:
return base.OnOptionsItemSelected(item);
}
}
private void setUpDrawerContent(NavigationView navigationView)
{
navigationView.NavigationItemSelected += (object sender, NavigationView.NavigationItemSelectedEventArgs e) =>
{
e.MenuItem.SetChecked(true);
drawerLayout.CloseDrawers();
};
}
public class ViewPagerAdapter : FragmentPagerAdapter
{
public List<SupportFragment> Fragments { get; set; }
public List<string> FragmentsNames { get; set; }
public ViewPagerAdapter(SupportFragmentManager FragManager) : base (FragManager)
{
Fragments = new List<SupportFragment>();
FragmentsNames = new List<string>();
}
public void AddFragment(SupportFragment fragment, string name)
{
Fragments.Add(fragment);
FragmentsNames.Add(name);
}
public override int Count
{
get
{
return Fragments.Count;
}
}
public override SupportFragment GetItem(int position)
{
return Fragments[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(FragmentsNames[position]);
}
}
public void OnPageScrollStateChanged(int state)
{
throw new NotImplementedException();
}
public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
{
throw new NotImplementedException();
}
public void OnPageSelected(int position)
{
//I think that here I need to put something like bottomNavigatin.setItemselecte(position) or (bottomNavigation.something(Resource.Id.navigation_flashcards)
}
}

Related Links

Find invalid push tokens for ios and android
Switching Camera in android
How to solve the issue faced in Automated billing system
How to apply an Affine Transform to an Android ImageView?
Android: ConstraintLayout has different behavior on SDK 4.1.x
Sample Android Code for moving an image behind a selection box
Android 4.4.4 can not connect to Nordic semi SDK 13 devices
Unable to set drawable as background on a textview in api 19
Android Make one request with post method, but it save 2 data in database
Android command is depreciated after updating SDK
The resolution doesn't fit the screen
Camera2 API Real-Time OCR
Android ListView generated from SQLite database not displaying on phone screen (but displaying on emulator)
How to filter (not search) Listview BaseAdapter
The Activity restart after saving data in Firebase
How do I use ConstraintSet in Android to stack TextViews horizontally?

Categories

HOME
batch-file
developer-tools
jax-rs
mjml
flex
oracle-apex-5
jsf-2.2
spring-data-redis
edge
rom
bing-maps
octave
watson-conversation
resultset
filechannel
3nf
qsqlquery
bitnami
package-managers
linear-algebra
azure-iot-hub
android-7.0-nougat
forgot-password
soci
enterprise-miner
sencha-touch
lilypond
symbols
event-store
zxing
google-cloud-print
maze
stackexchange
licensing
appirater
jasmine-node
firebase-analytics
android-things
gp
carrierwave
cart
catch-unit-test
clickjacking
sendbird
vue-chartjs
processwire
protege4
magento-1.9.2.4
google-knowledge-graph
password-protection
google-data-api
urlsession
signals-slots
dbnull
placeholder
servermanager
fwrite
opennms
facebook-pixel
configurationmanager
carrot2
er-diagram
janus
rmongo
nikeplus-api
cifilter
xdoclet
deep
bizagi
gcloud-ruby
dbamp
upx
nupic
realsense
contentcontrol
design-principles
crouton-os
linklabel
java-money
jdi
roadkill-wiki
rpart
sha512
aspectj-maven-plugin
matlab-deployment
osc
document-repository
tweenlite
trdion2011
cuba
drupal-nodes
gwt-openlayers
c++builder-xe5
cnf
merb
joomla-template
server-administration
curb
cuda-gdb
objectquery
cgimage
event-receiver
wysiwym
perl5.12
change-management
mathematical-typesetting

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App