Reputation: 69
I have a Fragment
(CreaStanzaFragment) with an ImageButton
.
This ImageButton
has a picture.
Now when I click on this imagebutton, he open a new class (Gallery, an activity with lot of images). When I click on one of this image I need that the ImageButton
(In fragment) change and open this fragment with this new picture in imagebutton
.
CreaStanzaFragment belongs to a navigationdrawer. This is Fragment code:
public class CreaStanzaFragment extends Fragment {
public CreaStanzaFragment(){}
ImageButton icon;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_log_creastanza, container, false);
return rootView;
}
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
icon = (ImageButton) view.findViewById(R.id.newicon);
icon.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getContext(), Galleria.class));
}
});
}
here i post only important parts of Galleria
public class Galleria extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_main); //where all the images are located
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// ......
recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new GalleryAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {
Bundle bundle = new Bundle();
bundle.putSerializable("images", images);
bundle.putInt("position", position);
Image image = images.get(position);
String ilnome = inomi.get(position);
Log.i("ILNOME", ilnome);
// What i need to do here to change pictures of ImageButton? How can i open Fragment?
ImageButton icon = (ImageButton) findViewById(R.id.newicon); //loceted in Fragment (CreaStanzaFragment)
imageButton.setBackgroundResource(0);
Glide.with(getApplicationContext()).load(image.getMedium())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageButton);
fragmentTransaction.commit();
}
Whit this code:
setContentView(R.layout.activity_log_creastanza);
ImageButton imageButton = (ImageButton) findViewById(R.id.newicon);
imageButton.setBackgroundResource(0);
Glide.with(getApplicationContext()).load(image.getMedium())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageButton);
When i open CreaStanzaFragment:
When i click on blue picture (open Galleria activity)
When i click on 1 of pictures:
WHY?
Upvotes: 0
Views: 1208
Reputation: 1054
Basic Problem is that you need to access your Fragment from the Galleria Instance. For that you can temporarily "pass" the current Fragment to the Galleria.
Galleria Class holding a reference to current CreaStanzaFragment:
public class Galleria extends Activity {
private static CreaStanzaFragment currentCreaStanzaFragment = null;
public static void setFragment(CreaStanzaFragment f) {currentCreaStanzaFragment = f;}
public static CreaStanzaFragment getFragment() {return currentCreaStanzaFragment;}
...
In CreaStamzaFragment you set the Fragment Reference before Intent Action is invoked:
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
icon = (ImageButton) view.findViewById(R.id.newicon);
icon.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// pass Fragment Reference
Galleria.setFragment(CreaStamzaFragment .this)
startActivity(new Intent(getContext(), Galleria.class));
}
});
}
In Galleria on Adapter Action, access the fragment and modify the imageButton (implies that fragment is still active in background and is not reloaded):
recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new GalleryAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {
Bundle bundle = new Bundle();
bundle.putSerializable("images", images);
bundle.putInt("position", position);
Image image = images.get(position);
String ilnome = inomi.get(position);
Log.i("ILNOME", ilnome);
// get imagebutton on fragment
CreaStamzaFragment f = Galleria.getFragment()
ImageButton icon = (ImageButton) f. getView().findViewById(R.id.newicon); //loceted in Fragment (CreaStanzaFragment)
// load as icon
icon.setBackgroundResource(0);
Glide.with(getApplicationContext()).load(image.getMedium())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(icon);
// explicit release reference
Galleria.setFragment(null);
// close galliera activity
Galleria.this.finish();
}
...
Note: Be aware that this introduces coupling between the Fragment and Galleria, a cleaner but possibly more complex process would be for example to use a Notification (Observer Pattern) from "Notifier" Galleria to "Listener" Fragment that an Image was selected and the Fragment should load the image instead in the notification handler.
Yet it is fast to implement and sufficient if Galleria/CreaStanzaFragment are not to be reused.
Upvotes: 1