当前位置:网站首页>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
边栏推荐
- 湖仓一体释放全量数据价值,SequoiaDB v5.2线上发布会重磅来袭
- Onemanager and cloudflare workers deployment and installation - binding domain names and using cloudflare CDN acceleration
- Virbox compiler, which supports source code encryption of the whole platform and architecture
- Modelarts, Pangu big model, ModelBox... Detailed explanation of Huawei cloud AI development production line
- Summer Niuke multi school 1:i chiitoitsu (expectation DP, inverse yuan)
- 拦截器和过滤器
- Calendar common methods
- [JVM learning 03] class loading and bytecode Technology
- Installation and use of lsky Pro lancong drawing bed: a drawing bed program for online uploading and managing pictures
- Pay close attention! List of the latest agenda of 2022 open atom open source Summit
猜你喜欢

On July 31, 2022, the dama-cdga/cdgp data governance certification class was opened!

Reading notes of XXL job source code

Tcl/tk file operation

Literature reading: gopose 3D human pose estimation using WiFi

Common methods of string class

多线程与并发编程常见问题(未完待续)

Convolutional Neural Networks in TensorFlow quizs on Coursera
思源笔记 v2.1.2 同步问题

2022 Hangdian multi school second session 1009 shuangq (Mathematics)

PostgreSQL weekly news - July 13, 2022
随机推荐
[face to face experience of school recruitment] 8 real questions of pointer interview. Come and test how many you have mastered.
How does PostgreSQL decide PG's backup strategy
Solve the problem of disconnection due to long idle time after SSH login
[JVM learning 04] JMM memory model
Cmake series tutorial 1- initial cmake
asp. Net core, C # summary about path
FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 1)
MySQL1
Pam4 popular science
2022 Hangzhou Electric Multi school first Dragon Slayer (dfs+ state compression)
拦截器和过滤器
OpenGL learning (IV) glut 3D image rendering
Convolutional neural network CNN
Cesium uses czml to implement dynamic routes
Day 5 (array)
profile环境切换
Decision tree_ ID3_ C4.5_ CART
Anaconda installs labelimg (super simple and available)
Biopharmaceutical safety, power supply and production guarantee
PostgreSQL Elementary / intermediate / advanced certification examination (7.16) passed the candidates' publicity