日報2015/10/28(極客學院安卓視頻學習)

使用 Fragment

噹噹噹當,終於到了Fragment的學習了!

界面的切換使用Activity的話速度會慢一點,同一個app裏面使用Fragment進行界面切換方便而且效率,使用Fragment其實和Activity很像,也是和一個佈局xml相對應,在Fragment類中onCreateView() 方法中,可以爲當前的Fragment綁定佈局文件:

//這個方法要返回一個View
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
...
return rootView;

一些Fragment的操作一般都是通過

getFragmentManager()
        .beginTransaction()
        ...//這裏進行一些操作
        .commit();

例如,在一個id爲container的佈局中包含一個名爲BlandkFragment的fragment,可以通過add(R.id.container, new BlankFragment()) 來做,但是,在Fragment類中,有一段註釋值得注意

* Activities that contain this fragment must implement the
* {@link BlankFragment.OnFragmentInteractionListener} interface
* to handle interaction events.

如果要在Activity裏面包含Fragment,似乎是要實現這個Fragment的OnFragmentInteractionListener 否則會報錯

在一個fragment裏面進行一些操作的話,同樣是在onCreateView() 中做,就好像是在Activity的onCreate() 方法裏做一些操作一樣,同樣可以用findViewById() 來獲取當前Fragment佈局中的一些控件,給按鈕綁定事件、獲取文本框內容之類的。

視頻中有一個從Fragment1跳到Fragment2的操作,如果在Fragment1中有addToBackStack(null) 操作的話,回退鍵可以返回Fragment1,但是自己試的時候沒效果,查了一下
FragmentTransaction addToBackStack 無效問題

自己把Activity的父類改爲android.app.Activity ,後退操作就可以實現了。


Fragment 的生命週期

這裏寫圖片描述


Tabbed Activity

as可以自動創建帶滑動標籤的activity,利用了fragment和viewpager,需要三個類,這裏寫圖片描述

其中public class SectionsPagerAdapter extends FragmentPagerAdapter ,看一下這個類的源碼,

    /**
     * Return the Fragment associated with a specified position.
     */
    public abstract Fragment getItem(int position);
    /**
     * Return a unique identifier for the item at the given position.
     *
     * <p>The default implementation returns the given position.
     * Subclasses should override this method if the positions of items can change.</p>
     *
     * @param position Position within this adapter
     * @return Unique identifier for the item at position
     */
    public long getItemId(int position) {
        return position;
    }

在SectionsPagerAdapter中通過這個方法,返回不同的fragment實例

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1);
    }

通過下面這個方法指定顯式的tab頁數

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

在main佈局中指定一個ViewPager,id爲container,

mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

爲ViewPager設置Adapter,這裏的每一個都使用的是同一種fragment佈局樣式,也可以自己定義fragment,讓 每個fragment使用不同的fragment。
例如我想讓第二個tab用我自己定義的ImageFragment,那麼我只要修改SectionsPagerAdapter

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //如果position爲1,即第二個頁面,那麼讓它返回自定義的ImageFragment
        if(position==1){
            return new ImageFragment();
        }
        return PlaceholderFragment.newInstance(position + 1);
    }

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章