- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的 Activity
中有两个 Fragment
:带有按钮 X
的 fragment A
和 fragment B
带有按钮 Y
。
如何在单击按钮 B
时更改按钮 X
的背景图像?可能吗?
最佳答案
来自文档,
Because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
话虽这么说,你想要做的是create event callbacks to the activity .这样做的一个好方法是在 fragment 内定义一个回调接口(interface),并要求宿主 Activity 实现它。当 Activity 通过接口(interface)接收到回调时,它可以根据需要与布局中的其他 fragment 共享信息。这是在两个单独的 Fragment
之间共享事件的推荐方式,即通过 Activity 共享事件。
查看上面的链接...它提供了几个很好的示例。如果您仍然遇到问题,请告诉我,也许我可以更明确。
假设您单击了 fragment A
中的一个按钮,并且您希望这会导致 fragment B
中的按钮发生变化。下面是一些说明该概念的示例代码:
public interface OnButtonClickedListener {
public void onButtonClicked();
}
public class SampleActivity extends Activity implements OnButtonClickedListener {
/* Implementation goes here */
public void onButtonClicked() {
// This method is called from fragment A, and when it is called,
// it will send information to fragment B. Remember to first
// check to see if fragment B is non-null.
/* Make call to a method in fragment B that will update its display */
}
}
public class FragmentA extends Fragment {
OnButtonClickedListener mListener;
/* Implementation goes here */
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnButtonClickedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnButtonClickedListener ");
}
}
public void clickButton() {
// When the button is clicked, notify the activity.
// The activity will then pass the information to fragment
// B (if it has been created).
mListener.onButtonClicked();
}
}
现在,您可能想知道,“为什么会有人经历所有这些麻烦?当您可以让 fragment A
直接操作 fragment 时,创建一个单独的 Activity 回调方法有什么意义 B
?”
您要这样做的主要原因是确保每个 fragment 都设计为模块化和可重用的 Activity 组件。这一点尤其重要,因为模块化 fragment 允许您针对不同的屏幕尺寸更改 fragment 组合。在设计支持平板电脑和手机的应用程序时,您可以在不同的布局配置中重复使用您的 fragment ,以根据可用的屏幕空间优化用户体验。例如,在手机上,当同一 Activity 无法容纳多个 fragment 时,可能需要分离 fragment 以提供单 Pane UI。使用 Activity 回调可确保在屏幕上看不到 fragment B
的情况下,您可以轻松地重用 fragment 。例如,如果您在手持设备上并且没有足够的空间来显示 fragment B
,那么您可以轻松地检查您的 Activity 以查看 fragment B
当前是否显示在屏幕上。
抱歉,如果不清楚...我发现很难描述 :P。努力解决这个问题 tutorial可能会有所帮助...作为开发人员,当您使用交互式多 Pane 布局时, Activity 回调让您的生活变得特别轻松。
关于android - 如何从 fragment B访问 fragment A的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10695473/
我有一个像这样的 fragment 栈 F1 -> F2 -> F3 -> F4 -> F5 现在我需要删除 F2、F3、F4 fragment 。 我需要如果我从 F5 fragment 按下后退按
我需要帮助来理解以下场景的工作原理以及如何实现结果。 我有一个名为 ShoppingCart 的类。它有一个名为 addItemsToShoppingCartFromPreviousOrder 的方法
我有一个包含 ViewPager 的 fragment 。这个 ViewPager 显然是由其他 Fragments 填充的。 我的问题是,ViewPager 中的 Fragment 是否有任何方法(
所以我正在实现一个 FragmentActivity 并尝试添加一个 fragment ,但是我遇到了多个问题。我以前做过这个,实际上我使用的代码与上一个项目(它工作的地方)相同,但由于某种原因它在这
Closed. This question needs details or clarity。它当前不接受答案。
我想将 Android X fragment (androidx.fragment.app.Fragment) 转换为 Android native fragment (android.app.Fra
假设我有一个包含 10 列文本类型 (20) 的 SQLite 表。 ListFragment 将从数据库中提取 4 列并使用 SimpleCursorAdapter 显示在列表中。 选择后,List
我有一个对话框 fragment ,我为延迟初始化创建了一个类。当我显示对话框时,它显示正常。但是,当我关闭对话框时,它崩溃的原因是: fragment 与 fragment 管理器无关。 我也尝试过
我想在 View 分页器更改为 fragment 时刷新该 fragment 。 package com.mcivisoft.rcbeam; import android.os.Bundle; imp
我有一个名为的 Activity MainActivity 我在容器 R.id.mainContainer 中添加了 fragment “BenefitFragment”。 在 BenefitFrag
所以我有这个 ClientListView,效果很好,可以显示客户,我可以单击一个客户并在右侧获取他们的详细信息(在我的第二个 fragment 中)。由此处的布局定义: 这很好用,但后来我
我试过这段代码,但点击第一个 fragment 中的按钮并没有改变第二个 fragment 中的字符串值。 这是第一个 fragment 的 kotlin 文件。它有两个按钮,点击它们可以更改字符串值
我有以下情况:我打开 fragment A 和目标,通过按钮的单击事件转到 fragment B。当我在 fragment B 中并点击后退按钮(为了返回 fragment A) 我想将一些参数传递给
我制作了一个 NavigationDrawer fragment ,其中包含 Home、Settings、Feedback 等项目。 home 项在点击 home 时应该打开,home 是在应用程序启
我正在一个接一个地替换 2 个 fragment ,两个 fragment 都有不同的选项菜单。当我替换第二个 fragment 时,它也显示第一个 fragment 的菜单。 setHasOptio
我有问题: android.app.Fragment$InstantiationException: Unable to instantiate fragment ${packageName}.${a
我正在研究 fragment 转换。当我用第二个 fragment 替换第一个 fragment 时,它出现在第一个 fragment 的下方。我希望它移动到第一个 fragment 之上。我该怎么做
我在抽屉导航里总共有 12 个 fragment 。每个 fragment 都有 volley 方法。每个 fragment 都显示自己的 Volley 响应,除了 position = 1 和 po
我在我的 Activity 中使用了两个 fragment 。最初我将向 Activity 添加一个 fragment 。然后在第一个 fragment 中使用监听器我想用第二个 fragment 替
我正在实现一个“fragments-101”程序,当相应的按钮被点击时我会替换一个 fragment 。然而,下面的事情发生了: 为什么会这样?为什么初始 fragment 没有被完全替换?MainA
我是一名优秀的程序员,十分优秀!