Answer the question
In order to leave comments, you need to log in
Why are Fragments not showing with TabLayout?
The bottom line is that there is a Drawer in it 6 Items when you open the application, we immediately stand on the first Item and when we move on its Tab, the text changes. But when I open the 2nd Item, the text remains the same as in the first Tab, that is, it does not change at all. I visited activity_main.xml to connect 2 ViewPager-a then the texts will overlap each other.
This is activity_main.xml code
<?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>
@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: [email protected]")
.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>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question