以下内容来自于自己的实践和网络资料的整理,希望对你有帮助。
一、Activity把值传递给Fragment
建议的传值方式是通过Bundle来传递,而不是直接作为fragment的构造参数传递。在activity中创建bundle数据包,并调用fragment的setArguments(Bundle bundle)方法,即可将Bundle数据包传给fragment。在Fragment中用getArguments方法得到传递过来的值。
二、Fragment把值传递给Activity
在fragment中定义一个内部回调接口,再让包含该fragment的activity实现该回调接口,这样fragment即可调用该回调方法将数据传给activity。其实接口回调的原理都一样,以前的博客有讲到,接口回调是java不同对象之间数据交互的通用方法。
activity实现完了接口怎么传给刚才的fragment呢?当fragment添加到activity中时,会调用fragment的方法onAttach(),这个方法中适合检查activity是否实现了OnArticleSelectedListener接口,检查方法就是对传入的activity的实例进行类型转换,然后赋值给我们在fragment中定义的接口。
在一个fragment从activity中剥离的时候,就会调用onDetach方法,这个时候要把传递进来的activity对象释放掉,不然会影响activity的销毁,产生不必要的错误。注意看onAttach方法中的代码,在赋值之前要做一个判断,看看Activity中有没有实现了这个接口,用到了instanceof。如果没有实现接口,我们就抛出异常。
例:左侧加一堆按钮,点击左侧,之后右侧内容发生变化
fragment书写
<code class="hljs java has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:14px;"><span class="hljs-keyword" style="box-sizing: border-box;">package</span> com.stady.hg.demos.fragmentinteraction; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.app.Activity; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.app.Fragment; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.os.Bundle; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.view.LayoutInflater; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.view.View; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.view.ViewGroup; <span class="hljs-javadoc" style="box-sizing: border-box;">/** * Created by Intellij IDEA * project:FragmentInteraction * Author:hg * Email:531057892@qq.com * Date:2015/4/30 */</span> <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box;">MenuFragment</span> <span class="hljs-keyword" style="box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box;">Fragment</span> <span class="hljs-keyword" style="box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box;">View</span>.<span class="hljs-title" style="box-sizing: border-box;">OnClickListener</span> {</span> <span class="hljs-javadoc" style="box-sizing: border-box;">/** * 用来与外部activity交互的 */</span> <span class="hljs-keyword" style="box-sizing: border-box;">private</span> FragmentInteraction listterner; <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-title" style="box-sizing: border-box;">MenuFragment</span>(){} <span class="hljs-javadoc" style="box-sizing: border-box;">/** * 当FRagmen被加载到activity的时候会被回调 *<span class="hljs-javadoctag" style="box-sizing: border-box;"> @param</span> activity */</span> <span class="hljs-annotation" style="box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-keyword" style="box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onAttach</span>(Activity activity) { <span class="hljs-keyword" style="box-sizing: border-box;">super</span>.onAttach(activity); <span class="hljs-keyword" style="box-sizing: border-box;">if</span>(activity <span class="hljs-keyword" style="box-sizing: border-box;">instanceof</span> FragmentInteraction) { listterner = (FragmentInteraction)activity; } <span class="hljs-keyword" style="box-sizing: border-box;">else</span>{ <span class="hljs-keyword" style="box-sizing: border-box;">throw</span> <span class="hljs-keyword" style="box-sizing: border-box;">new</span> IllegalArgumentException(<span class="hljs-string" style="box-sizing: border-box;">"activity must implements FragmentInteraction"</span>); } } <span class="hljs-annotation" style="box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-keyword" style="box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onDetach</span>() { <span class="hljs-keyword" style="box-sizing: border-box;">super</span>.onDetach(); listterner = <span class="hljs-keyword" style="box-sizing: border-box;">null</span>; } <span class="hljs-javadoc" style="box-sizing: border-box;">/** * 定义了所有activity必须实现的接口 */</span> <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">interface</span> <span class="hljs-title" style="box-sizing: border-box;">FragmentInteraction</span> {</span> <span class="hljs-javadoc" style="box-sizing: border-box;">/** * Fragment 向Activity传递指令,这个方法可以根据需求来定义 *<span class="hljs-javadoctag" style="box-sizing: border-box;"> @param</span> str */</span> <span class="hljs-keyword" style="box-sizing: border-box;">void</span> process(String str); } <span class="hljs-annotation" style="box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="box-sizing: border-box;">public</span> View <span class="hljs-title" style="box-sizing: border-box;">onCreateView</span>(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_menu, container, <span class="hljs-keyword" style="box-sizing: border-box;">false</span>); View btn = view.findViewById(R.id.tv_button); View btn_m = view.findViewById(R.id.movie_button); View btn_c = view.findViewById(R.id.comic_button); <span class="hljs-keyword" style="box-sizing: border-box;">if</span> (btn != <span class="hljs-keyword" style="box-sizing: border-box;">null</span>||btn_m!=<span class="hljs-keyword" style="box-sizing: border-box;">null</span>||btn_c!=<span class="hljs-keyword" style="box-sizing: border-box;">null</span>) { btn.setOnClickListener(<span class="hljs-keyword" style="box-sizing: border-box;">this</span>); btn_m.setOnClickListener(<span class="hljs-keyword" style="box-sizing: border-box;">this</span>); btn_c.setOnClickListener(<span class="hljs-keyword" style="box-sizing: border-box;">this</span>); } <span class="hljs-keyword" style="box-sizing: border-box;">return</span> view; } <span class="hljs-annotation" style="box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-keyword" style="box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onClick</span>(View v) { <span class="hljs-keyword" style="box-sizing: border-box;">int</span> id = v.getId(); <span class="hljs-keyword" style="box-sizing: border-box;">switch</span> (id) { <span class="hljs-keyword" style="box-sizing: border-box;">case</span> R.id.tv_button: listterner.process(<span class="hljs-string" style="box-sizing: border-box;">"我是电视剧"</span>); <span class="hljs-keyword" style="box-sizing: border-box;">break</span>; <span class="hljs-keyword" style="box-sizing: border-box;">case</span> R.id.movie_button: listterner.process(<span class="hljs-string" style="box-sizing: border-box;">"我是电影"</span>); <span class="hljs-keyword" style="box-sizing: border-box;">break</span>; <span class="hljs-keyword" style="box-sizing: border-box;">case</span> R.id.comic_button: listterner.process(<span class="hljs-string" style="box-sizing: border-box;">"我是动漫"</span>); <span class="hljs-keyword" style="box-sizing: border-box;">break</span>; } } }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">10</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">11</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">12</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">13</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">14</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">15</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">16</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">17</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">18</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">19</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">20</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">21</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">22</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">23</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">24</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">25</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">26</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">27</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">28</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">29</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">30</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">31</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">32</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">33</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">34</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">35</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">36</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">37</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">38</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">39</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">40</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">41</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">42</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">43</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">44</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">45</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">46</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">47</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">48</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">49</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">50</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">51</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">52</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">53</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">54</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">55</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">56</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">57</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">58</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">59</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">60</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">61</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">62</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">63</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">64</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">65</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">66</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">67</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">68</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">69</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">70</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">71</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">72</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">73</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">74</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">75</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">76</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">77</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">78</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">79</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">80</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">81</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">82</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">83</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">84</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">85</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">86</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">87</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">88</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">89</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">90</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">91</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">92</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">93</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">94</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">95</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">96</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">97</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">98</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">99</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">100</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">101</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">102</span></li></ul>
!!!
注意在onDetach方法中把传递进来的activity释放掉。
activity书写
<code class="hljs java has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:14px;"><span class="hljs-keyword" style="box-sizing: border-box;">package</span> com.stady.hg.demos.fragmentinteraction; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.app.Activity; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.os.Bundle; <span class="hljs-keyword" style="box-sizing: border-box;">import</span> android.widget.TextView; <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="box-sizing: border-box;">class</span> <span class="hljs-title" style="box-sizing: border-box;">MainActivity</span> <span class="hljs-keyword" style="box-sizing: border-box;">extends</span> <span class="hljs-title" style="box-sizing: border-box;">Activity</span> <span class="hljs-keyword" style="box-sizing: border-box;">implements</span> <span class="hljs-title" style="box-sizing: border-box;">MenuFragment</span>.<span class="hljs-title" style="box-sizing: border-box;">FragmentInteraction</span>{</span> <span class="hljs-keyword" style="box-sizing: border-box;">private</span> TextView textView; <span class="hljs-javadoc" style="box-sizing: border-box;">/** * Called when the activity is first created. */</span> <span class="hljs-annotation" style="box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-keyword" style="box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">onCreate</span>(Bundle savedInstanceState) { <span class="hljs-keyword" style="box-sizing: border-box;">super</span>.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) findViewById(R.id.content_text); } <span class="hljs-javadoc" style="box-sizing: border-box;">/** * 给Fragment调用 *<span class="hljs-javadoctag" style="box-sizing: border-box;"> @param</span> str */</span> <span class="hljs-comment" style="box-sizing: border-box;">// public void setContent(String str)</span> <span class="hljs-comment" style="box-sizing: border-box;">// {</span> <span class="hljs-comment" style="box-sizing: border-box;">// if (str!=null)</span> <span class="hljs-comment" style="box-sizing: border-box;">// {</span> <span class="hljs-comment" style="box-sizing: border-box;">// textView.setText(str);</span> <span class="hljs-comment" style="box-sizing: border-box;">// }</span> <span class="hljs-comment" style="box-sizing: border-box;">// }</span> <span class="hljs-annotation" style="box-sizing: border-box;">@Override</span> <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-keyword" style="box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">process</span>(String str) { <span class="hljs-keyword" style="box-sizing: border-box;">if</span> (str != <span class="hljs-keyword" style="box-sizing: border-box;">null</span>) { textView.setText(str); } } } </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">10</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">11</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">12</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">13</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">14</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">15</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">16</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">17</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">18</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">19</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">20</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">21</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">22</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">23</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">24</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">25</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">26</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">27</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">28</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">29</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">30</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">31</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">32</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">33</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">34</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">35</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">36</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">37</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">38</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">39</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">40</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">41</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">42</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">43</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">44</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">45</span></li></ul>
三、Fragment与Fragment之间的传值
在Activity中加载Fragment的时候、有时候要使用多个Fragment切换、并传值到另外一个Fragment、也就是说两个Fragment之间进行参数的传递、查了很多资料、找到两种方法。
1、通过共同的Activity传递
这种方法是在Activity里面添加一个字段、来临时保存要一些值。在Activity中定义一个字段、然后添加set和get方法、代码如下、mTitle就是要传递的参数、如果是传递对象、可以把mTitle换成一个对象即可.
<code class="hljs cs has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:14px;"><span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-keyword" style="box-sizing: border-box;">class</span> DemoActivity { <span class="hljs-keyword" style="box-sizing: border-box;">private</span> String mTitle; <span class="hljs-keyword" style="box-sizing: border-box;">public</span> String <span class="hljs-title" style="box-sizing: border-box;">getmTitle</span>() { <span class="hljs-keyword" style="box-sizing: border-box;">return</span> mTitle; } <span class="hljs-keyword" style="box-sizing: border-box;">public</span> <span class="hljs-keyword" style="box-sizing: border-box;">void</span> <span class="hljs-title" style="box-sizing: border-box;">setmTitle</span>(String title) { <span class="hljs-keyword" style="box-sizing: border-box;">this</span>.mTitle = title; } }</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">8</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">9</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">10</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">11</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">12</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">13</span></li></ul>
Fragment调用方法、需要注意的是在设值的时候要进行强转一下
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:14px;">((DemoActivity)getActivity())<span class="hljs-preprocessor" style="box-sizing: border-box;">.getmTitle</span>()<span class="hljs-comment" style="box-sizing: border-box;">;</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">1</span></li></ul>
2、通过Bundle来传递
可以使用bundle进行参数传递、这样在两个Fragment跳转的时候就可以带上参数了、同样也可以传递一个复杂的对象。
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:14px;">ft<span class="hljs-preprocessor" style="box-sizing: border-box;">.hide</span>(getActivity()<span class="hljs-preprocessor" style="box-sizing: border-box;">.getSupportFragmentManager</span>()<span class="hljs-preprocessor" style="box-sizing: border-box;">.findFragmentByTag</span>(<span class="hljs-string" style="box-sizing: border-box;">""</span>))<span class="hljs-comment" style="box-sizing: border-box;">;</span> DemoFragment demoFragment = new DemoFragment()<span class="hljs-comment" style="box-sizing: border-box;">; </span> Bundle bundle = new Bundle()<span class="hljs-comment" style="box-sizing: border-box;">; </span> bundle<span class="hljs-preprocessor" style="box-sizing: border-box;">.putString</span>(<span class="hljs-string" style="box-sizing: border-box;">"key"</span>, <span class="hljs-string" style="box-sizing: border-box;">"这是方法二"</span>)<span class="hljs-comment" style="box-sizing: border-box;">; </span> demoFragment<span class="hljs-preprocessor" style="box-sizing: border-box;">.setArguments</span>(bundle)<span class="hljs-comment" style="box-sizing: border-box;">; </span> ft<span class="hljs-preprocessor" style="box-sizing: border-box;">.add</span>(R<span class="hljs-preprocessor" style="box-sizing: border-box;">.id</span><span class="hljs-preprocessor" style="box-sizing: border-box;">.fragmentRoot</span>, demoFragment, SEARCHPROJECT)<span class="hljs-comment" style="box-sizing: border-box;">; </span> ft<span class="hljs-preprocessor" style="box-sizing: border-box;">.commit</span>()<span class="hljs-comment" style="box-sizing: border-box;">; </span> </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">1</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">2</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">3</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">4</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">5</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">6</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">7</span></li><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">8</span></li></ul>
在另外一个Fragment获取参数的方式只需要一个语句、key是自己定义的一个标识、参数的形式只要bundle能传递都可以实现
<code class="hljs vbnet has-numbering" style="display: block; padding: 0px; box-sizing: border-box; font-family: 'Source Code Pro', monospace; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span style="font-size:14px;"><span class="hljs-built_in" style="box-sizing: border-box;">String</span> <span class="hljs-built_in" style="box-sizing: border-box;">string</span> = getArguments().getString(<span class="hljs-string" style="box-sizing: border-box;">"key"</span>); </span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;"><span style="font-size:14px;">1</span></li></ul>
从上面两种方法看来、方法一要简单一些、但个人觉得有点投机取巧、不过也很合Java的开发规范、如果是同一个Activity中要加载很多Fragment的话、这种方法无疑要简单很多。而方法二是使用了Android的Bundle传递的、这种方法应该更官方一点吧、不过在项目开发中、怎么实现简单就怎么来、也没太多关系