简体   繁体   中英

Android BottomNavigation and Tabs, view disappear after first click

I'm trying to use both BottomNavigationView and Tabs. Tabs change for each navigation item. Here's my MainActivity code:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navView = findViewById(R.id.nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_a, R.id.navigation_b, R.id.navigation_c, R.id.navigation_d)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
    }

This is my navigation_b class:

public class NavigationBFragment extends Fragment {
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_ui_b, container, false);

        MyTabAdapter adapter = new MyTabAdapter( getActivity().getSupportFragmentManager() );
        adapter.add( new TabA(), "Tab A");
        adapter.add( new TabB(), "Tab B");

        ViewPager viewPager = root.findViewById(R.id.partida_view_pager);
        viewPager.setAdapter(adapter);

        TabLayout tabLayout = root.findViewById(R.id.partida_tab_layout);
        tabLayout.setupWithViewPager(viewPager);

        return root;
    }
}

My navigation_b layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:background="@color/colorLightGray">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/partida_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabIndicatorHeight="4dp"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/partida_view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/default_padding" />
</LinearLayout>

When I first click "Navigation B", the tabs works as well, but when I click (for example) "Navigation A" and then "Navigation B", nothing is shown in PageView. What can I do?

I just solved the problem. I changed the class MyTabsAdapter , whilom it extended FragmentPagerAdapter and now it extends FragmentStatePagerAdapter . Work as well.

Thanks for this github forum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM