Barış Barış - 2 months ago 29
Android Question

Viewpager with Toolbar

I have a bar which includes menu. Im using viewbar and i need to show my bar every single page.

So thats why i created my bar in MainActivity and viewpager_layout.xml

If i use same fragment layout every page its working well but when i try change layout its suck. Here my code.

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_layout);
ViewPager vpPager = (ViewPager) findViewById(R.id.viewPager);
adapterViewPager = new viewPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
vpPager.setCurrentItem(0);

getBottomBar();

// Attach the page change listener inside the activity
vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

// This method will be invoked when a new page becomes selected.
@Override
public void onPageSelected(int position) {

Toast.makeText(MainActivity.this,
"Seçilen sayfa: " + position, Toast.LENGTH_SHORT).show();

if (position == 0)
{
bottomNavigationBar.selectTab(0);
}
if (position == 1)
{
bottomNavigationBar.selectTab(1);
}
}

// This method will be invoked when the current page is scrolled
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// Code goes here
}

// Called when the scroll state changes:
// SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING, SCROLL_STATE_SETTLING
@Override
public void onPageScrollStateChanged(int state) {
// Code goes here
}
});
}
BottomNavigationBar bottomNavigationBar;
private void getBottomBar() {
bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);

bottomNavigationBar
.addItem(new BottomNavigationItem(R.drawable.home, "Anasayfa"))
.addItem(new BottomNavigationItem(R.drawable.group, "Gruplar"))
.addItem(new BottomNavigationItem(R.drawable.counter, "Votloc"))
.addItem(new BottomNavigationItem(R.drawable.date, "Profil"))
.addItem(new BottomNavigationItem(R.drawable.password, "Daha Fazla"))
.initialise();
bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener(){
@Override
public void onTabSelected(int position) {
}
@Override
public void onTabUnselected(int position) {
}
@Override
public void onTabReselected(int position) {
}
});
bottomNavigationBar
.setMode(BottomNavigationBar.MODE_FIXED);
bottomNavigationBar
.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
bottomNavigationBar
.setActiveColor(R.color.colorPrimary)
.setInActiveColor("#FFFFFF")
.setBarBackgroundColor("#FF1717");


}


viewpager_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:layout_gravity="bottom"
android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</LinearLayout>


What should I do for use my bar every single page.
Am I have to create bar in every fragments ?

Answer

I realized it was because I used <include> to add the ListView to my fragment from another layout resource file.

After that I created a new layout file and I just included my ListView directly into the new layout file. Then I used this new layout file for myMainFragment.java file and finally my ViewPager works well.

Comments