Cуть в том что есть Drawer в нем 6 Items при открытии приложения мы сразу стоим на первом Item и когда переходим по его Tab то текст меняется. Но когда открываю 2ой Item то текст остается тот же что и в первом Tab, то есть он не меняется вообще. Побывал в activity_main.xml подключать 2 ViewPager-a тогда тексты налажуются на друг друга.
Это код activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
layout="@layout/toolbar">
</include>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="2dp"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="@android:color/white" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_height="match_parent"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>
кусочек кода MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.myTheme2);
super.onCreate(savedInstanceState);
setContentView(LAYOUT);
initTabs();
// Инициализируем Toolbar
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
if (toolbar !=null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
initializeNavigationDrawer(toolbar);
}
private void initTabs() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
// Инициализируем TabLayout
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
TabFragmentFish adapter = new TabFragmentFish(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public void onBackPressed() {
if (drawerMenu != null && drawerMenu.isDrawerOpen()) {
drawerMenu.closeDrawer();
} else {
super.onBackPressed();
}
}
//Инициализация Дравера
private void initializeNavigationDrawer(Toolbar toolbar) {
drawerMenu = new Drawer()
.withActivity(this)
.withToolbar(toolbar)
.withActionBarDrawerToggle(true)
.withHeader(R.layout.drawer_header)
//Добавление Итемов в шторку
.addDrawerItems(
new PrimaryDrawerItem()
.withName(R.id.aqua_fish)
.withIdentifier(1)
.withIcon(FontAwesome.Icon.faw_circle),
new PrimaryDrawerItem()
.withName(R.id.aqua_plant)
.withIdentifier(2)
.withIcon(FontAwesome.Icon.faw_leaf),
new PrimaryDrawerItem()
.withName(R.id.aqua_water)
.withIdentifier(3)
.withIcon(FontAwesome.Icon.faw_tint),
new PrimaryDrawerItem()
.withName(R.id.aqua_lighting)
.withIdentifier(4)
.withIcon(FontAwesome.Icon.faw_lightbulb_o),
new PrimaryDrawerItem()
.withName(R.id.aqua_co2)
.withIdentifier(5)
.withIcon(FontAwesome.Icon.faw_circle),
new PrimaryDrawerItem()
.withName(R.id.aqua_filtration)
.withIdentifier(6)
.withIcon(FontAwesome.Icon.faw_filter),
//--------------------------------------------- Secondary
new DividerDrawerItem(),
new SecondaryDrawerItem()
.withName(R.id.about)
.withIdentifier(7),
new SecondaryDrawerItem()
.withIdentifier(8)
.withName(R.id.exit))
//Реализуем клик по айтэмам
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
switch (drawerItem.getIdentifier()){
case 1:
TabFragmentFish adapter = new TabFragmentFish(getSupportFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
break;
case 2:
TabFragmentAdapterPlants adapterPlants = new TabFragmentAdapterPlants(getSupportFragmentManager());
viewPager.setAdapter(adapterPlants);
tabLayout.setupWithViewPager(viewPager);
break;
//Мини окошко "Об авторе с закрытием ОК"
case 7:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.AppCompatAlertDialogStyle);
builder
.setTitle("Об авторе")
.setMessage("Автор:Артем Мишуровский"+"\n\nEmail: artm.mishurovskiy@gmail.com")
.setCancelable(false)
.setNegativeButton("Ок", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
break;
//Выход из программы
case 8:
finish();
break;
}
}
})
.build();
Кусочки кода одного из фрагментов
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import house.naturalaquarium.R;
public class FragmentPlants extends Fragment {
private static final int LAYOUT = R.layout.fragments_plants2;
private View view;
public static FragmentPlants getInstance() {
Bundle args = new Bundle();
FragmentPlants fragmentPlants = new FragmentPlants();
fragmentPlants.setArguments(args);
return fragmentPlants;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(LAYOUT, container, false);
return view;
}
}
Кусочек ХМЛ того же фрагмента
<?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">
<TextView
android:text="#plants"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:layout_margin="50dp"
android:textSize="30sp"
android:textStyle="normal|bold"
android:textColor="@color/common_google_signin_btn_text_dark_focused" />
</LinearLayout>