AndroidPro4_006_Fragments

# Ensure that there’s a default constructor for your fragment class.
    When the system restores a fragment, it calls the default constructor (with no arguments) and then restores this bundle of arguments to the newly created     fragment.

# Add a bundle of arguments as soon as you create a new fragment so these subsequent methods can properly set up your fragment, and so the system can restore your fragment properly when necessary.

# A fragment can save state into a bundle object when the fragment is being re-created, and this bundle object gets given back to the fragment’s onCreate() callback. This saved bundle is also passed to onInflate(), onCreateView(), and onActivityCreated(). 
    Note that this is not the same bundle as the one attached as initialization arguments. This bundle is one in which you are likely to store the current state of the fragment, not the values that should be used to initialize it.

# Fragment Lifecycle
newInstance() method,take the appropriate number and type of arguments, and then build the arguments bundle appropriately. 
public static MyFragment newInstance(int index) {
    MyFragment f = new MyFragment();
    Bundle args = new Bundle();
    args.putInt(“index”, index);
    f.setArguments(args);
    return f;
}
    \ onInflate()
    如果 fragment 定义在layout的XML里有<fragment> tag, onInflate()会被调用(就像有layout时activity的setContentView()一样).
    
    \ onAttach()
    This callback invoked after fragment is associated with its activity. getActivity()可以获得associted activity.
    在整个lifecycle中,getArguments()都可获得初始化fragment时的args,但是当fragment is attached to its activity,就不能再调用setArguments() method了.

    \ onCreated()
    Activity的onCreate()结束后调用. This callback gets the saved state bundle passed in.
    This callback is about as early as possible to create a background thread to get data that this fragment will need. Your fragment code is running on the UI thread, and you don’t want to do disk I/O or network accesses on the UI thread.

    \ onCreateView()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if(container == null)
        return null;
    View v = inflater.inflate(R.layout.details, container, false);
    TextView text1 = (TextView) v.findViewById(R.id.text1);
    text1.setText(myDataSet[ getPosition() ] );
    return v;
}
    important: do not attach the fragment’s view to the container parent in this callback
    \ onActivityCreated()
    This is called after the activity has completed its onCreate() callback.
    This is where you can do final tweaks to the user interface before the user sees it, be sure that any other fragment for this activity has been attached to your activity.
    \ onStart()
    This callback is tied to the activity’s onStart(). put your logic into the fragment’s onStart()

    \ onResume()
    This callback is tied to the activity’s onResume(). 
    \ onPause()
    Tied to the activity’s onPause()

    \ onSaveInstanceState()
     Usually called right after onPause(), can occur any time before onDestroy().

    \ onStop()
    This one is tied to the activity’s onStop(), A fragment that has been stopped could go straight back to the onStart() callback, which then leads to onResume().

    \ onDestroyView()
    \ onDestroy()
    Note that it is still attached to the activity and is still findable, but it can’t do much.

    \ onDetach() 
    The final callback in a fragment’s lifecycle.Once this is invoked, the fragment is not tied to its activity, release all its resources.

# Your Activity’s Layout XML for Landscape Mode
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout-land/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <fragment class="com.androidbook.fragments.bard.TitlesFragment"
        android:id="@+id/titles" android:layout_weight="1"
        android:layout_width="0px"
        android:layout_height="match_parent" />
    <FrameLayout
        android:id="@+id/details" android:layout_weight="2"
        android:layout_width="0px"
        android:layout_height="match_parent" />
</LinearLayout>
    Note: <fragment> tag 不接受任何子tag. 
            这只是activity的layout, 设置了fragment的位置,但是并没有定义他,所以必须再后面定义 in code. 

# Main Activity Code:
public boolean isMultiPane() {
    return getResources().getConfiguration().orientation
     == Configuration.ORIENTATION_LANDSCAPE;
}
/**
* Helper function to show the details of a selected item, either by
* displaying a fragment in-place in the current UI, or starting a
* whole new activity in which it is displayed.
*/
public void showDetails(int index) {
    Log.v(TAG, "in MainActivity showDetails(" + index + ")");
    if (isMultiPane()) {
        // Check what fragment is shown, replace if needed.
        DetailsFragment details = (DetailsFragment)getFragmentManager().findFragmentById(R.id.details);
        if ( (details == null) || (details.getShownIndex() != index) ) {
            // Make new fragment to show this selection.
            details = DetailsFragment.newInstance(index);
            // Execute a transaction, replacing any existing
            // fragment with this one inside the frame.
            Log.v(TAG, "about to run FragmentTransaction");
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); // 必须在 replace()之前调用,否则无效果.
            //ft.addToBackStack("details");
            ft.replace(R.id.details, details); // 可以用remove()和add()来代替replace().
            ft.commit();    // 让UI thread 准备更新.
        }
    } else {
    // Otherwise you need to launch a new activity to display
    // the dialog fragment with selected text.
    Intent intent = new Intent();
    intent.setClass(this, DetailsActivity.class);
    intent.putExtra("index", index);
    startActivity(intent);
    }
}

    \ Source Code for DetailsFragment
public class DetailsFragment extends Fragment {
    private int mIndex = 0;
    public static DetailsFragment newInstance(int index) {
        Log.v(MainActivity.TAG, "in DetailsFragment newInstance(" + index + ")");
        DetailsFragment df = new DetailsFragment();
        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        df.setArguments(args);
        return df;
    }
    public static DetailsFragment newInstance(Bundle bundle) {
        int index = bundle.getInt("index", 0);
        return newInstance(index);
    }
    @Override
    public void onCreate(Bundle myBundle) {
        Log.v(MainActivity.TAG, "in DetailsFragment onCreate. Bundle contains:");
        if(myBundle != null) {
            for(String key : myBundle.keySet()) {
                Log.v(MainActivity.TAG, " " + key);
            }
        }
        else {
            Log.v(MainActivity.TAG, "myBundle is null");
        }
        super.onCreate(myBundle);
        mIndex = getArguments().getInt("index", 0); // 为什么不简单的在newInstance()中getArguments呢?因为android会用默认构造函数 re-create fragment,而默认构造函                                                                       // 数不会调用你的newInstance(). (那newInstance()有什么价值?)
    }
    public int getShownIndex() {
    return mIndex;
    }
    @Override
    public View onCreateView(LayoutInflater inflater,
    ViewGroup container, Bundle savedInstanceState) {
        Log.v(MainActivity.TAG, "in DetailsFragment onCreateView. container = " + container);
        // Don't tie this fragment to anything through the inflater.
        // Android takes care of attaching fragments for us. The
        // container is only passed in so you can know about the
        // container where this View hierarchy is going to go.
        View v = inflater.inflate(R.layout.details, container, false);    // R.layout.details 对应下面的details.XML
        TextView text1 = (TextView) v.findViewById(R.id.text1);
         text1.setText(Shakespeare.DIALOGUE[ mIndex ] );
        return v;
     }
}
    The details.xml Layout File for the Details Fragment
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/details.xml -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView android:id="@+id/scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </ScrollView>
</LinearLayout>
    可以把这个layout放在/res/layout 下,这样他就是一个default layout(不同于/res/layout-land),

# FragmentTransactions
    \ A fragment must live inside a view container, also known as a view group.
    \ FrameLayout is a good choice as the container for the details fragment in the main.xml layout file of your activity. 
    \ 如果不用FrameLayout ,就不能swap.
    \ FrameLayout swap via FragmentTransactions 
# FragmentManager
    Fragmentmanager 属于 activity. 用于管理fragment.
内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值