Start class Fragment of another Class Fragment

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 Passeios class from the GuiaDaCidade class? In the onclickListener method of class GuiaDaCidade already has +/- the theoretical idea of what I want, but it does not work.

    
asked by anonymous 24.12.2015 / 23:10

1 answer

1

@Maroni the problem exists because you called the StartActivity method and instead of an Activity you have passed your Fragment Walk. A fragment can only exist within an Activity and can only be added to an Activity dynamically ( I'll focus my answer in this way ) or static.

  • Dynamic Form : To add the fragment dynamically, or better while the application is running, you need to have a FrameLayout with an id that will be a container for you to put in 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 the following way you can add remove or replace the fragments of this framelayout using the class FragmentManager which contains necessary methods for this.

In this way you would have an activity of name ActivityGuia for example and in onCreate you would setContentView to an XML like the one I gave in the example with a FrameLayout and add the fragment Guide to the activity with a line similar to this:

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

In order to achieve the objective you put here in the question you should create another activity of name ActivityPassion (for example) and follow the same process as adding the fragment to this activity.

Once you have each fragment in your activity, now yes you can use the startActivity method passing an intent where the 2 parameter is the class of the Activity walk. Your new call would look something like this:

Intent int = new intent (getActivity, ActivityPassage.class);

startActivity (intent);

    
25.12.2015 / 22:51