当前位置:网站首页>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
边栏推荐
- Implement typescript runtime type checking
- What is the reason for the worse website SEO ranking?
- Tiktok Kwai, e-commerce enters the same river
- Easycvr, an urban intelligent video monitoring image analysis platform, plays national standard equipment videos and captures unstable packets for troubleshooting
- [log service CLS] Tencent cloud game battle engine mgobe accesses CLS
- How to troubleshoot and solve the problem that the ultra-low delay security live broadcast system webrtc client plays no audio in the browser?
- [2021 taac & Ti-One] frequently asked questions related to the notebook function
- Classic examples of C language 100
- Can yangjianyun's new media operation in 2021 bear all the expectations of the enterprise's private domain traffic demand?
- Best practices for H5 page adaptation and wechat default font size
猜你喜欢
随机推荐
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
test
Erc-721 Standard Specification
Yupi made an AI programming nickname generator!
"Gambler" bubble Matt turns around
AFG EDI requirements details
Let ups "Impressionist users" re understand reliability
Tensor and tensor network background and significance - basic knowledge
Cloud development environment to create a five-star development experience
How much does the page length affect the ranking?
Devops in digital transformation digital risk
Snapshot management for elastic cloud enterprise
Research on clock synchronization performance monitoring system based on 1588v2 Technology
问题有多大,中台就有多大
IBM: supporting AI and enterprise digital reshaping in the cloud era with modern architecture
重新定义存储架构,华为用了不止5颗“芯”
构建跨公链平台解决DApp开发问题
Live broadcast Preview - on April 1, I made an appointment with you to explore tcapulusdb with Tencent cloud
实现TypeScript运行时类型检查

