1.在第一个fragment中开启事务,设置标记
Toast.makeText(getActivity(), "切换到下一个fragment中", Toast.LENGTH_SHORT).show(); //开启事务跳转 FragmentTransaction transaction = getFragmentManager().beginTransaction(); String textItem = ((TextView) view).getText().toString(); ProduceDetailFragment produceDetailFragment = new ProduceDetailFragment(); Bundle bundle = new Bundle(); bundle.putString("productTitle", textItem); produceDetailFragment.setArguments(bundle); transaction .addToBackStack(null) //将当前fragment加入到返回栈中 .replace(R.id.fl_main_fragment,produceDetailFragment) .show(produceDetailFragment) .commit();
2.在第二个里面
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_produce, container, false); ButterKnife.bind(this, view); //设置公共标题 setTitle(); initData(); return view; }
3.获取数据
/*--------------设置公共标题-------------*/ private void setTitle() { title = getArguments().getString("productTitle"); tvCustomTitle.setText(title); btnClose.setText("返回"); btnSearch.setVisibility(View.GONE); }
4.返回到上一个fragment
@OnClick(R.id.btn_close) public void onClick() { getFragmentManager().popBackStack(); }
5. 设置fragment的跳转动画
transaction.setCustomAnimations(R.anim.enter,R.anim.exit,android.R.anim.slide_in_left,android.R.anim.slide_out_right);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
popexit.xml,popenter.xml是系统自带的