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是系統自帶的