当前位置:网站首页>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
边栏推荐
- Integer
- Summary of articles in 2020
- Modelarts, Pangu big model, ModelBox... Detailed explanation of Huawei cloud AI development production line
- [resolved] CVC datatype valid. 1.2.1: '' is not a valid value for 'ncname'.
- Tcl/tk grouping and replacement rules
- PCI express physical layer - electrical part
- Sequences, time series and prediction in tessorflow quizs on coursera (I)
- Colon sorting code implementation
- Mysql database, subquery, union, limit
- Analysis of the basic concept of digital warehouse
猜你喜欢

湖仓一体释放全量数据价值,SequoiaDB v5.2线上发布会重磅来袭

FPGA 20个例程篇:9.DDR3内存颗粒初始化写入并通过RS232读取(下)

LTSpice software power settings

Clion configuring WSL tool chain

多线程与并发编程常见问题(未完待续)
![[laser principle and application -6]:q switching element and Q drive circuit board](/img/30/e199b73fb9b0ad335f26f2378cfc45.png)
[laser principle and application -6]:q switching element and Q drive circuit board

FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 1)

Emergency lighting design of large stadiums and gymnasiums

OPENGL学习(二)OPENGL渲染管线

OpenGL learning (II) opengl rendering pipeline
随机推荐
LTSpice software power settings
Install SSL Certificate in Litespeed web server
Emergency lighting design of large stadiums and gymnasiums
Literature reading: gopose 3D human pose estimation using WiFi
Tencent Browser service TBS usage
MySQL1
Detailed explanation of the relationship between MySQL tables
Those gods on Zhihu reply
Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack
原反补及大小端
Integer
Reading notes: you only look once:unified, real time object detection
OpenGL learning (V) modern OpenGL triangle rendering
Ensure the health and safety of front-line construction personnel, and implement wrong time construction at Shenzhen construction site
Wireshark simple filter rule
Day 6 (array example)
Tcl/tk file operation
Pam4 popular science
【JVM学习03】类加载与字节码技术
Free and open source website navigation source code collection, sorting and summary - self built personal navigation Homepage