how to get a fragment added in an XML layout

前端 未结 5 1485
暗喜
暗喜 2020-12-05 12:27

I have a layout which includes a fragment as follows:



        
相关标签:
5条回答
  • 2020-12-05 13:01

    I did exactly the same in android and the simplest way to do this in using interfaces. I had an activity with 6 fragments and i needed to update only 3 of them.

    I use this

        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();
            }
        }
    

    Where, PageAdapterOfe is my activity fragments adapter. I loop all of my fragments and search for those that implement my interface, when i found one, I execute the method defined by my interface and that is!

    I use this code inside the activity that holds all the fragments, in response a broadcast signal, you can put it where you need.

    The interface:

    public interface IOfertaFragment {
    
        public void actualizaListaOfertas();
    }
    
    0 讨论(0)
  • 2020-12-05 13:01

    If Fragment is included inside the layout file of Activity then it can be referenced by SupportFragmentManager like...

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

    If Fragment is included inside the layout file of another Fragment then it can be referenced by ChildFragmentManager like...

     MyFragment myFragment= (MyFragment)getChildFragmentManager().findFragmentById(R.id.FRAGMENTID)
    
    0 讨论(0)
  • 2020-12-05 13:05

    You can find the fragment using findFragmentById (if you know the component it is included in) or by findFragmentByTag (if you know its tag)

    I don't know which variables you want to update, but you can replace the fragment with another fragment using the FragmentTransaction API.

    See http://developer.android.com/guide/components/fragments.html for examples.

    0 讨论(0)
  • 2020-12-05 13:20

    If the fragment is embedded in another fragment, you need getChildFragmentManager() but not getFragmentManager(). For example, in layout xml define the fragment like this:

    <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" />
    

    in Code, you can get the fragment instance like this:

    FragmentManager f = getChildFragmentManager();
    FragmentTransaction transaction = f.beginTransaction();
    chatFragment = f.findFragmentById(R.id.chatfragment);
    
    if (chatFragment != null) {
       transaction.hide(chatFragment);
    }
    transaction.commit();
    
    0 讨论(0)
  • 2020-12-05 13:23

    You can get the fragment instance as follows:

    getSupportFragmentManager().findFragmentById(R.id.yourFragmentId)
    
    0 讨论(0)
提交回复
热议问题