Open ImageView in Fragment by clicking on ImageView

0

How can I do so that every time I click on an image it opens in a Fragment ?

I have done the following, I do not know if it will be the right method but it is what has occurred to me.

My main class: Here I have the three ImageView where I click and open the Fragment :

    image1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            DialogFragment newFragment = MyDialogFragment.newInstance();
            newFragment.show(ft, "tag");
        }
    });

    image2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            DialogFragment newFragment = MyDialogFragment.newInstance();
            newFragment.show(ft, "tag");
        }
    });

    image3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            DialogFragment newFragment = MyDialogFragment.newInstance();
            newFragment.show(ft, "tag");
        }
    });

Fragment :

public class MyDialogFragment extends DialogFragment {


    static MyDialogFragment newInstance() {

        return new MyDialogFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_my_dialog_fragment, container, false);


        return view;
    }
}

activity_my_dialog_fragment :

<ImageView
    android:id="@+id/imagefragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:srcCompat="@drawable/ic_launcher"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />

How can I make every time I click on one of the images in my%% of main% the clase is opened but the image I clicked on is displayed there?

    
asked by UserNameYo 28.05.2017 в 21:58
source

2 answers

1

Add a Bundle when starting DialogFragment adding as extra the Drawable :

Bundle argumentos = new Bundle(); 
argumentos.putInt("imagen", R.drawable.ic_launcher_round); 
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.setArguments(argumentos);
newFragment.show(ft, "tag");

To get id of Drawable you get it in the following way:

public class MyDialogFragment extends DialogFragment {

    static MyDialogFragment newInstance() {

        return new MyDialogFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_my_dialog_fragment, container, false);

        //tu imageview
        ImageView imageView = (ImageView) view.findViewById(R.id.imagefragment);


        Bundle bundle = this.getArguments();
        if (bundle != null) {
          int myInt = bundle.getInt("imagen", 0);
          imageView.setImageResource(myInt);
        }

        return view;
    }
}

The reference of Drawable is only a int , but with this system you can pass any element.

    
answered by 29.05.2017 / 03:21
source
0

It may sound ordinary and if you use ImageButton, I am making a music player and instead of using images directly I use ImageButton with the images of previus play and next

Edited

android.support.v4.app.FragmentManager fm;
android.support.v4.app.FragmentTransaction ft;  

ImageButton btn=(ImageButton)findViewById(R.id.Button)


 btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDialogFragment dl=newMyDialogFragment();
                fm=getSupportFragmentManager();
                ft=fm.beginTransaction().add(R.id.Container,dl);
                ft.addToBackStack(null);
                ft.commit();
            }
        });

Where container is the container you want to replace, be it a RelativeLayout etc.

    
answered by 29.05.2017 в 05:28