两个fragment之间简单的跳转

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是系统自带的

转载于:https://www.cnblogs.com/fanfusuzi/p/7017833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值