Start Fragment class of another Fragment class

Asked

Viewed 231 times

1

I have a class GuiaDaCidade.

public class GuiaDaCidade extends Fragment {

    View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.guia_da_cidade, null);

        View botaoPasseios = (View) rootView.findViewById(R.id.btpasseios);
        View botaoAgenda = (View) rootView.findViewById(R.id.btagenda);

        botaoPasseios.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),Passeios.class));    ///O que fazer aqui?
            }
        });

        botaoAgenda.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Uri uri = Uri.parse("http://www.agendanatal.com.br");
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);

            }
        });

        return rootView;
    }

And the Class Passeios:

public class Passeios extends Fragment {

    private ArrayList<HashMap<String, String>> list;
    private ExpandableListAdapter listAdapter;
    private ExpandableListView expListView;
    private List<String> listDataHeader;
    private HashMap<String, List<String>> listDataChild;
    private WebView webView;
    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.guia_passeios, null);

        expListView = (ExpandableListView) rootView.findViewById(R.id.listaexp);

        prepareListData();

        listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

        expListView.setAdapter(listAdapter);
        expandeAll();
        expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {

                mudaTela(groupPosition, childPosition);

                return false;
            }
        });

        return rootView;
    }

How do I start the class Passeios from the class GuiaDaCidade? In the way onclickListener class GuiaDaCidade already has +/- the theoretical idea of what I want, but does not work.

  • Maroni, with Guided Touring Guidethe wants to trade one for the other.. or he wants to open a new Activity that contains the new Tours Tour? You can clarify better?

  • open a new Activity that contains Tours.

  • Tours has this layout rootView = Inflater.inflate(R.layout.guia_tours, null); I would like to show you, that’s all I would like to show you .-.

1 answer

1

@Maroni the problem exists because you called the method Startactivity and instead of an Activity you passed your Fragment Ride. An Fragment can only exist within an Activity and this can only be added to an Activity dynamically(I will focus my response in this way) or static.

  • Forma Dinamica: To add Fragment dynamically, or better while the application is running, you need to have in your Activity layout a Framelayout with an id that will be a container for you to place the Fragment.

example : activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<!--Voce pode ter a toolbar aqui-->
<!--O framelayout para conter o fragment-->
     <FrameLayout 
           android:id="@+id/fragment_container"
           android:layout_width="match_parent"
           android:layout_height="match_parent" />
</LinearLayout>

With the layout as follows you can add or replace the Fragments of this framelayout using the class Fragmentmanager which contain methods necessary for this.

Then you would have an Activity named Activityguia for example and in onCreate you would setContentView to an XML that nor what I gave in the example with a Framelayout and add the Guide Ragment Walk the Activity with a line similar to this :

getSupportFragmentManager(). beginTransaction(). add(R.id.container,new Guidedcity()). commit();

To achieve the goal you put here in the question you must create another activity named Activitypasseio(eg) and follow the same process as adding the Walk Fragment to this Activity.

Once you have each Fragment in your Activity, you can now use the startActivity method by passing an Intent where the 2 parameter is the Activity ride class. Your new call would look something like this:

Intent Intent = new Intent(getActivity,Activitypasseio.class);

startActivity(Intent);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.