简体   繁体   中英

Fragment sits in loading and doesn't load when calling it from activity

I'm pretty new to Android, and I'm learning to use fragments. I created a fragment that shows a Textview when a specific tab on my BottomNavigation view is selected. I open the fragment like this:

public void switchToWorkoutFragment() {
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.content, new 
            ListFragment()).commit();
    }

Then I call this function when the "Workout" button is selected like such:

private BottomNavigationView.OnNavigationItemSelectedListener 
mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText("Stats Fragment");
                    return true;
                case R.id.navigation_dashboard:
                    mTextMessage.setText("Workout Fragment");
                    switchToWorkoutFragment();
                    return true;
                case R.id.navigation_notifications:
                    mTextMessage.setText("Goals Fragment");
                    return true;
            }
            return false;
        }

    };

When I press the Workout button, the fragment just doesn't want to load. It sits with the spinning icon indefinitely and doesn't load anything. I'm not sure why it would do that, since there's not that much stuff there to load (like I said, it's just a textview)

Did you set the listener to the view? Like:

(BottomNavigationView) nav = findViewById(R.id.bottom_navigation_panel);
nav.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

The problem is where you pass the new ListFragment() in the switchToWorkoutFragment method. A ListFragment contains a ListView to display the items, and this ListView needs an Adapter to pull data which is the one to display. Since you are passing a completely new ListFragment without setting an Adapter and passing data to display, the Fragment don't have anything to show. So you can do something like this:

ListFragment fragment = new ListFragment();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
adapter.addAll(Arrays.asList(new String[]{"Item one", "Item two", "Item 3"}));
fragment.setListAdapter(adapter);

getSupportFragmentManager()
        .beginTransaction()
        .add(R.id.fragmentContainer, fragment)
        .commit();

Note that setting the Adapter is enough to hide the spinning icon and correctly display the ListFragment (with or without data)

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