call a Fragment from a ListFragment on android

1

I've been looking everywhere and I can not find such an answer, what happens is that I have a ListFragment and that ListFragment I have a method setOnItemClickListener , everything works very well, only I need to call a fragment from setOnItemClickListener of ListFragment , for now I only have about TOAST to differentiate between clicks

Java code of the ListFragment

public class Main2Activity extends ListFragment {

    String[] players={"uno","dos","tres"};
    int[] images={R.drawable.enrique,R.drawable.kiko,R.drawable.astiazaran};


    ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String,String>>();
    SimpleAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        HashMap<String, String> map=new HashMap<String, String>();

        for(int i=0;i<players.length;i++)
        {
            map=new HashMap<String, String>();
            map.put("Player", players[i]);
            map.put("Image", Integer.toString(images[i]));
            data.add(map);
        }


        //KEYS IN MAP
        String[] from={"Player","Image"};
        //IDS OF VIEWS
        int[] to={R.id.nameTxt,R.id.imageView1};
        //ADAPTER
        adapter=new SimpleAdapter(getActivity(), data,  R.layout.model, from, to);
        setListAdapter(adapter);


        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();

        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int pos,
                                    long id) {
                // TODO Auto-generated method stub


                if (pos == 0){


                   Toast.makeText(getActivity().getApplicationContext(), String.valueOf("seleccionaste a 0"), Toast.LENGTH_SHORT).show();

                }

                if (pos == 1){

                    Toast.makeText(getActivity().getApplicationContext(), String.valueOf("Seleccionaste a 1"), Toast.LENGTH_SHORT).show();

                }
                if (pos == 2){

                    Toast.makeText(getActivity().getApplicationContext(), String.valueOf("seleccionaste 2"), Toast.LENGTH_SHORT).show();

                }

            }
        });



    }

    }

EVERYTHING WORKS very well only that I want to call another fragment

Java code of the fragment that I want to call instead of putting the toast in the if of the listfragment

public class FragmentoInicio extends Fragment {


    public WebView mWebView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_fragmento_inicio, container, false);
        mWebView = (WebView) v.findViewById(R.id.webView);
        mWebView.loadUrl("https://google.com/");

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());

        return v;
    }
}
    
asked by Jorge1023 03.11.2016 в 10:20
source

1 answer

1

Within your onItemClick() method you can perform the Fragment transaction, assuming your container in the layout is called frame_container :

  getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int pos,
                                    long id) {
            Fragmnent fragment = new FragmentoInicio();         
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();

            }
        });

Important to note the use of getSupportFragmentManager() or getFragmentManager() when making the transaction:

If the support library (android.support.v4.app.FragmentActivity) is used, it is used:

getSupportFragmentManager ()

otherwise and using an API greater than or equal to 14 is used:

getFragmentManager ()

    
answered by 03.11.2016 / 19:53
source