2023年7月13日发(作者:)
Fragment详解之五——Fragment间参数传递在关Fragment间参数的传递,有两种情况:第⼀种情况:同⼀个container中不同fragment间的参数传递。这种情况⼀般发⽣在fragment跳转时,上⼀个Fragment将参数传递给下⼀个Fragment。第⼆种情况:是同⼀个Activity中,不个container间Fragment的参数传递。有关第⼀种情况,以前写过⼀篇⽂章,详细说明了上⼀个Fragment将参数传递给下⼀个Fragment,及数据回传的⽅法。详细参见:《Fragment跳转时传递参数及结果回传的⽅法》下⾯详细看看这两种参数传递⽅法。⼀、同⼀个container间的参数传递。有关这个问题,请⼤家移步以前写的⼀篇⽂章:《Fragment跳转时传递参数及结果回传的⽅法》,在这篇⽂章中有关Fragment跳转时的参数传递和结果回传已经讲的很清楚了,这⾥就没必要重新再写⼀遍,下⾯是这篇⽂章的效果图:1、在界⾯开始时,点击“加载第⼆个Fragment”按钮2、调起Fragment2,并向其传递⼀个参数“从Fragment1传来的参数”,显⽰在Fragment2中3、点击Fragment2中的四个⼩动物中的⼀个,会向Fragment1回传⽤户点击的是哪个动物,在Fragment1中显⽰出来。看起来挺有意思?那就移步到这篇⽂章⾥看看吧⼆、同⼀个Activity,不同container间的参数传递这⾥到了这篇⽂章的重点内容了哦,这可并不是说上⼀部分不重要哈,其实上⼀部分要⽐这部分重要!同⼀个container中不同Fragment间的参数传递⼀般的⼯程都会⽤到的,所以⼤家⼀定要看。⽽我这⾥不讲,是因为以前有讲过,这⾥就没必要再重复⼀遍了,好了,废话说了好多……开整吧。先看看效果图:1、在这个Actiivty中有两个Fragment;2、Fragment1中有⼀个listView,当我们点击ListView的Item的时候,把Item上的内容更新到Fragment2上这⾥有多种实现⽅法,最可取的是⽅法三。我们由简到易慢慢讲。我们想使两个fragment实例要能通信,那如果我们都能通过findViewById()找到所有的控件,直接操控的话,岂不就实现了。⽽通过findViewById()能找到所有控件实例的地⽅就是在Activity中了,所以这就有了⽅法⼀。⽅法⼀:直接在Activity中操作在Activity中找到对应的控件实例,然后直接操控即可。先看看MainActivity的布局:activity_
可以看到在fragment2中⾮常⼲净,只有⼀个TextView来显⽰当前⽤户在fragment1中的点击结果。下⾯看看在MainActivity中是如何实现的吧protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_main);
ArrayAdapter arrayAdapter = new ArrayAdapter
mFragment2_tv = (TextView)findViewById(nt2_tv);
temClickListener(new ClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { mFragment2_t(mStrings[position]); } });}其中:private String[] mStrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler"};难度不⼤,通过(ListView)findViewById();找到fragment1中的listview,通过(TextView)findViewById(nt2_tv);找到fragment2中的textView,然后直接对他们进⾏操作。即下⾯的代码:temClickListener(new ClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { mFragment2_t(mStrings[position]); }});当⽤户点击listView的⼀个item时,将值setText到fragment2的textView中。源码在⽂章底部给出可见,直接在activity中操作各个fragment的控件就可以实现消息互传。但,这样真的好吗?如果每个fragment中的控件都在Activity中操作,那还要fragment⼲嘛!最最起码,应该每个fragment负责⾃⼰的控件操作才对嘛!所以,我们对这种⽅法进⾏改进,将点击Item的赋值操作放到fragment1中去。所以,这就有⽅法⼆;⽅法⼆:直接在fragment中操作在这⾥我们会把所有⽅法写在Fragment1中,这⾥涉及到两⽅⾯的内容:第⼀:在Fragment中如何获得⾃⼰控件的引⽤,⽐较这⾥Fragment1⾥的listview控件。第⼆:在Fragment中如何获得其它Fragment页⾯中控件的引⽤,⽐如这⾥Fragment2⾥的TextView控件。
⾸先,获取⾃⼰控件引⽤的⽅法:
⽅法⼀:在onCreateView()中获取。
就⽐如这⾥获取⾃⼰的listView控件的引⽤,代码如下:public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = e(nt1, container, false); listView = (ListView)ewById();//获取⾃⼰视图⾥的控件引⽤,⽅法⼀ return rootView;}由于在onCreateView()中,还没有创建视图,所以在这⾥如果使⽤getView()⽅法将返回空。所以如果要获取其实图中指定控件的引⽤,只⽤⽤e()返回的rootView;在这个rootView()中⽤findViewById来查找。⽅法⼆:在onActivityCreated()函数中获取。从《Fragment详解之⼀——概述》的流程图中可以看到,onActivityCreated()回调会在Activity的OnCreate()执⾏完成后再执⾏,也就是说,onActivityCreated()会在Activity的OnCreate()⼯作完成以后才会执⾏。所以当执⾏到onActivityCreated()的时候Activity已经创建完成,它其中的各个fragment也视图等等的也都已经创建完成。所在可以在这⾥获取跟Activity相关的各种资源。第⼆个问题中的获取其它Fragment页⾯中控件的引⽤也是在onActivityCreated()中来做的。先看看在onActivityCreated()中如何获得⾃⼰视图中控件的引⽤吧public void onActivityCreated(Bundle savedInstanceState) { vityCreated(savedInstanceState);
listView = (ListView) getView().findViewById();//获取⾃⼰视图⾥的控件引⽤,⽅法⼆}
然后,获得其它Fragment页⾯中控件的引⽤的⽅法在上⾯已经说了,要获取Activity中的资源,就必须等Acitivity创建完成以后,所以必须放在onActivityCreated()回调函数中。其获取⽅法为:public void onActivityCreated(Bundle savedInstanceState) { vityCreated(savedInstanceState);
mFragment2_tv = (TextView) getActivity().findViewById(nt2_tv);//获取其它fragment中的控件引⽤的唯⼀⽅法
}上⾯讲了⼀堆之后,下⾯就看看在Fragment1中如何实现的吧。由上⾯的讲述可知,⽆论是获取⾃⼰视图中控件的引⽤还是获取其它fragment中控件的引⽤都可以放在onActivityCreated()函数中,所以我们就把它们全部放在onActivityCreated()中来实现了。public void onActivityCreated(Bundle savedInstanceState) { vityCreated(savedInstanceState);
mFragment2_tv = (TextView) getActivity().findViewById(nt2_tv);//获取其它fragment中的控件引⽤的唯⼀⽅法 listView = (ListView) getView().findViewById();//获取⾃⼰视图⾥的控件引⽤,⽅法⼆
ArrayAdapter arrayAdapter = new ArrayAdapter
public interface titleSelectInterface{ public void onTitleSelect(String title);}(2)、接⼝变量赋值接⼝是给activity⽤的,所以要在activity中给这⾥的接⼝变量赋值,可以有很多⽅法,当然可以选择写⼀个setXXX()函数来赋值,但如果⽤户忘了怎么办?所以我们要强制⽤户赋值。所以采⽤强转的⽅式,在fragment与activity相关联时,进⾏强转赋值:public void onAttach(Activity activity) { ch(activity);
try { mSelectInterface = (titleSelectInterface) activity; } catch (Exception e) { throw new ClassCastException(ng() + "must implement OnArticleSelectedListener"); }}采⽤强转的⽅式的问题在于,如果⽤户的activity没有implements titleSelectInterface,就会抛出错误,所以在调试过程中就会发现。(3)、调⽤接⼝变量下⼀步就是在fragment1中在⽤户点击listView的item的时候,将结果回传给Activity了,代码如下:public void onActivityCreated(Bundle savedInstanceState) { vityCreated(savedInstanceState);
listView = (ListView) getView().findViewById();//获取⾃⼰视图⾥的控件引⽤,⽅法⼆ ArrayAdapter arrayAdapter = new ArrayAdapter
……
@Override public void onTitleSelect(String title) { FragmentManager manager = getSupportFragmentManager(); Fragment2 fragment2 = (Fragment2)agmentById(nt2); t(title); }}在上⾯代码中可以看出,在结果返回后,通过findFragmentById()来获得fragment2的实例,这⾥⾸次出现了findFragmentById()函数的⽤法,这个函数主要⽤来静态添加的fragment中,通过fragment的ID值来获取它的实例。在获得fragment2的实例以后,通过调⽤我们写好了setText()⽅法来将结果显⽰在textView中。Fragment2 fragment2 = (Fragment2)agmentById(nt2);OK啦,到这⾥这篇⽂章就结束啦
发布者:admin,转转请注明出处:http://www.yc00.com/news/1689216216a222303.html
评论列表(0条)