您的位置:首页 > 其它

如何在一个fragment or 任意类中操作另一个fragment中的方法

2014-04-19 09:12 459 查看
1 如何在acitivty中执行fragment中的方法:

首先获得这个Fragment的对象

xxxFragment fragmentObject = (xxxFragment) getFragmentManager.findFragmentByTag("xx");


2 如何在Fragment中执行activity中的方法:

第一种:让acitivity继承接口

第二种:在fragment中使用getActivity()但是要加上acitivity的名字,即:

((MainActivity)getActivity()).xxx();


3 如果在fragment中要操作一个fragment,首要要得到这个对象,如何得到?使用getActivity中的FragmentMnager的getFragmentByTag,然后就可以使用这个fragment的对象来操作他的方法了。

当然获得这个Fragment的时候要转成这个Fragment的一个对象eg:

FragmentManager fm = getActivity.getSupportFragmentManager();


xxxFragment = (xxxFragment)fm.findFragmentByTag("xxx")


4 如何在任意类中操作一个fragment,首先要得到环境参数,如何得到?

在activity中:

private static WeakReference<ActionButtonActivity> actionButtonActivty = null;
actionButtonActivty = new WeakReference<ActionButtonActivity>(this);
从activity中将这个actionButtonActivity对象传递到这个任意类中

asyncTask.setActivity(actionButtonActivty);
在任意类中:

private static WeakReference<ActionButtonActivity> actionButtonActivty;
public void setActivity(
WeakReference<ActionButtonActivity> actionButtonActivty) {
this.actionButtonActivty = actionButtonActivty;
}


/**
* this method is invoked on the UI thread after the background computation
* finishes. The result of the background computation is passed to this step
* as a parameter.
*/
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);

FragmentManager fm = actionButtonActivty.get().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
BFragmentTab_one_event_details bt_det = (BFragmentTab_one_event_details) fm
.findFragmentByTag("2_det");
bt_det.setEvidenceImage(result);
bt_det.setButtonClickable();
ft.addToBackStack(null).commit();

}


OK 上面已经介绍了所有类和fragment之间的调用关系了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐