Implementing multiple fragments in a single activity Dynamically

后端 未结 3 545
北海茫月
北海茫月 2020-12-04 09:15

I am working on fragments


Use case i am trying to implement::

  • I am using dynamic fragments
  • I am using three fragments
3条回答
  •  孤城傲影
    2020-12-04 09:42

    Communication between Fragments

    There can be many scenarios where communication between fragment is required. You need to pass data between fragments on button click event. You may also use Android toolbar to switch between fragments. When you add buttons to your toolbar, you need to dynamically change screen using fragment.

    Create an interface which will help us to communicate

    Communicate.java

    package com.example.amaanmemon.testfragment;
    
    interface Communicate {
     public void sendData();
    }
    

    TopFragment.java

    package com.example.amaanmemon.testfragment;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.EditText;
    
    public class TopFragment extends Fragment {
    
    EditText  firstName;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    
        View view=inflater.inflate(R.layout.my_fragment1, container, false);
        return view;
    }
    
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        firstName = (EditText) getActivity().findViewById(R.id.editText1);
    }
    
    public String getData(){
        return firstName.getText().toString();
    }
    }
    

    MiddleFragment.java

    package com.example.amaanmemon.testfragment;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    public class MiddleFragment extends Fragment implements OnClickListener{
    
    View view;
    Button btn;
    Communicate cm;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        view=inflater.inflate(R.layout.my_fragment2, container, false);
        return  view;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        cm = (Communicate) getActivity();
        btn = (Button) getActivity().findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        cm.sendData();
    }
    }
    

    BottomFragment.java

    package com.example.amaanmemon.testfragment;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class BottomFragment extends Fragment{
    
    int count;
    View view;
    TextView display_text;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        view=inflater.inflate(R.layout.my_fragment3, container,false);
        return view;
    }
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        display_text = (TextView)getActivity().findViewById(R.id.textView1);
    }
    
    public void incrementData(String displayText){
        display_text.setText(displayText);
    }
    }
    

    MainActivity.java

    package com.example.amaanmemon.testfragment;
    
    import android.support.v4.app.FragmentActivity;
    import android.os.Bundle;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    
    public class MainActivity extends FragmentActivity implements Communicate{
    
    TopFragment frg;
    MiddleFragment frg1;
    BottomFragment frg2;
    FragmentTransaction transaction;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        frg = new TopFragment();
        frg1 = new MiddleFragment();
        frg2 = new BottomFragment();
    
        FragmentManager manager=getSupportFragmentManager();
        transaction=manager.beginTransaction();
        transaction.add(R.id.My_Container_1_ID, frg, "Frag_Top_tag");
        transaction.add(R.id.My_Container_2_ID, frg1, "Frag_Middle_tag");
        transaction.add(R.id.My_Container_3_ID, frg2, "Frag_Bottom_tag");
        transaction.commit();
    }
    
    @Override
    public void sendData() {
        String temp = frg.getData();
        frg2.incrementData(temp);
    }
    }
    

    You can copy xml files from question. you can watch the output below.

提交回复
热议问题