0

I have implemented a project with 1 activity, actionBar with tab Navigation mode (5 tabs, each one is a fragment).

Each fragment have its own menu options (in action bar).

What I need is that when user clicks one of that options, change the layout of that current fragment. I know where to put the code, in this case it will be something like this...

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.fragment1, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

        case R.id.menu_listMode:
            //Here I need to put the code to change layou of this current fragment...
            return true;
        default:
            break;
        }
        return true;
    }

To be more specific... An example would be: You have a fragment with a calendar view, and when you click the option in the action bar, that view changes to a listViewFragment. (The layout changes, but the tab it's the same).

I have used this sample project: https://github.com/sgolivernet/curso-android-src/tree/master/android-actionbar-tabs

Borja
  • 1,269
  • 1
  • 17
  • 30

2 Answers2

0

If you have ref to your activity, use the activity to find your Fragment via FragmentManager. Then ask your Fragment to inflate another layout or add the listview dynamically.

If you do not have ref to your activity, use the callback interface to notify your activity.

Anson Yao
  • 1,544
  • 17
  • 27
-1

CallBack is the best solution for your problem. Link 1 Link 2 may help you for this.

Community
  • 1
  • 1
Hanan
  • 444
  • 4
  • 16