open a new fragment with listview when you click on each listview item

advertisements

I am following this youtube tutorial to create a recipe app (Navigation Drawer and Fragments With ListViews) Youtube tutorial and sucessfully implemented this. like when click the navigation drawer menu it will show the new fragment with listview (Veg, Non-veg). and when click the each item (Veg) it will show the list item name in toast (Veg).

But my app concept is something like this

So i need to do open a new different fragment with listview when the each listview item clicked. How to do that?

In my code when i click the each list item it will show the same fragment. i need to show different fragment for each item. please check my code. help me

This is Home.java (Fragment)

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Home extends Fragment {
    private ListView lv;
    private static String[] spacecrafts = {"Non-Veg", "Veg"};

    public static Home newInstance()
    {
        Home home =new Home();
        return home;
    }

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

        lv = (ListView) rootView.findViewById(R.id.home_lv);

        lv.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, spacecrafts));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                //Toast.makeText(getActivity(), spacecrafts[i], Toast.LENGTH_SHORT).show();
                Home.this.getFragmentManager().beginTransaction().replace(R.id.containerID, Veg.newInstance()).commit();
            }
         });
        return rootView;
    }

    @Override
    public String toString(){
        return "Home";
    }
}

This is Veg.java Fragment

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Veg extends Fragment {
    private ListView lv;
    private static String[] vegcat = {"Vegetables", "Fruits"};

    public static Veg newInstance()
    {
        Veg veg =new Veg();
        return veg;
    }

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

        lv = (ListView) rootView.findViewById(R.id.veg_lv);

        lv.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, vegcat));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
                Toast.makeText(getActivity(), vegcat[i], Toast.LENGTH_SHORT).show();
            }
        });
        return rootView;
    }

}

this is veg.xml layout code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@+id/veg_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


Create three separate fragments

1.VegFragment

2.NonVegFragment

3.Description Fragment

now on your home fragment's listview click replace home fragment with your another fragment(List of veg or nonveg). upon click of veg or nonveg menu item from listview replace current with the Description fragment which has your description data(Description Fragment)

code to replace fragment is as follows :

VegFragment finalFragment = new VegFragment();
         FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                            fragmentTransaction.replace(R.id.flContainer, finalFragment);
                            fragmentTransaction.commit();