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

How can I have both a return and a done keys in an EditText's softkeyboard?
convert touched value to points based on matrix
Creating internal file in android
Download packages of assets in phonegap
Timing issues with the GPS
Is this is OK to use the ListView for inline editing?
Partial wake lock is not working
Stuck while installing sample helloAndroid application in emulator
How to make words in checkbox text to be link
About connecting web database with Android application
can we make the whole application zoomable in android ?
Getting issue to set the color on list view item
Android - Is it possible to have dynamically moving clickable objects in a view
Make ball bounce and eventually come to rest
fbShare in android
How to play an encrypted video file in Android

Categories

HOME
windows
url-redirection
facebook-oauth
android-studio-2.2
formal-languages
swt
themes
package
echo
nuget-package
google-cloud-datalab
odoo-8
discrete-mathematics
fosrestbundle
startup
ipmitool
screen-readers
capistrano3
coin3d
wolframalpha
jmp
poedit
springfox
roundup
aws-codecommit
scatter3d
code-snippets
sammy.js
reason
playframework-2.1
verifiable-c
c9.io
setup.py
apache-stanbol
ogc
xv6
linter
yii2-api
launchctl
activeperl
insight
video-conferencing
modelsim
quicktime
flac
juice-ui
cvxpy
preprocessor
tinkerpop3
spring-cloud-feign
jquery-jscrollpane
android-dateutils
vorpal.js
google-query-language
jnlua
vhosts
dynamics-nav-2016
skspritenode
uvc
fail2ban
zxspectrum
shopizer
universal-image-loader
jama
raygun
lambda-architecture
draw2d-js
conditionaltagsupport
getopt
kendo-dataviz
kogrid
flex++
route-provider
composite-component
insert-id
tree-balancing
sentestingkit
screen-size
xcelsius
update-statement
collectionviewsource
objective-j
interprocess
tui
cuda.net
fault-tolerance
flvplayback
product-management

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