fragment里面是没有findviewbyid的 那么怎么获得其中的控件呢 ?
MainActivity中建立fragmentManager -->使用fm.findFragmentById(R.id.fragment1)
在该fragment 的类中 onCreateView 方法内提供了inflater属性 使用这个属性 可以从XML文件填充一个view(控件就在这个xml文件上) View view = inflater.inflate(R.layout.fragment1, null);
通过这个view 就可以Button bt = (Button) view.findViewById(R.id.bt); 接下来 就可以为其在fragment 的类中注册事件
但是 我们的目的是和其他 fragment 交互 其他fragment的组件我怎么在当前的fragment类中建立关系呢?
在事件中 getActivity() 可以取得当前fragment所绑定的Activity(在这个例子里面是MainActivity)
然后getActivity().getFragmentManager()找到管理者 再findFragmentById(R.id.xxx),即getActivity().getFragmentManager().findFragmentById(R.id.要建立联系的目标fragment)
这时候2者已经建立了联系 此时目标fragment需要暴露一个方法供其他fragment使用
activity_main.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <fragment android:id="@+id/fragment1" android:name="com.example.firstfragment.Fragment1" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="com.example.firstfragment.Fragment2" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" /> </LinearLayout> |
fragment1.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000ff" android:orientation="vertical" > <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击改变fragment2的文字 " /></LinearLayout> |
fragment2.xml
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0000" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是文本" /></LinearLayout> |
MainActivity.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.example.firstfragment;import android.os.Bundle;import android.app.Activity;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //仅仅展示了怎样在MainActivity获得Fragment对象// FragmentManager fm = getFragmentManager();// Fragment1 fragment1 = (Fragment1) fm.findFragmentById(R.id.fragment1);//找到第一个Fragment// } } |
Fragment1.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package com.example.firstfragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;public class Fragment1 extends Fragment{ /** * 当Fragment被创建时候调用的方法,返回当前Fragment显示的内容 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, null); Button bt = (Button) view.findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.out.println("按钮被点击"); Fragment2 fragment2 = (Fragment2) getActivity().getFragmentManager().findFragmentById(R.id.fragment2);//建立联系 fragment2.setText("一点击就改变"); } }); return view; }} |
Fragment2.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.example.firstfragment;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class Fragment2 extends Fragment{ /** * 当Fragment被创建时候调用的方法,返回当前Fragment显示的内容 */ private TextView tv ; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment2, null); tv = (TextView) view.findViewById(R.id.tv); return view; } public void setText(String text) { tv.setText(text); }} |
浙公网安备 33010602011771号