最近在學Android,在學習的過程中多少總是遇到很多問題,我希望後面學習的朋友能少走點彎路。
本節講的是Fragmnet篇。本文由簡單到一般。
導入fragment的時候會有兩個包,記得選v4的,因爲v4的兼容性很好。
一、靜態添加Fragment
1、實現一個Activity中顯示兩個平分的Fragment。
a、創建兩個新的Activity:left_fragment和right_fragment,同時會自動生成相應的兩個XML佈局。
b、分別使得這兩個Fragment繼承Fragment類。
c、重載Fragment中的onCreateView()方法
d、在主佈局中添加fragment組件
代碼開始:
activity_left_fragment.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="@android:color/holo_red_dark"
android:layout_height="match_parent"
tools:context=".left_fragment">
</LinearLayout>
activity_right_fragment.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context=".right_fragment">
</LinearLayout>
很顯然,我只是讓兩個Fragment顯示不同的顏色~
Java代碼:
left_fragment.java:
public class left_fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_left_fragment,null);
return view;
}
}
right_fragment.java:
public class left_fragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_right_fragment,null);
return view;
}
}
Activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/left"
android:layout_width="match_parent"
android:name="com.baidu.sql002.fragment004.left_fragment"
android:layout_weight="1"
android:layout_height="match_parent"/>
<fragment
android:id="@+id/right"
android:layout_width="match_parent"
android:layout_weight="1"
android:name="com.baidu.sql002.fragment004.right_fragment"
android:layout_height="match_parent"/>
</LinearLayout>
在主佈局中只是添加了兩個碎片而已,重點是控件的id和name屬性,name用來指定是哪個佈局,指向的是碎片的java文件噢~請注意,採用這種方法添加的,必須要添加ID,不然會報錯!
運行結果:
第一個碎片就出來啦~
下面再來一個動態添加碎片的,這種方式更加的靈活~
二、動態添加碎片
首先在主佈局中刪除一個fragment,並創建一個幀佈局管理器(FrameLayout),並且設置一下這個的ID:
<fragment
android:id="@+id/left"
android:layout_width="match_parent"
android:name="com.baidu.sql002.fragment004.left_fragment"
android:layout_weight="1"
android:layout_height="match_parent"/>
<FrameLayout
android:id="@+id/framelayout1"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>
在Main_activity.java中添加:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取FragmentManager
FragmentManager fragmentManager = getSupportFragmentManager();
// 開啓一個事務
FragmentTransaction transaction = fragmentManager.beginTransaction();
// 將佈局添加到相應位置,這裏是添加到R.id.framelayout中
transaction.add(R.id.framelayout1,new right_fragment());
// 提交事務
transaction.commit();
}
運行走你,結果和上圖一樣的就說明沒問題啦~
那麼接下來我們就能實現一個簡單的按鈕點擊就變更碎片的方法啦:
在activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:text="刷新"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/framelayout1"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
就是添加了一個按鈕,去掉了一個fragment的組件,只保留了一個FrameLayout。
在MainActivity中添加:
public class MainActivity extends AppCompatActivity {
// 這裏定義一個當前的碎片的模式
private int METHOD = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 切換功能
if(METHOD ==0){
METHOD = 1;
MyFragment(new right_fragment());
}else{
MyFragment(new left_fragment());
METHOD = 0;
}
}
});
}
// 簡單的封裝了一個添加碎片的方法。
private void MyFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.framelayout1,fragment);
transaction.commit();
}
}
其實到這裏,你就可以開發一個簡單的Tab頁切換了。
我來更新啦~
不知道大家注意到沒有,我們使用的是add()方法,字面意思就是添加嘛,現在看起來好像沒啥大問題,大家可以稍微修改一下代碼就知道了,每使用一次add其實都會在容器內添加一個Fragment。
解決辦法也很簡單:把add換成replace就可以啦~
我簡單的封裝一下吧~
private void set_fragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 是將第二個參數的碎片放到第一個裏面。這裏我是設置了一個LinearLayout
fragmentTransaction.replace(R.id.linefragment,fragment);
fragmentTransaction.commit();
}