其他分享
首页 > 其他分享> > Android中的Fragment

Android中的Fragment

作者:互联网

参考文章

1、create fragment

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragmentManager = getChildFragmentManager();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frg_my, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        textView = getActivity().findViewById(R.id.id_tv_my);
        button = getActivity().findViewById(R.id.id_btn_my);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("TAG", "------ button was clicked !");
            }
        });
    }

2、add/show和replace的区别

add/show切换fragment不会让fragment重新创建,保持之前的那个fragment将fragment隐藏了而不是销毁再创建
replace切换fragment会使fragment重新创建, 不会保持之前的那个fragment,每次都是重新创建
private FragmentManager fragmentManager = getSupportFragmentManager();
private FragmentManager fragmentManager = getChildFragmentManager();

1)add/show

	/**
     * add show方式
     */
    private void addShowFragmentOne() {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (myLeftOneFragment == null) {
            myLeftOneFragment = MyLeftOneFragment.newInstance();
            fragmentTransaction.add(R.id.id_fg_left, myLeftOneFragment);
        }
        hideAllFragment(fragmentTransaction);
        fragmentTransaction.show(myLeftOneFragment);
        fragmentTransaction.commit();
    }
	/**
     * 隐藏 fragment
     *
     * @param fragmentTransaction
     */
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (myLeftOneFragment != null) {
            fragmentTransaction.hide(myLeftOneFragment);
        }
        if (myLeftTwoFragment != null) {
            fragmentTransaction.hide(myLeftTwoFragment);
        }
    }

2)replace

	/**
     * replace
     */
    private void replaceFragmentOne() {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (myLeftOneFragment == null) {
            myLeftOneFragment = MyLeftOneFragment.newInstance();
        }
        fragmentTransaction.replace(R.id.id_fg_left, myLeftOneFragment);
        fragmentTransaction.commit();
    }

3、Fragment生命周期

在这里插入图片描述

onAttach()Fragment和Activity建立关联的时候调用
onCreateView()为Fragment加载布局时调用
onActivityCreated()当Activity中的onCreate方法执行完后调用
onDestroyView()Fragment中的布局被移除时调用
onDetach()Fragment和Activity解除关联的时候调用

4、Fragment之间通信

getActivity()方法可以让Fragment获取到关联的Activity,然后再调用Activity的findViewById()方法,就可以获取到和这个Activity关联的其它Fragment的视图。

public class Fragment2 extends Fragment {
 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment2, container, false);
	}
 
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		//frg2的 Button
		Button btn_frg2 = getActivity().findViewById(R.id.id_btn_frg2);
		//frg1的 TextView
		TextView tv_frg1 = getActivity().findViewById(R.id.id_tv_frg1);
		//点击 frg2中的Button, 显示 frg1中TextView的内容
		btn_frg2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view){
				Toast.makeText(getActivity(), tv_frg1.getText(), Toast.LENGTH_LONG).show();
			}
		});
	}
 
}

标签:Fragment,void,fragment,Android,fragmentTransaction,id,myLeftOneFragment
来源: https://blog.csdn.net/weixin_41733225/article/details/114288566