当前位置:网站首页>Fragment usage

Fragment usage

2022-06-24 17:27:00 Android Trainee

1. Dynamic addition :

establish Fragment Of java Document and xml file

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

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

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class FragmentA extends Fragment {

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container, false);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv = view.findViewById(R.id.tv);
        EventBus.getDefault().register(this);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setEvent(MessageEvent messageEvent) {
        tv.setText(messageEvent.getMessage());

    }


}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="222dp"
    tools:context=".FragmentA">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="HELLOW" />

</LinearLayout>

The layout document of the main business face is Fragment Leave a place

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btSend"
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" Send a message "
       />
// A place reserved for fragments 
    <LinearLayout
        android:id="@+id/lyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:ignore="Orientation" />


</LinearLayout>

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;


import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;


public class MainActivity extends AppCompatActivity {
FragmentA fragmentA;
Button btSend ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btSend = findViewById(R.id.btSend);
        fragmentA = new FragmentA();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // Add fragments to the layout 
        transaction.add(R.id.lyFragment, fragmentA);
        transaction.commit();
        btSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new MessageEvent(" Received received received "));
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

2. Static add

Add... In the activity of fragment attachment fragment label ( Need to introduce dependency )

 implementation 'androidx.fragment:fragment:1.3.2'

xml file

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button
        android:id="@+id/btSend"
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=" Send a message "
       />
    <fragment
        android:id="@+id/lyFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.myjava01.FragmentA"
        tools:ignore="Orientation" />


</LinearLayout>

Fragment The document remains unchanged

原网站

版权声明
本文为[Android Trainee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211556099736.html