码迷,mamicode.com
首页 > 其他好文 > 详细

怎样在一个fragment or 随意类中操作还有一个fragment中的方法

时间:2015-01-11 19:00:25      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

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




怎样在一个fragment or 随意类中操作还有一个fragment中的方法

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/4216861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!