前言
學習Java和Android將近一年的時間了,期間的成果應該就是獨立完成了一個Android客戶端,并且保證了其在主線版本的穩定性。期間遇到了很多坑,也跟著師兄學到了很多Android知識。但是人總是要擁抱變化,不能讓自己太安逸,雖然有不舍,但是我已經證明了自己的學習能力,下一步就是開始做Rom Porting了。這里總結一下之前項目中用到最多的Fragment。
Fragment簡介
Fragment可以理解成Activity中用戶界面的一個行為或者一部分,它必須被嵌套在Activity中。但是一個Fragment有它自己獨立的xml布局文件,并且具有良好的封裝性,因此特殊情況下Fragment可以很容易用Activity來進行替換。
創建Fragment
創建一個Fragment和創建Activity類似,需要實現XML布局文件和Java Class。
XML布局文件和其他布局文件都一樣,例如如下所示的布局文件(fragment_layout.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/testview" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> </LinearLayout>
Java代碼中,一般情況下可以根據需要實現Fragment以下幾個生命周期方法:
1. onAttach():當Fragment依附于activity時被調用,可以在該方法中獲取activity句柄,從而實現Fragment和activity之間的通信。
2. onCreate():對Fragment做初始化。
3. onCreateView():在第一次為Fragment繪制用戶界面時系統會調用此方法。
4. onActivityCreated():在宿主Activity onCreate函數執行完成之后被調用,可以在這個方法里進行Fragment自己的widget實例化和業務邏輯處理。
5. onDestoryView():當Fragment開始被銷毀時調用。
6. onStart():當Fragment可見時被調用。
還有許多其他用以操縱Fragment生命周期中各個階段的回調函數,大家可自行Google學習。
Fragment生命周期
每一個Fragment都有自己的一套生命周期回調方法和處理自己的用戶輸入事件。對應的生命周期如下圖所示:
在Activity中加入Fragment
首先,需要確保Acitivity支持Fragment,因此Activity通常需要繼承自FragmentActivity。在Activity中添加Fragment通常有兩種方法:靜態的和動態的。
靜態方法
直接在Activity的XML布局文件中加入Fragment,如下所示:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <fragment android:id="@+id/first" android:name="com.example.FristFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/second" android:name="com.example.SecondFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
<fragment>中的android:name 屬性指定了布局中實例化的fragment類
當系統創建Activity布局時,它實例化了布局文件中指定的每一個Fragment,并且為它們調用onCreateView()函數,以獲取每一個fragment的布局。系統直接在<fragment>元素位置插入fragment返回的view
注意:每個fragment都需要一個唯一的標識,如果重啟activity,系統可用來恢復fragment(并且用來捕捉fragment的事務處理,例如移除)。為了Fragment提供ID有三種方法:
動態方法
使用FragmentTranscation??梢允褂肍ragmentTranscation的API來對Activity的Fragment進行操作(例如添加,移除,或者替換Fragment)。參考代碼如下:
FragmentManager fragmentManager = getFragmentManager() FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit();
傳入add()函數的第一個參數是Fragment被放置的ViewGroup,它由資源ID(resource ID)指定,第二個參數就是要添加的fragment。一旦通過FragmentTranscation做了更改,都應當使用commit()視變化生效。
Fragments通信
Fragments之間不應該直接進行通信,它們之間的交互應該通過宿主Activity進行。有三種Fragment和Acitivity交互的方法:
1. Activity創建帶參數的Fragment。
2. Activity中保持了Fragment的對象句柄,可通過句柄直接調用該Fragment的public方法。
3. Fragment可在onAttach函數中獲取定義的listener句柄。
創建帶參數的Fragment
在某些特定的情況下,Fragment可能需要特定的參數來進行初始化。由于Fragment必須只有一個無參構造函數,因此可以考慮使用靜態的newInstance方法來創建帶參數的Fragment。示例代碼如下:
import android.os.Bundle; import android.support.v4.app.Fragment; public class TestFragment extends Fragment { public static TestFragment newInstance(int num, String title) { TestFragment fragment = new TestFragment(); Bundle args = new Bundle(); args.putInt("num", num); args.putString("title", title); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int num = getArguments().getInt("num", 0); String title = getArguments().getString("title", ""); } }
你可以在Activity里,簡單的加載一個帶參數的Fragment:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); TestFragment fragment = TestFragment.newInstance(5, "fragment title"); ft.replace(R.id.placeholder, fragment); ft.commit();
調用Fragment的方法
因為Activity可以獲取嵌入的Fragment的句柄,因此可以直接通過Fragment句柄調用該方法。
public class TestFragment extends Fragment { public void doSomething(String param) { // do something in fragment } }
在Activity中,可以直接通過Fragment的對象句柄調用該方法:
public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TestFragment testFragment = new TestFragment(); testFragment.doSomething("some param"); } }
Fragment Listener
如果Fragment需要共享事件給Activity,則需要利用這個方法。Fragment中定義一個接口,并且由Activity來實現這個接口。在onAttach()方法中將實現了這個接口的Activity獲得到。
在Fragment中定義接口代碼如下:
import android.support.v4.app.Fragment; public class MyListFragment extends Fragment { // ... // Define the listener of the interface type // listener is the activity itself private OnItemSelectedListener listener; // Define the events that the fragment will use to communicate public interface OnItemSelectedListener { public void onRssItemSelected(String link); } // Store the listener (activity) that will have events fired once the fragment is attached @Override public void onAttach(Activity activity) { super.onAttach(activity); if (activity instanceof OnItemSelectedListener) { listener = (OnItemSelectedListener) activity; } else { throw new ClassCastException(activity.toString() + " must implement MyListFragment.OnItemSelectedListener"); } } // Now we can fire the event when the user selects something in the fragment public void onSomeClick(View v) { listener.onRssItemSelected("some link"); } }
在Activity中實現這個接口:
import android.support.v4.app.FragmentActivity; public class RssfeedActivity extends FragmentActivity implements MyListFragment.OnItemSelectedListener { DetailFragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rssfeed); fragment = (DetailFragment) getSupportFragmentManager() .findFragmentById(R.id.detailFragment); } // Now we can define the action to take in the activity when the fragment event fires @Override public void onRssItemSelected(String link) { if (fragment != null && fragment.isInLayout()) { fragment.setText(link); } } }
新聞熱點
疑難解答