Activity And Fragment Interaction

前端 未结 7 1271
南旧
南旧 2020-12-10 12:54

I have an Activity with multiple Fragments. I want to show a DialogFragment or open another Fragment from one of the

7条回答
  •  無奈伤痛
    2020-12-10 13:43

    You will need to pass your data from Fragment X up to your FragmentActivity which will pass this data on to your Fragment Y. You do that by way of an interface defined in your fragment class and instantiate a callback that is defined in onAttach().

    More information on how to do this here Communication With other Fragments

    Quick example, consider Fragment A and Fragment B. Fragment A is a list fragment and whenever an item is selected it will change what is displayed in Fragment B. Simple enough, right?

    At first, define Fragment A as such.

     public class FragmentA extends ListFragment{
    
       //onCreateView blah blah blah
    
    }
    

    And here's Fragment B

    public class FragmentB extends Fragment{
    
     //onCreateView blah blah blah
    
    }
    

    And here's my FragmentActivity that will govern them both

    public class MainActivity extends FragmentActivity{
    
    //onCreate 
    //set up your fragments
    
    }
    

    Presumably you have something like this already, now here's how you would change FragmentA(the list fragment that we need to get some data from).

        public class FragmentA extends ListFragment implements onListItemSelectedListener, onItemClickListener{
    
    OnListItemSelectedListener mListener;
    
       //onCreateView blah blah blah
    
    
    
     // Container Activity must implement this interface
        public interface OnListItemSelectedListener {
        public void onListItemSelected(int position);
    }
    
    
    }
    
    
      @Override
      public void onAttach(Activity activity) {
        super.onAttach(activity);
    
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mListener = (OnListItemSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnListItemSelectedListener");
        }
    }
    
    
      @Override 
     public void onItemClick(AdapterView parent, View view, int position, long id){
    
    
     //Here's where you would get the position of the item selected in the list, and  pass    that position(and whatever other data you need to) up to the activity 
    //by way of the interface you defined in onAttach
    
      mListener.onListItemSelected(position);
    
    
    }
    

    The most important consideration here is that your parent Activity implements this interface, or else you will get an exception. If implemented successfully, everytime an item in your list fragment is selected, your Activity will be notified of it's position. Obviously you could alter your interface with any number or type of parameters, in this example we're just passing in our integer position. Hope this clarifies a bit man, good luck.

提交回复
热议问题