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);

    }
}

 

 

 

 

posted @ 2021-09-07 09:26  涂妖教  阅读(193)  评论(0)    收藏  举报