3

I created an Activity that contains two tabs. One of these tabs is a Map (Google Maps API v2).

There is not a layout of Activty (no setContent() is called), since the activity screen is dynamically filled with a Fragment, depending on the selected tab.

Now, I need to add a TextView on the bottom of the Map.

I guess I can extend MapFragment and then work on the onCreateView method, in order to returning a different View that include both the Map and the TextView. I don't know exactly how I can do it. Any suggestion?

EDIT:

actionbar.addTab(actionbar.newTab()
                .setText("LIST")
                .setTabListener(new TabListener<ListFragment>(
                        this, "list", ListFragment.class)));


        Log.i("TabListener","chiamata TABLISTENER 2");
        actionbar.addTab(actionbar.newTab()
                .setText("MAP")
                .setTabListener(new TabListener<CustomMapFragment>(
                        this, "map", CustomMapFragment.class)));

The TabListener class:

public class TabListener<T extends Fragment> implements ActionBar.TabListener {
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    private final Bundle mArgs;
    private Fragment mFragment;

    public TabListener(Activity activity, String tag, Class<T> clz) {
        this(activity, tag, clz, null);
    }

    public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        mArgs = args;

        mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
            ft.detach(mFragment);
            ft.commit();

        }

    }

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            ft.attach(mFragment);
        }

    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        if (mFragment != null) {
            ft.detach(mFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }
}
GVillani82
  • 17,196
  • 30
  • 105
  • 172

1 Answers1

0

You'll need to place the MapFragment (or SupportMapFragment) inside a FrameLayout or RelativeLayout. Then you can add the TextView in the same Layout as a sibling of the MapFragment. Maybe the easiest way to accomplish this is to subclass MapFragment and override the onCreateView method.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    FrameLayout wrapper = new FrameLayout(...);
    TextView tv = new TextView(...);
    wrapper.addView(view);
    wrapper.addView(tv);
    return wrapper;
}
Matt Accola
  • 4,090
  • 4
  • 28
  • 37