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

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之间的调用关系了。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值