Android Fragment学习笔记
16lz
2021-01-23
本程序的重点是在一个Activity中放入了两个Fragment。
页面布局如下面代码(fragment_hide_show.xml):
[java] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:gravity="center_horizontal"
- android:layout_width="match_parent"android:layout_height="match_parent">
- <TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"
- android:gravity="center_vertical|center_horizontal"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:text="Demonstrationofhidingandshowingfragments."/>
- <LinearLayoutandroid:orientation="horizontal"android:padding="4dip"
- android:gravity="center_vertical"android:layout_weight="1"
- android:layout_width="match_parent"android:layout_height="wrap_content">
- <Buttonandroid:id="@+id/frag1hide"
- android:layout_width="wrap_content"android:layout_height="wrap_content"
- android:text="Hide"/>
- <fragmentandroid:name="polycom.com.cn.FirstFragment"
- android:id="@+id/fragment1"android:layout_weight="1"
- android:layout_width="0px"android:layout_height="wrap_content"/>
- </LinearLayout>
- <LinearLayoutandroid:orientation="horizontal"android:padding="4dip"
- android:gravity="center_vertical"android:layout_weight="1"
- android:layout_width="match_parent"android:layout_height="wrap_content">
- <Buttonandroid:id="@+id/frag2hide"
- android:layout_width="wrap_content"android:layout_height="wrap_content"
- android:text="Hide"/>
- <fragmentandroid:name="polycom.com.cn.SecondFragment"
- android:id="@+id/fragment2"android:layout_weight="1"
- android:layout_width="0px"android:layout_height="wrap_content"/>
- </LinearLayout>
- </LinearLayout>
在每一个LinearLayout里均有一个Fragment,以下是java代码实现。主要分为三个.java文件,其中FragmentActivity.java主要是设置了两个按钮监听器,用来控制显示和隐藏Fragment中的内容,FirstFragment.java、SecondFragment.java主要是为Fragment加载显示页面。
FragmentActivity.java
[java] view plain copy
- importandroid.app.Activity;
- importandroid.app.Fragment;
- importandroid.app.FragmentManager;
- importandroid.app.FragmentTransaction;
- importandroid.os.Bundle;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- publicclassFragmentActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.fragment_hide_show);
- FragmentManagerfm=getFragmentManager();
- //下面的两行代码是在Activity中按钮添加监听事件,控制Fragment的显示和隐藏
- addShowHideListener(R.id.frag1hide,fm.findFragmentById(R.id.fragment1));
- addShowHideListener(R.id.frag2hide,fm.findFragmentById(R.id.fragment2));
- }
- voidaddShowHideListener(intbuttonId,finalFragmentfragment){
- //获取activity中的button
- finalButtonbutton=(Button)findViewById(buttonId);
- button.setOnClickListener(newOnClickListener(){
- publicvoidonClick(Viewv){
- FragmentTransactionft=getFragmentManager().beginTransaction();
- /*为Fragment设置淡入淡出效果,Android开发网提示这里这两个动画资源是android内部资源无需我们手动定义。*/
- ft.setCustomAnimations(android.R.animator.fade_in,
- android.R.animator.fade_out);
- if(fragment.isHidden()){
- ft.show(fragment);
- button.setText("隐藏");
- }else{
- ft.hide(fragment);
- button.setText("显示");
- }
- ft.commit();
- }
- });
- }
- }
FirstFragment.java
[java] view plain copy
- importandroid.app.Fragment;
- importandroid.os.Bundle;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.TextView;
- publicclassFirstFragmentextendsFragment{
- TextViewmTextView;
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
- BundlesavedInstanceState){
- //从文件example_fragment.xml加载了一个layout
- Viewv=inflater.inflate(R.layout.labeled_text_edit,container,true);
- Viewtv=v.findViewById(R.id.msg);
- ((TextView)tv).setText("Thefragmentsavesandrestoresthistext.");
- mTextView=(TextView)v.findViewById(R.id.saved);
- if(savedInstanceState!=null){
- mTextView.setText(savedInstanceState.getCharSequence("text"));
- }
- returnv;
- }
- @Override
- publicvoidonSaveInstanceState(BundleoutState){
- super.onSaveInstanceState(outState);
- outState.putCharSequence("text",mTextView.getText());
- }
- }
[java] view plain copy
- importandroid.app.Fragment;
- importandroid.os.Bundle;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.ViewGroup;
- importandroid.widget.TextView;
- publicclassSecondFragmentextendsFragment{
- @Override
- publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
- BundlesavedInstanceState){
- //从文件example_fragment.xml加载了一个layout
- Viewv=inflater.inflate(R.layout.labeled_text_edit,container,true);
- Viewtv=v.findViewById(R.id.msg);
- ((TextView)tv).setText("TheTextViewsavesandrestoresthistext.");
- //另外一种TextView的保存模式
- ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true);
- returnv;
- }
- }
下面是Fragment中加载的布局文件(labeled_text_edit.xml)
[java] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:padding="4dip"
- android:layout_width="match_parent"android:layout_height="wrap_content">
- <TextViewandroid:id="@+id/msg"
- android:layout_width="match_parent"android:layout_height="wrap_content"
- android:layout_weight="0"
- android:paddingBottom="4dip"/>
- <EditTextandroid:id="@+id/saved"
- android:layout_width="match_parent"android:layout_height="wrap_content"
- android:layout_weight="1"
- android:text="@string/initial_text"
- android:freezesText="true">
- <requestFocus/>
- </EditText>
- </LinearLayout>
http://download.csdn.net/detail/flying_tao/3422836
更多相关文章
- Android中播放mp3文件
- Android 文件的下载
- android 分享到 代码
- Android中NFC功能流程图解析及代码演示『轉』
- Android 将图片转化为缩略图再加载
- android 关于 发送post请求的代码总结(包含加上参数)