NavigationComponents - навигационный ящик не перемещается - PullRequest
0 голосов
/ 10 октября 2019

Я только что реализовал навигацию с новым проектом, но мой ящик навигации не надувает каждый из фрагментов навигации, он показывает только первый, где бы я ни щелкнул вариант

public class DisplayScreen2 extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_screen2);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_perfil, R.id.nav_clima, R.id.nav_mapa,
                R.id.nav_config, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

Показан только фрагментвот этот

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }
}

public class HomeViewModel extends ViewModel {

    private MutableLiveData<String> mText;

    public HomeViewModel() {
        mText = new MutableLiveData<>();
        mText.setValue("This is home fragment");
    }

    public LiveData<String> getText() {
        return mText;
    }
}

Остальные фрагменты навигационного ящика не отображаются после того, как я нажму на них, я действительно не знаю, почему это происходит, и если мне нужно что-то настроить в домашнем фрагменте, чтобыбыть в состоянии войти в другие фрагменты

Спасибо

Ответы [ 2 ]

0 голосов
/ 10 октября 2019

решение добавляет это

navigationView.bringToFront();

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.nav_gallery:
                    Toast.makeText(DisplayScreen.this, "Gallery", Toast.LENGTH_SHORT).show();
                    break;
                }

                drawer.closeDrawers();
                return false;
            }


        });

для навигации, это должно быть реализовано с шаблоном навигации по умолчанию

0 голосов
/ 10 октября 2019

Просто убедитесь, что идентификаторы предметов в вашем drawer_menu.xml и идентификаторы фрагментов в navigation.xml совпадают. Вот пример:

hook_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:id="@+id/fragment1"
       android:title="Fragment 1"/>

   <item android:id="@+id/fragment2"
       android:title="Fragment 2"/>

   <item android:id="@+id/fragment3"
       android:title="Fragment 3"/>

</menu>

navigation.xml

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   app:startDestination="@id/fragment1">
   <fragment
       android:id="@+id/fragment1" //this id should match the corresponding item in drawer_menu.xml
       android:name="Fragment1"
       android:label="@string/fragment_1_title"
       tools:layout="@layout/fragment1" />
   <fragment
       android:id="@+id/fragment2" //this id should match the corresponding item in drawer_menu.xml
       android:name="Fragment2"
       android:label="@string/fragment_2_title"
       tools:layout="@layout/fragment2" />
   <fragment
       android:id="@+id/fragment3" //this id should match the corresponding item in drawer_menu.xml
       android:name="Fragment3"
       android:label="@string/fragment_3_title"
       tools:layout="@layout/fragment3" />
</navigation>
...