当前位置:网站首页>Data transmission of different fragments in the same activity
Data transmission of different fragments in the same activity
2022-07-24 19:23:00 【Martin-Rayman】
Reference article :http://blog.csdn.net/harvic880925/article/details/44966913
Recently, such a requirement needs to be implemented in the project :
The same Activity There are two Fragment, Then there are two Fragment. two Fragment There are different ListView.
Then realize from Fragment1 Medium ListView Click on item Then add to Fragment2 Of ListView in
Let's first understand the situation .
About Fragment Data transfer , There are actually two situations
Situation 1 :Fragment Between show and hide is to use replace To achieve the
Situation two :Fragment Between show and hide is to use hide and show Method
Everyone knows this , See your project needs specifically .
There are different solutions for specific situations ,
For case one , We can use fragment.setArgument() Method to transfer data ( Refer to the great God's blog)
Let's focus on the second case , adopt Interface callback Method to realize data transmission
Literature reference :http://www.cnblogs.com/kissazi2/p/3440257.html
Steps of setting interface callback :
1.Fragment1 Define an interface in
public interface onDataChangedListener {
void onDataChanged(HarvestBean bean); // Parameters are the data content that users need to pass , Here I use a Bean object
}2. At the same time Fragment1 A method in instantiates the interface
onDataChangedListener listener = (onDataChangedListener) getActivity();
listener.onDataChanged((HarvestBean) msg.obj);PS: Here I am directly introducing a Bean Object's , Because I'm here onDataChanged(HarvestBean bean); The parameters set in the method are Bean 3. In the father Activity Implement the interface in and rewrite onDataChanged(); Method , At the same time, by calling Fragment2 Of updateData(); Method update Fragment2 The data of
@Override
public void onDataChanged(HarvestBean bean) {
if (expressFragment != null) {
expressFragment.updateData(bean);
} else {
Log.i("harvest", " It's empty ?");
}
}4. stay Fragment2 Create a public method in updateData(HarvestBean bean) Method is used to update Fragment2 in LitView The data of
List<HarvestBean> data = new ArrayList<>();public void updateData(HarvestBean harvestBean) {
bean = harvestBean;
data.add(bean);
mLv_express.setAdapter(new ExpressAdapter(getActivity(), data));
}You are hungry , I want to eat , Just ask your mother later " There's no dinner ?" This is the normal function call .
among , You told your mother to call you on her cell phone , It's an action where you save the callback function handle to your mother . Your mother called you , It's a callback process .
from :http://hi.baidu.com/%CE%E2_%F0%A9/blog/item/eec507cf7e72d20f92457eb2.html
边栏推荐
- mysql排序.按字段值排序
- FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)
- Meshlab & PCL ISS key points
- 【无标题】
- Configmanager of unity framework [JSON configuration file reading and writing]
- Leetcode652 finding duplicate subtrees
- Mysql database, de duplication, connection
- 2022 Hangzhou Electric Multi school first Dragon Slayer (dfs+ state compression)
- Ebpf verifier
- [laser principle and application -6]:q switching element and Q drive circuit board
猜你喜欢
随机推荐
Hold the C pointer
Reading notes: you only look once:unified, real time object detection
pyhanlp安装教程
OPENGL学习(二)OPENGL渲染管线
On dynamic application of binary array
FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(上)
Tupu software digital twin civil aviation flight networking, building a new business form of smart Civil Aviation
拦截器和过滤器
JS part
Biopharmaceutical safety, power supply and production guarantee
Summary of articles in 2020
Ebpf verifier
First knowledge database
Onemanager and cloudflare workers deployment and installation - binding domain names and using cloudflare CDN acceleration
Compressed string
On July 31, 2022, the dama-cdga/cdgp data governance certification class was opened!
OpenGL learning (V) modern OpenGL triangle rendering
[untitled]
思源笔记 v2.1.2 同步问题
Nezha monitoring - server status monitoring, SSL certificate change expiration, Ping monitoring and scheduled task reminder





