A
A
Andrey Goroshko2018-08-31 12:52:56
Android
Andrey Goroshko, 2018-08-31 12:52:56

Why does not show the desired fragment when selecting the menu item nav drawer android?

I have two fragments in the program, each of which is responsible for one type of message - incoming or outgoing. There is an adapter that implements the selection of these fragments. And I want to make it so that when I select a menu item, I see a certain fragment. Here is my adapter:

public class PagerAdapter extends FragmentStatePagerAdapter {
    private int mNoOfTabs;

    public PagerAdapter(FragmentManager fm) {
        super(fm);
        //this.mNoOfTabs = NumberOfTabs;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position)
        {
            case 0:
                return new Received();
            case 1:
                return new Sent();
               /* default:
            return new Received();*/
        }

        return new Sent();
    }

    @Override
    public int getCount() {
        return mNoOfTabs;
    }
}

Here is a selection of menu items:
public boolean onNavigationItemSelected(@NonNull MenuItem item) {


        int id = item.getItemId();
        fragmentManager = getSupportFragmentManager();
       /* mAdapter = new PagerAdapter(getSupportFragmentManager());
        mPager = findViewById(R.id.frame);*/
        ft = fragmentManager.beginTransaction();
        if (id == R.id.received) {
            ft.replace(R.id.frame, new Received());
        } else if (id == R.id.sended) {
            ft.replace(R.id.frame, new Sent());
        }


        ft.commit();


        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

and here is the adapter in the menu:
mAdapter = new PagerAdapter(getSupportFragmentManager());
mPager = findViewById(R.id.frame);

but I have only one fragment - Incoming messages, but outgoing messages I do not show. Maybe I didn't add something somewhere, or somewhere else there is an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-08-31
@klim76

if (id == R.id.received) {
ft.replace(R.id.frame, new Received());
} else if (id == R.id.sent) {
ft.replace(R.id.frame, new Sent());
}
do you need to refer to your adapter here and specify which item to display to it? not that's all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question