Fragment知识总结
2016-04-27 21:43
357 查看
1.静态加载fragment
创建2个类,都继承fragment。
创建一个activity,
2.动态加载fragment
3.fraagment之间的通信
多个fragment在一个activity里,主要都是通过getActivity这个方法实现的。getActivity()方法可以让Fragment获取到关联的Activity,然后再调用Activity的findViewById方法,就可以获取到和这个Activity关联的其它Fragment的控件了。
例如:
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final TextView textView= (TextView) getActivity().findViewById(R.id.text);
Button button= (Button) getActivity().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),textView.getText().toString(),Toast.LENGTH_LONG).show();
}
});
}
创建2个类,都继承fragment。
public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } }
public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, container, false); } }
创建一个activity,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" > <fragment android:id="@+id/fragment1" android:name="com.example.fragment.Fragment1" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="com.example.fragment.Fragment2" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
2.动态加载fragment
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); <p>//1.获取到FragmentManager,在Activity中可以直接通过getFragmentManager得到。</p><p>//2.开启一个事务,通过调用beginTransaction方法开启。</p><p>//3.向容器内加入Fragment,一般使用replace方法实现,需要传入容器的id和Fragment的实例。</p><p>//4.提交事务,调用commit方法提交。 </p> Fragment1 fragment1 = new Fragment1(); getFragmentManager().beginTransaction().replace(R.id.main_layout, fragment1).commit(); } }
3.fraagment之间的通信
多个fragment在一个activity里,主要都是通过getActivity这个方法实现的。getActivity()方法可以让Fragment获取到关联的Activity,然后再调用Activity的findViewById方法,就可以获取到和这个Activity关联的其它Fragment的控件了。
例如:
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final TextView textView= (TextView) getActivity().findViewById(R.id.text);
Button button= (Button) getActivity().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(),textView.getText().toString(),Toast.LENGTH_LONG).show();
}
});
}
相关文章推荐
- JSP注释 声明 输出表达式 脚本
- 多区域OSPF动态路由配置(拓扑图)
- 团队项目作业四 - WBS
- I/O Completions port
- [置顶] 嵌入式系统开发常用网址
- BZOJ 4562|HAOI 2016|食物链|动态规划
- bzoj 3823: 定情信物
- STL浅析
- poj2762 Going from u to v or from v to u? 强连通分量 + 拓扑排序
- 每日Scrum(9)
- BZOJ2301: [HAOI2011]Problem b 莫比乌斯反演
- java中final与static的区别
- 队列和栈
- Java基础
- 2015级C++第10、11周实践项目 继承和派生
- 内存泄露会导致程序可用内存耗尽,然后崩溃
- poj2793 素数和
- Xamarin.Forms WebView
- 牛客网今日头条笔试详解
- 搭建gpu的theano