我有Fragment
(CreaStanzaFragment),ImageButton
。
这个ImageButton
有一张图片。
现在当我点击这个图像按钮时,他打开了一个新类(Gallery,一个包含大量图像的活动)。当我点击其中一张图片时,我需要更改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));
}
});
}
这里我只发布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活动)
当我点击其中一张图片时:
为什么?
答案 0 :(得分:1)
基本问题是您需要从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;}
...
在CreaStamzaFragment中,您可以在调用Intent Action之前设置Fragment Reference:
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&#的通知(观察者模式),更简洁但可能更复杂的过程34; Galleria to" Listener"选择了Image的片段,Fragment应该在通知处理程序中加载图像。
然而,如果不重复使用Galleria / CreaStanzaFragment,那么实施起来很快并且足够。