天天看點

Fragment碎片

1.靜态加載fragment

package com.example.shinelon.diyfragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//自定義一個類繼承Fragment,繼承這個android.app.Fragment,v4那個已經被淘汰,詳情見菜鳥教程,及fragment的作用
public class Demo2 extends Fragment{
    @Nullable
    //重寫onCreate方法
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //用inflate加載布局頁面,布局頁面命名規則 fragment_xxxx
        View view = inflater.inflate(R.layout.fragment2, null);
        return view;
    }
}
           

在activity_main布局中加載fragment

name屬性值目前所要加載的fragment的全類名

标簽全小寫

<fragment
        android:name="com.example.shinelon.diyfragment.Demo3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></fragment>
           

2.動态加載fragment(橫豎屏切換加載不同的頁面)

Fragment碎片
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //擷取手機螢幕
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        //擷取fragment管理者
        android.app.FragmentManager manager = getFragmentManager();
        //管理者開啟事務
        // 開啟一個事物,事物是執行一段邏輯,要麼同時成功,要麼同時失敗
        FragmentTransaction transaction = manager.beginTransaction();

        if(height>width){
            //加載之前的寫好的頁面
            transaction.replace(R.id.LL,new Demo1());
        }else {
            transaction.replace(R.id.LL,new Demo2());
        }
        //送出一個事務,業務邏輯完成後一定要送出(commit)事務
        transaction.commit();
    }
}
           

activity_main中

<LinearLayout
    android:id="@+id/LL"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"></LinearLayout>
           
Fragment碎片

3.Fragment與Activity的互動

左邊的Button是一個fragment,右邊的TextView在Activity上

Fragment碎片

fragment_layout

簡單建立一個布局,上面隻寫了一個Button

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="button"/>
</LinearLayout>
           

Fragment

/和之前的操作一樣,先建立一個類繼承app.Fragment,用inflate加載剛才寫的布局
public class Fragment extends android.app.Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, null);
        //設定Button的監聽,用匿名内部類的方法
        // 這裡的view應該相當于上下文對象,了解的不是很好
        view.findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //用getActivity方法,得到Activity activity = getActivity();
                //指定是MainActivity,然後進行強制轉換
                MainActivity m = (MainActivity) getActivity();
                m.update("123456");
            }
        });
        return view;
    }
}
           

activity_main

主布局檔案中,寫了一個線性布局,用來放Fragment

<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="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/LL"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"></LinearLayout>

    <TextView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hello Lenovo" />
    
</LinearLayout>
           

MainActivity

public class MainActivity extends AppCompatActivity {
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.LL,new Fragment());
        transaction.commit();
    }
    //将修改文字封裝成一個方法,然後在Button的點選事件中,直接調用這個方法
    public void update(String s){
        tv.setText(s);
    }
}
           
Fragment碎片

4.Fragment與Fragment之間的資料互傳

效果與之前類似,不再做示範

Fragment2

//操作與上一個案例相同,布局檔案也很簡單,就是一個TextView
public class Fragment2 extends Fragment{
    @Nullable
    TextView tv;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2_layout, null);
        tv = view.findViewById(R.id.tv);
        return view;
    }
    public void setData(String s){
        tv.setText(s);
    }
}
           

Fragment1

public class Fragment1 extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1_layout, null);
        view.findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity mactivity = (MainActivity) getActivity();
                //這裡用一個getFragmentManger方法,得到manger對象
                FragmentManager manager = mactivity.getFragmentManager();
                //用manger找到這個取名為"f2"的fragment
                //和上一個案例不同之處
                Fragment2 f2 = (Fragment2) manager.findFragmentByTag("f2");
                f2.setData("123654");
            }
        });
        return view;
    }
}
           

activity_main

主布局檔案中就放兩個LineLayout

<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"
    android:orientation="horizontal">

    <LinearLayout
    //設定id,必須要設定orientation的方向
       android:id="@+id/LL1"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"></LinearLayout>

    <LinearLayout
        android:id="@+id/LL2"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"></LinearLayout>

</LinearLayout>
           

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager f1 = getFragmentManager();
        FragmentTransaction t1 = f1.beginTransaction();
        //和上一個不同之處,在後面加了一個自取的fragment名字
        //用于前面FragmentManager找到這個取名的fragment
        t1.replace(R.id.LL1,new Fragment1(),"f1");
        t1.commit();

        FragmentManager f2 = getFragmentManager();
        FragmentTransaction t2 = f2.beginTransaction();
        t2.replace(R.id.LL2,new Fragment2(),"f2");
        t2.commit();
    }
}
           
Fragment碎片

繼續閱讀