Fragment-Fragment communication in Android

前端 未结 6 1203
太阳男子
太阳男子 2020-11-28 10:46

I am at beginner level in Android programming, so I need your sincere help for this. Anyone help me please.

I am trying to build a SLIDING UI using fragments

6条回答
  •  情歌与酒
    2020-11-28 11:40

    It should be done thought listener, so Fragments are still not depend on each other and can be used in one or two pane mode. Activity should handle listeners of both fragments.

    Here is an example of activity with two Fragments:

    package com.example;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    
    import com.example.fragment.FragmentA;
    import com.example.fragment.FragmentA.TextChangeListener;
    import com.example.fragment.FragmentB;
    
    public class ActivityAB extends FragmentActivity {
    
        FragmentA fragmentA;
        FragmentB fragmentB;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ab);
    
            FragmentManager manager = getSupportFragmentManager();
            fragmentA = (FragmentA) manager.findFragmentById(R.id.fragmentA);
            fragmentB = (FragmentB) manager.findFragmentById(R.id.fragmentB);
    
            fragmentA.setTextChangeListener(new TextChangeListener() {
    
                @Override
                public void onTextChange(CharSequence newText) {
                    fragmentB.updateTextValue(newText);
                }
            });
        }
    
    }
    

    Here is Fragment A, that has custom listener for text change event.

    package com.example.fragment;
    
    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;
    
    import com.example.R;
    
    public class FragmentA extends Fragment {
    
        TextChangeListener listener;
    
        public interface TextChangeListener {
            public void onTextChange(CharSequence newText);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_a, container, false);
    
            Button btn = (Button) view.findViewById(R.id.button1);
            final TextView textView = (TextView) view.findViewById(R.id.textView1);
    
            btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    if (null != listener) {
                        listener.onTextChange(textView.getText());
                    }
    
                }
            });
            return view;
        }
    
        public void setTextChangeListener(TextChangeListener listener) {
            this.listener = listener;
        }
    }
    

    Here is Fragment B that has public method to update text field:

    package com.example.fragment;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import com.example.R;
    
    public class FragmentB extends Fragment {
    
        TextView textView;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_b, container, false);
            textView = (TextView) view.findViewById(R.id.textView1);
            return view;
        }
    
        public void updateTextValue(CharSequence newText) {
            textView.setText(newText);
        }
    }
    

    ActivityAB xml Layout:

    
    
    
        
    
        
    
    
    

    Fragment A xml layout:

    
    
    
        
    
        

    Fragment B xml layout:

    
    
    
        
    
    
    

提交回复
热议问题