Fragment之间传值与跳转

一:自定义一个接口

第一个id是你要跳转到那个Fragment的索引,比如说一共有三个Fragment你要跳到第二个Fragment   id就=1  索引从0开始。

第二个参数是你要传入的数据   或者是对象  集合  都可以

我一共有三个Fragment

private BlankFragment blankFragment;
private BlankFragment2 blankFragment2;
private BlankFragment3 blankFragment3;

public interface Myonchoelin {
    void Claont(int id,String url);
}

二:在Fragment里面调用

private Myonchoelin myonchoelin;
//需要重写set方法
public void setMyonchoelin(Myonchoelin myonchoelin) {
    this.myonchoelin = myonchoelin;
}
点击事件里面

shiFragement.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        String url = results.get(groupPosition).getChildren().get(childPosition).getUrl();
        myonchoelin.Claont(2,url);
        return true;
    }
});

三:在锁定Fragment的MainActivity里面

blankFragment2.setMyonchoelin(this);

然后重写this里面的方法

@Override
public void Claont(int id, String url) {
    //用bundle传值
    Bundle bundle = new Bundle();
    bundle.putString("url",url);
    blankFragment3.setArguments(bundle);
    main1Viewpager.setCurrentItem(id);
}

四:在你跳转到的Fragment里面重写setUserVisibleHint();方法

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser){
        if (getArguments()!=null){
            String url = getArguments().getString("url");
            Log.d("BlankFragment3", url);
//这只是个视频播放器
            JZVideoPlayerStandard jzVideoPlayerStandard = (JZVideoPlayerStandard)inflate.findViewById(R.id.videoplayer);
            jzVideoPlayerStandard.setUp(url
                    , JZVideoPlayerStandard.SCREEN_WINDOW_NORMAL, "");
        }
    }
}
完成

你可能感兴趣的:(Fragment之间传值与跳转)