简体   繁体   中英

Android using intent to open a fragment from an activity

I am implementing an app which has a gridview of images in one activity and one fragment for each image which contains the image in full screen. When i click on any of the images in the grid, it should open up the corresponding fragment. However, we cannot use intent to do this. here is my code

public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub
            if(position==0)
            {
                Intent i=new Intent(Gallery.this,ImageFrag1.class);
                startActivity(i);
            }

and the fragment is

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ImageFrag1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.imagefrag1, container, false);
    }
}

This fragment is bound to an activity ImagesSwipe. SO how do i achieve the transition between grid view item and its corresponding fragment. Thanks

You don't need one Fragment for one Image. Just reuse one Fragment with an ImageView in it's layout for every Image.

Fragments aren't invoked like Activities through an Intent. They can only exist as part off an Activity, that is what they are designed for. Think about them as a reusable UI-Modul for an Activity. To add a Fragment to an Activity you have to use the FragmentManager and the FragmentTransaction classes, which provide all interactions with Fragments.

    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(YourFragment.newInstance(), null);
    ft.commit();

Have a look at this guide from the Google Docs where the basic things about GridViews are described. In Addition you should read about Fragments . And here is a Tutorial about your approach.

You may want to check out DialogFrament, here is an example .

Instead of using intent you use FramentManager:

if(position==0)
{
FragmentManager fm = getFragmentManager();
ImageFrag1 imageDialog = new ImageFrag1()
ImageFrag1.show(fm, "image_title");
}

And your dialogFrament becomes:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ImageFrag1 extends DialogFragment {

public ImageFrag1() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dialog_fragment, container, false);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM