android - 如何在 ButterKnife 中绑定(bind) XML fragment ?

标签 android xml android-fragments butterknife

xml 中的 fragment

<fragment
    android:id="@+id/parent_fragment"
    android:name="com.app.example.ParentFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

在 Activity 中绑定(bind)

@BindView(R.id.parent_fragment)
ParentFragment parentFragment;

gradle 构建错误信息

@BindView fields must extend from View or be an interface

是否有类似@BindFragment 的东西用于使用@+id 绑定(bind)XML fragment ?

如果这是显而易见的事情,我很抱歉。

最佳答案

显然,那个库中没有这样的注释。

http://jakewharton.github.io/butterknife/

由于您的 Activity 中不会有很多 fragment ,因此可能没有必要使用库。只需使用 FragmentManager

的经典方法
parentFragment = (ParentFragment) getSupportFragmentManager().findFragmentById(R.id.parent_fragment);

关于android - 如何在 ButterKnife 中绑定(bind) XML fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47050414/

相关文章:

android - 如何知道用户已从应用程序中的 1 个位置单击任何 Activity 屏幕

xml - 关于根节点的简单 XPath 问题

android - 调用 popBackStack() 后当前 fragment 会发生什么情况?

java - 如何通过单击按钮滑动 fragment

android - 在 Xamarin.Android 上旋转屏幕/更改选项卡时如何保存选定的选项卡?

android - 如何对齐 ActionBar 中的菜单项

android - 检查所有 EditText 是否为空

python - 如何通过脚本从 XMI 正确导入一个完整的 Enterprise Architect 项目?

javascript - 安装后尝试设置时出现 react 导航错误 - 元素类型无效

java - Jaxb 应该编码内部类吗?