本人初学Fragment,写有几个简单例子,现提出来方便大家学习和研究。

Android Fragment学习笔记(一)

本程序的重点是在一个Activity中放入了两个Fragment。


页面布局如下面代码(fragment_hide_show.xml):

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns: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">    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"        android:gravity="center_vertical|center_horizontal"        android:textAppearance="?android:attr/textAppearanceMedium"        android:text="Demonstration of hiding and showing fragments." />    <LinearLayout android:orientation="horizontal" android:padding="4dip"        android:gravity="center_vertical" android:layout_weight="1"        android:layout_width="match_parent" android:layout_height="wrap_content">        <Button android:id="@+id/frag1hide"            android:layout_width="wrap_content" android:layout_height="wrap_content"            android:text="Hide" />        <fragment android:name="polycom.com.cn.FirstFragment"                android:id="@+id/fragment1" android:layout_weight="1"                android:layout_width="0px" android:layout_height="wrap_content" />    </LinearLayout>    <LinearLayout android:orientation="horizontal" android:padding="4dip"        android:gravity="center_vertical" android:layout_weight="1"        android:layout_width="match_parent" android:layout_height="wrap_content">        <Button android:id="@+id/frag2hide"            android:layout_width="wrap_content" android:layout_height="wrap_content"            android:text="Hide" />        <fragment android: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

import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class FragmentActivity extends Activity {@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.fragment_hide_show);        FragmentManager fm = getFragmentManager();                //下面的两行代码是在Activity中按钮添加监听事件,控制Fragment的显示和隐藏        addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1));        addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2));    }    void addShowHideListener(int buttonId, final Fragment fragment) {    //获取activity中的button        final Button button = (Button)findViewById(buttonId);        button.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                FragmentTransaction ft = 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

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 FirstFragment extends Fragment {    TextView mTextView;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {    //从文件 example_fragment.xml 加载了一个layout         View v = inflater.inflate(R.layout.labeled_text_edit, container, true);                View tv = v.findViewById(R.id.msg);        ((TextView)tv).setText("The fragment saves and restores this text.");        mTextView = (TextView)v.findViewById(R.id.saved);        if (savedInstanceState != null) {            mTextView.setText(savedInstanceState.getCharSequence("text"));        }        return v;    }    @Override    public void onSaveInstanceState(Bundle outState) {        super.onSaveInstanceState(outState);        outState.putCharSequence("text", mTextView.getText());    }}
SecondFragment.java

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 SecondFragment extends Fragment {    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {    //从文件 example_fragment.xml 加载了一个layout         View v = inflater.inflate(R.layout.labeled_text_edit, container, true);        View tv = v.findViewById(R.id.msg);        ((TextView)tv).setText("The TextView saves and restores this text.");        //另外一种TextView的保存模式        ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true);        return v;    }}

下面是Fragment中加载的布局文件(labeled_text_edit.xml)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:padding="4dip"    android:layout_width="match_parent" android:layout_height="wrap_content">    <TextView android:id="@+id/msg"        android:layout_width="match_parent" android:layout_height="wrap_content"        android:layout_weight="0"        android:paddingBottom="4dip" />    <EditText android: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>

这只是一个简单的例子,仅供刚学习Fragment的人员进行参考,希望我在这里能起到一个抛砖引玉的作用。如果需要完整的程序,可以到我空间资源里下载,资源名字为myFragment。




更多相关文章

  1. Android启动器(Launcher)开发详解
  2. 解决Android(安卓)studio 2.3升级到Android(安卓)studio 3.0 后a
  3. Android滑动冲突解决方式(下拉刷新上拉加载更多,适配RecyclerVie
  4. Android(安卓)Studio添加so文件并打包到APK的lib文件夹中
  5. 将tensorflow训练好的模型移植到Android(安卓)(MNIST手写数字识
  6. 创建android phonegap项目
  7. android studio 设置模板
  8. Android偶遇问题处理
  9. build/envsetup.sh简记

随机推荐

  1. android menu自定义菜单 Demo
  2. android 监听其它app的错误或者崩溃的方
  3. android java 文件的读写
  4. Android(安卓)ContextMenu和Listview配合
  5. Android之关于Intent的Flag属性的讲解
  6. How to fix the Android(安卓)AVD no tar
  7. OnclickListener与View.OnclickListener
  8. Android(安卓)studio3.1.2 坑——3rd-par
  9. Android(安卓)Event.getX 与getRawX的区
  10. Android调用系统播放器