我如何用另一个 Fragment 更改另一个 activity 的 Image ImageButton
How can i change Image ImageButton from another activity with another Fragment
我有一个 Fragment
(CreaStanzaFragment) 和一个 ImageButton
。
这个ImageButton
有图。
现在,当我点击这个图片按钮时,他会打开一个新的 class(画廊,一个有很多图片的 activity)。当我单击其中一张图片时,我需要 ImageButton
(在片段中)更改并使用 imagebutton
中的新图片打开此片段。
CreaStanzaFragment 属于导航抽屉。
这是片段代码:
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));
}
});
}
这里我 post 只是 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();
}
编写此代码:
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);
当我打开 CreaStanzaFragment 时:
当我点击蓝色图片时(打开 Galleria activity)
当我点击其中一张图片时:
为什么?
基本问题是您需要从 Galleria 实例访问您的片段。为此,您可以暂时 "pass" 当前片段到 Galleria。
Galleria Class 持有对当前 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;}
...
在调用 Intent Action 之前在 CreaStamzaFragment 中设置片段引用:
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));
}
});
}
在 Galleria on Adapter Action 中,访问片段并修改 imageButton(意味着片段在后台仍然处于活动状态并且不会重新加载):
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();
}
...
注意: 请注意,这会引入 Fragment 和 Galleria 之间的耦合,一个更简洁但可能更复杂的过程例如使用来自 "Notifier" Galleria 到 "Listener" 已选择图像的片段,片段应在通知处理程序中加载图像。
然而,如果 Galleria/CreaStanzaFragment 不被重用,那么实施起来就足够了。
我有一个 Fragment
(CreaStanzaFragment) 和一个 ImageButton
。
这个ImageButton
有图。
现在,当我点击这个图片按钮时,他会打开一个新的 class(画廊,一个有很多图片的 activity)。当我单击其中一张图片时,我需要 ImageButton
(在片段中)更改并使用 imagebutton
中的新图片打开此片段。
CreaStanzaFragment 属于导航抽屉。 这是片段代码:
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));
}
});
}
这里我 post 只是 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();
}
编写此代码:
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);
当我打开 CreaStanzaFragment 时:
当我点击蓝色图片时(打开 Galleria activity)
当我点击其中一张图片时:
为什么?
基本问题是您需要从 Galleria 实例访问您的片段。为此,您可以暂时 "pass" 当前片段到 Galleria。
Galleria Class 持有对当前 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;}
...
在调用 Intent Action 之前在 CreaStamzaFragment 中设置片段引用:
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));
}
});
}
在 Galleria on Adapter Action 中,访问片段并修改 imageButton(意味着片段在后台仍然处于活动状态并且不会重新加载):
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();
}
...
注意: 请注意,这会引入 Fragment 和 Galleria 之间的耦合,一个更简洁但可能更复杂的过程例如使用来自 "Notifier" Galleria 到 "Listener" 已选择图像的片段,片段应在通知处理程序中加载图像。
然而,如果 Galleria/CreaStanzaFragment 不被重用,那么实施起来就足够了。