当前位置:网站首页>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
边栏推荐
- Explanation of pod DNS configuration & cases of DNS resolution failure
- Solution to the problem that kibana's map cannot render longitude and latitude coordinate data
- Zblog system realizes the tutorial of the number of articles published on the same day when the foreground calls
- [log service CLS] Tencent cloud game battle engine mgobe accesses CLS
- To redefine the storage architecture, Huawei has used more than five "cores"
- Tencent released "warehouse express" and issued "ID card" for each commodity!
- [2021 taac & Ti-One] FAQs related to preliminary round computing resources
- New MySQL 8.0 feature - enhanced logical backup recovery
- "Gambler" bubble Matt turns around
- The problem is as big as the middle stage
猜你喜欢
随机推荐
Introduction to visual studio shortcut keys and advanced gameplay
Analysis of software supply chain attack package preemption low cost phishing
When the game meets NFT, is it "chicken ribs" or "chicken legs"?
[version upgrade] Tencent cloud firewall version 2.1.0 was officially released!
2. Leveldb design principle -- LSM
NFT元宇宙源码搭建解析与介绍
[DB Bao 45] MySQL highly available mgr+consult architecture deployment
Future banks need to think about today's structure with tomorrow's thinking
Comparison of similarities and differences between easynvr video edge computing gateway and easynvr software versions
Dunhuang Research Institute and Tencent have launched a new strategic cooperation to take you around the digital new silk road with AI
Users of the Tiktok open platform are authorized to obtain the user's fan statistics and short video data
TVP experts talk about geese factory middleware: innovating forward and meeting the future
Go language GC implementation principle and source code analysis
[play Tencent cloud] experience and development of game multimedia engine (II)
What is the problem that the data is not displayed on the login web page after the configuration of the RTSP video security intelligent monitoring system easynvr is completed
Erc-20 Standard Specification
Memory alignment in golang
Contributed code to famous projects for the first time, a little nervous
zblog判断某个插件是否安装启用的内置函数代码
究竟有哪些劵商推荐?现在网上开户安全么?

