1. 程式人生 > 其它 >4.1 簡單的Fragment例子

4.1 簡單的Fragment例子

左邊點選新聞標題,右邊顯示新聞內容。

最開始是為了平板的簡潔來使用。

現在手機也是,點選底部各個item就在上面顯示一個介面。

什麼是Fragment

1.具備生命週期

2.必須寄生在宿主activity中才能使用

Fragment就像一個小的activity,且在宿主activity活著的情況下他有自己獨立的個體生命

不同的Fragment可以執行在相同的activity之上,也可執行在不同的activity之上

一個簡單的fragment專案:

專案結構

右鍵新建一個Fragment 空的就行(blank)

fragment_blank.xml
<?xml version="1.0" encoding="utf-8"
?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".BlankFragment"> <!-- TODO: Update blank fragment layout
--> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_blank_fragment" /> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height
="wrap_content" android:layout_marginTop="40dp" android:text="你好嗎"/> </FrameLayout>
activity_main.xml(注意不寫id會報錯)
<?xml version="1.0" encoding="utf-8"?>
<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">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.myfragment.BlankFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


</LinearLayout>
BlankFragment.class
package com.example.myfragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import java.security.PrivateKey;


public class BlankFragment extends Fragment {

    private View root;
    private TextView textView;
    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (root == null){
            // Inflate the layout for this fragment
            root =  inflater.inflate(R.layout.fragment_blank, container, false);
        }
        textView = root.findViewById(R.id.textview1);
        button = root.findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText("我很好,謝謝");
            }
        });
        return root;
    }

}

MainAcitivity.class
package com.example.myfragment;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}