How to pass data between fragments

后端 未结 13 2772
醉话见心
醉话见心 2020-11-22 07:03

Im trying to pass data between two fragmens in my program. Its just a simple string that is stored in the List. The List is made public in fragments A, and when the user cli

相关标签:
13条回答
  • 2020-11-22 07:32

    I'm working on a similar project and I guess my code may help in the above situation

    Here is the overview of what i'm doing

    My project Has two fragments Called "FragmentA" and "FragmentB"

    -FragmentA Contains one list View,when you click an item in FragmentA It's INDEX is passed to FragmentB using Communicator interface

    • The design pattern is totally based on the concept of java interfaces that says "interface reference variables can refer to a subclass object"
    • Let MainActivity implement the interface provided by fragmentA(otherwise we can't make interface reference variable to point to MainActivity)
    • In the below code communicator object is made to refer to MainActivity's object by using "setCommunicator(Communicatot c)" method present in fragmentA.
    • I'm triggering respond() method of interface from FrgamentA using the MainActivity's reference.

      Interface communcator is defined inside fragmentA, this is to provide least access previlage to communicator interface.

    below is my complete working code

    FragmentA.java

    public class FragmentA extends Fragment implements OnItemClickListener {
    
    ListView list;
    Communicator communicater;
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragmenta, container,false);
    }
    
    public void setCommunicator(Communicator c){
        communicater=c;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        communicater=(Communicator) getActivity();
        list = (ListView) getActivity().findViewById(R.id.lvModularListView);
        ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.items, android.R.layout.simple_list_item_1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
    
    }
    
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
    communicater.respond(index);
    
    }
    
    public interface Communicator{
        public void respond(int index);
    }
    

    }

    fragmentB.java

    public class FragmentA extends Fragment implements OnItemClickListener {
    
    ListView list;
    Communicator communicater;
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragmenta, container,false);
    }
    
    public void setCommunicator(Communicator c){
        communicater=c;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        communicater=(Communicator) getActivity();
        list = (ListView) getActivity().findViewById(R.id.lvModularListView);
        ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.items, android.R.layout.simple_list_item_1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(this);
    
    }
    
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
    communicater.respond(index);
    
    }
    
    public interface Communicator{
        public void respond(int index);
    }
    
    }
    

    MainActivity.java

    public class MainActivity extends Activity implements FragmentA.Communicator {
    FragmentManager manager=getFragmentManager();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        FragmentA fragA=(FragmentA) manager.findFragmentById(R.id.fragmenta);
        fragA.setCommunicator(this);
    
    
    }
    
    @Override
    public void respond(int i) {
        // TODO Auto-generated method stub
    
    FragmentB FragB=(FragmentB) manager.findFragmentById(R.id.fragmentb);
    FragB.changetext(i);
    }
    
    
    
    }
    
    0 讨论(0)
提交回复
热议问题