【问题标题】:how to get a fragment added in an XML layout如何在 XML 布局中添加片段
【发布时间】:2012-12-04 09:03:59
【问题描述】:

我有一个包含如下片段的布局:

<fragment
        android:id="@+id/mainImagesList"
        android:name="com.guc.project.ImagesList"
        android:layout_width="match_parent"
        android:layout_height="62dp"
        android:layout_below="@+id/addimagebutton"
        android:layout_weight="1"
        android:paddingTop="55dp" />

现在,我需要获取这个片段并对其进行投射,以便我可以对其进行操作并显示更新。我该怎么做?!

编辑:我想我已经设法获得了片段,但是当我更改一些变量时,更改不会出现!

【问题讨论】:

  • 如果未出现更改,请尝试在视图上调用 invalidate() 以重新绘制任何更改。

标签: android fragment


【解决方案1】:

可以通过如下方式获取片段实例:

getSupportFragmentManager().findFragmentById(R.id.yourFragmentId)

【讨论】:

    【解决方案2】:

    如果片段嵌入到另一个片段中,则需要getChildFragmentManager() 而不是getFragmentManager()。 例如,在布局 xml 中这样定义片段:

    <fragment 
        android:name="com.aventlabs.ChatFragment"
        android:id="@+id/chatfragment"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" />
    

    在代码中,你可以这样获取片段实例:

    FragmentManager f = getChildFragmentManager();
    FragmentTransaction transaction = f.beginTransaction();
    chatFragment = f.findFragmentById(R.id.chatfragment);
    
    if (chatFragment != null) {
       transaction.hide(chatFragment);
    }
    transaction.commit();
    

    【讨论】:

    • getChildFragmentManager() 就像我嵌套了片段一样,谢谢兄弟
    • 我的 Fragment 嵌入在父 Fragment 加载的 xml 布局文件中。 getChildFragmentManager 为我工作,而 getFragmentManager 没有。谢谢!
    【解决方案3】:

    我在 android 中做了完全一样的事情,而最简单的方法是使用接口来做到这一点。我有一个包含 6 个片段的活动,我只需要更新其中的 3 个。

    我用这个

        final Integer numeroFragments = ((PagerAdapterOfe) mViewPager.getAdapter()).getCount();
    
        for (int i=0; i<numeroFragments; i++) {
            Object fragment = ((PagerAdapterOfe) mViewPager.getAdapter()).getItem(i);
    
            // If the fragment implement my interface, update the list
            if (fragment instanceof IOfertaFragment){
            ((IOfertaFragment) fragment).actualizaListaOfertas();
            }
        }
    

    其中,PageAdapterOfe 是我的活动片段适配器。我循环我的所有片段并搜索那些实现我的接口的片段,当我找到一个时,我执行我的接口定义的方法,那就是!

    我在包含所有片段的 Activity 中使用此代码,作为对广播信号的响应,您可以将其放在您需要的地方。

    界面:

    public interface IOfertaFragment {
    
        public void actualizaListaOfertas();
    }
    

    【讨论】:

    • 这可行,但为了安全起见,您应该使用片段标签而不是循环。
    【解决方案4】:

    您可以使用 findFragmentById(如果您知道它包含在哪个组件中)或通过 findFragmentByTag(如果您知道它的标签)找到片段

    我不知道您要更新哪些变量,但您可以使用 FragmentTransaction API 将片段替换为另一个片段。

    有关示例,请参阅 http://developer.android.com/guide/components/fragments.html

    【讨论】:

    • 如果片段没有在布局中绘制,而只是在 xml 中使用片段标签声明,替换片段将不起作用。
    【解决方案5】:

    如果 Fragment 包含在 Activity 的布局文件中,那么它可以被 SupportFragmentManager 引用,例如...

    MyFragment myFragment= (MyFragment)getSupportFragmentManager().findFragmentById(R.id.FRAGMENTID)
    

    如果 Fragment 包含在另一个 Fragment 的布局文件中,那么它可以被 ChildFragmentManager 引用,例如...

     MyFragment myFragment= (MyFragment)getChildFragmentManager().findFragmentById(R.id.FRAGMENTID)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      • 2012-03-16
      相关资源
      最近更新 更多