碎片是什麼
碎片的使用方式
初步接觸碎片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"
/>
</LinearLayout>
佈局文件中只放了一個水平居中的按鈕button。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#00FF00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment"
/>
</LinearLayout>
佈局文件中只有一個TextView,顯示一句話“This is right fragment”,然後背景顏色是綠色。import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Deiatg on 2017/3/4.
*/
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//能動態地加載佈局的函數inflate()
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}
這裏僅僅重寫了Fragment類中的onCreateView()方法,這個方法的作用就是使用LayoutInflater的inflate()類將剛纔定義好的left_fragment.xml動態加載進來。用同樣的方法創建一份RightFragment類,代碼如下:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Deiatg on 2017/3/4.
*/
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//能動態地加載佈局的函數inflate()
View view = inflater.inflate(R.layout.right_fragment, container, false);
return view;
}
}
代碼基本相同,我們繼續。<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:name="com.open_open.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/right_fragment"
android:name="com.open_open.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
代碼將兩個碎片一左一右放入主佈局中,注意的是要將fragment的完整包名放在android:name中。
動態添加碎片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFF00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is another right fragment"/>
</LinearLayout>
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Deiatg on 2017/3/4.
*/
public class AnotherRightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//能動態地加載佈局的函數inflate()
View view = inflater.inflate(R.layout.another_right_fragment, container, false);
return view;
}
}
代碼同樣很簡單,在onCreateView()方法中加載剛纔創建好的another_right_fragment.xml文件。這就準備好了另一個碎片,之後我們看一下如何將其動態地添加到活動中。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/left_fragment"
android:name="com.open_open.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
repalceFragment(new RightFragment());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
repalceFragment(new AnotherRightFragment());
break;
default:
break;
}
}
private void repalceFragment (Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.commit();
}
}
代碼中MainActivity類實現了OnClickListener接口,這裏可以參考我的另一篇文章:鏈接 。代碼中首先我們給左側碎片中的button按鈕註冊了一個點擊事件,只有點擊這個按鈕就會調用我們自定義的方法repalceFragment(),這個方法能夠當前佈局替換一個新的碎片。點擊按鈕將右側碎片換成另一個碎片。結合replaeFragment()方法的代碼可以看到,動態添加碎片分爲五步。在碎片中模擬返回棧
也就是當有多個碎片都被動態添加,我們在調用了它們之後不想按返回鍵直接退出程序,而是逐個返回碎片,最後退出程序。想達到這種類似棧的返回操作其實很簡單,FragmentTransaction中提供了一個addToBackStack()方法,可以用於講一個事務添加到返回站中,修改代碼:public class MainActivity extends AppCompatActivity implements View.OnClickListener{
……
private void repalceFragment (Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
我們在事務提交之前調用了FragmentTransaction的addToBackStack()方法,它可以接收一個名字用於描述返回棧的狀態,一般傳入null即可。重新運行程序,我們發現,在碎片中模擬返回棧這個功能我們完成了。