天天看點

fragment簡單使用

學習導航

第一節 fragment基礎知識學習 :http://blog.csdn.net/bobo8945510/article/details/52788994

第三節 fragment的增删改學習 :http://blog.csdn.net/bobo8945510/article/details/52806916

第四節 fragment+viewpager:http://blog.csdn.net/bobo8945510/article/details/52821741

Fragment學習——fragment簡單實作

實作一個簡單的例子,流程如下:

一、添加兩個類繼承Fragment,并且為對應的fragment建立對應的xml布局。fragment01代碼如下:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by ENZ on 2016/10/11.
 */

public class Fragment01 extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //引用建立好的xml布局
        View view = inflater.inflate(R.layout.fragment01,container,false);
        return view;

    }
}
           
  • 他對應的布局
<?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"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="50dp"
        android:text="我是第一頁">
    </TextView>
</LinearLayout>
           
  • 布局效果:
    fragment簡單使用

二、那如何添加到activity中呢?看下面代碼,我注釋的很清楚。

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button button01,button02;
    private Fragment fragment01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setview();
    }
    private void setview() {
        button01 = (Button)findViewById(R.id.button01);
        button01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //擷取到FragmentManager,在V4包中通過getSupportFragmentManager,
                //在系統中原生的Fragment是通過getFragmentManager獲得的。
                FragmentManager FM = getFragmentManager();
                //2.開啟一個事務,通過調用beginTransaction方法開啟。
                FragmentTransaction MfragmentTransaction =FM.beginTransaction();
                //把自己建立好的fragment建立一個對象
                Fragment01  f1 = new Fragment01();
                //向容器内加入Fragment,一般使用add或者replace方法實作,需要傳入容器的id和Fragment的執行個體。
                MfragmentTransaction.add(R.id.fragment_buju,f1);
                //送出事務,調用commit方法送出。
                MfragmentTransaction.commit();
            }
        });
        button02 = (Button)findViewById(R.id.button02);
        button02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //擷取到FragmentManager,在V4包中通過getSupportFragmentManager,
                //在系統中原生的Fragment是通過getFragmentManager獲得的。
                FragmentManager FMs = getFragmentManager();
                //2.開啟一個事務,通過調用beginTransaction方法開啟。
                FragmentTransaction MfragmentTransactions = FMs.beginTransaction();
                //把自己建立好的fragment建立一個對象
                Fragment02 f2 = new Fragment02();
                //向容器内加入Fragment,一般使用add或者replace方法實作,需要傳入容器的id和Fragment的執行個體。
                MfragmentTransactions.add(R.id.fragment_buju,f2);
                //送出事務,調用commit方法送出。
                MfragmentTransactions.commit();
            }
        });
    }
}

           

上面代碼中用到了幾個重要的知識點,我在上一篇部落格有講到

  • 其實就用到了下面的幾行代碼,就可以把一個fragment添加到Activity中。
FragmentManager FMs = getFragmentManager();
FragmentTransaction MfragmentTransactions = FMs.beginTransaction();
Fragment02 f2 = new Fragment02();
MfragmentTransactions.add(R.id.fragment_buju,f2);
MfragmentTransactions.commit();
           

1、首先你需要了解getFragmentManager()是擷取的什麼,他的意思是什麼?

2、通過獲得的FMS對象,開啟一個beginTransaction()事務,但是FragmentTransaction什麼含義呢?

3、MfragmentTransactions.add()方法,添加fragment對象。

4、commit()方法的意思是什麼?

5、請先了解fragment初識篇章:http://blog.csdn.net/bobo8945510/article/details/52788994

注意:很多初學者到這一步就會遇到一個問題,就是下面這樣,添加兩個fragment,造成了重疊,原因就是,兩個fragment布局效果都布置到了主布局中造成的,效果如下

fragment簡單使用

怎麼解決這個問題呢?其實有兩個方法來解決!

第一種: (建議用此方法,簡單)

隻需要把 add()方法改為replace()即可,他的意思就是如果主布局已經被某個fragment使用過了,再有新的fragment布局,就會把老的fragment布局替換掉。
fragment簡單使用

第二種:

判斷主布局是否被使用,在添加fragment時下面代碼
if(FMs.findFragmentById(R.id.fragment_buju)==null){
        //如果沒有使用過
        MfragmentTransactions.add(R.id.fragment_buju,f2);
  }else {
       //如果有使用過,就把老的fragment替換掉。
        MfragmentTransactions.replace(R.id.fragment_buju,f2);
  }
           

效果圖如下:

fragment簡單使用

demo位址,http://download.csdn.net/detail/bobo8945510/9651004

代碼中我使用的是第二種方法,建議用第一種!