fragment给fragment传值2

昨天学习了两个fragment之间传值的一种方法,即通过Activity传值。今天来看一下来着之间直接传值的两种方法。
布局文件和上次的布局文件一样,在mainactivity中静态添加一个fragment1,fragment1中包含有一个EditText和一个button,fragment2中有一个TextView。
在fragment1中的onCreatView函数中,找到两个控件,在button的事件监听函数中,通过 getframentManger().findViewByFragment()来找到fragment2,并对它进行赋值通过调用Fragment-two中的方法对fragment2中的控件进行赋值。在MainActivity中的onCreate中来生成一个fragment2.

public class Fragment_one extends Fragment {
    private Context mcontext;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mcontext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1,container,false);
        final EditText editText = view.findViewById(R.id.editText);
        Button button = view.findViewById(R.id.buttonEd);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
           Fragment_two fragment_two = (Fragment_two) getFragmentManager().findFragmentById(R.id.frag_2);//找到fragment2
            fragment_two.setData(editText.getText().toString());//赋值
              
            }
        });
        return view;
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//加载fragment2
        Fragment_two fragment_two = new Fragment_two();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.frag_2,fragment_two);
        fragmentTransaction.commit();
    }
}
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2, container, false);
        textView = view.findViewById(R.id.textView);
        return view;
    }

    public void setData(String src) {
        textView.setText(src);//对textview进行赋值
    }
}

还有一种传值方式与上一种极为相似,只是不用找fragment2,而是直接找fragment2中的TextView这个控件。
在fragment1中的OnCreateView函数中的button监听事件中,直接找到TextView这个控件。TextView textView = getActivity().findViewById();

public class Fragment_one extends Fragment {
    private Context mcontext;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mcontext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1,container,false);
        final EditText editText = view.findViewById(R.id.editText);
        Button button = view.findViewById(R.id.buttonEd);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
                TextView textView = getActivity().findViewById(R.id.textView);
                textView.setText(editText.getText().toString());
            }
        });
        return view;
    }
}

你可能感兴趣的:(fragment给fragment传值2)