当前位置:网站首页>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
边栏推荐
- 5g brings opportunities and challenges. Are you ready to defend against DDoS?
- FPGA systematic learning notes serialization_ Day10 [sequential logic, competitive adventure, synchronous reset, asynchronous reset]
- Customizing security groups using BPF
- Development of block hash game guessing system (mature code)
- VBA Daniel used the nested loop
- H265/webvr video web page without plug-in player easyplayer Solution to the problem of cumulative delay of FLV video played by JS
- Swift array map/flatmap/compactmap/filter/reduce/chaining Usage Summary
- Elastic searchable snapshot function (frozen Tier 3)
- How to convert XML to HL7
- Live broadcast Preview - on April 1, I made an appointment with you to explore tcapulusdb with Tencent cloud
猜你喜欢
随机推荐
liver failure! My friend made a programming navigation website!
究竟有哪些劵商推荐?现在网上开户安全么?
Issue 003 how to detect whether a sticky positioned element is in a pinned state
Radiology: contralateral preoperative resting state MRI functional network integration is related to the surgical results of temporal lobe epilepsy
[play Tencent cloud] experience and development of game multimedia engine (II)
实现TypeScript运行时类型检查
MySQL learning -- table structure of SQL test questions
Ramda's little-known side
How to compile and debug go runtime source code
Cloud native monitoring configuration self built alertmanager to realize alarm
[2021 taac & Ti-One] frequently asked questions related to the notebook function
Analysis and introduction of NFT meta universe source code construction
2. Leveldb design principle -- LSM
As for IOT safety, 20 CSOs from major manufacturers say
Game business DDoS attack and defense confrontation case sharing
Implement typescript runtime type checking
Erc-721 Standard Specification
ClassNotFoundException v/s NoClassDefFoundError
How to build RTSP test URL in Intranet Environment
Introduction to visual studio shortcut keys and advanced gameplay

