1

I am working on an android application. I am loading some view inside a fragment in an activity. The First Fragment is having Gridview/ListView in this. I want to load another Fragment in the same activity when any Item of Listview/Gridview will be clicked, without restarting the activity.

How can I do this?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78

3 Answers3

0

Fragment Code:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {



                        YourFragment yf= new YourFragment ();



                      ((HomeActivity) mActivity).replceFrament(groupControlFragment);




        }
    });

MainActivity Code:

public void replceFrament(Fragment fragment) {

FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment,tag).addToBackStack("back").commit();

}
0

Here is an example. It not an exact answer, its just for a hint.

Fragment class may be look like this:

public class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;

    // Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Append the clicked item's row ID with the content provider Uri
        Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
        // Send the event and Uri to the host activity
        mListener.onArticleSelected(position);
    }

}

And the activity class will be like below:

public static class DetailsActivity extends Activity implements FragmentA.OnArticleSelectedListener{

    @Override
    public void onArticleSelected(int position){
        // your implementation ...
    }
}
Md Sufi Khan
  • 1,751
  • 1
  • 14
  • 19
  • Thank yo so much for this idea, it worked for me. Thanks a lot. I have one more issue that on clicking on any Gridview item I load the send fragment in the same activity by replacing the first fragment, but When I press back button of activity first fragment should be shown there, how can I do that ? – Prithniraj Nicyone Sep 02 '16 at 05:20
  • First you have to put first fragment in to stack by `addToBackStack`. To pop the stacked fragment use `getFragmentManager().popBackStack();` For more details you may follow the 2nd answer of http://stackoverflow.com/questions/10863572/programmatically-go-back-to-the-previous-fragment-in-the-backstack or http://stackoverflow.com/questions/25350397/android-return-to-previous-fragment-on-back-press – Md Sufi Khan Sep 04 '16 at 05:25
0

Inside your fragment which has the listview/gridview.

Fragment[] fragment=new Fragment{new MyFragment1(), new MyFragment2(), new MyFragment3()};

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

       View view=inflater.inflate(R.layout.mylayout, container, false);
       ListView listView=(ListView)view.findViewById(R.id.list_id);

       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    replaceFragment(fragment[i]);
                }
            });
      return view;
}

public void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager=getFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.containerLayout,fragment);
        fragmentTransaction.addToBackStack(null);           //Optional
        fragmentTransaction.commit();
}
Darshan Miskin
  • 844
  • 14
  • 25