Fragment操作宿主Activity

宿主 Activity 实现 Fragment 定义的对外接口 IOneFragmentClickListener,便可以实现 Fragment 调用 Activity 的功能。

public class OneFragment extends Fragment implements View.OnClickListener{

    private IOneFragmentClickListener clickListener;

    public interface IOneFragmentClickListener{
        void onOneFragmentClick();
    }

    public void setClickListener(IOneFragmentClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_one, null);
        contentView.findViewById(R.id.edt_one).setOnClickListener(this);
        return contentView;
    }

    @Override
    public void onClick(View v) {
        clickListener.onOneFragmentClick();
    }

}

需要在宿主 Activity 中额外添加一步监听设置。

public class MainActivity extends Activty implements IOneFragmentClickListener {

    @Overide
    public void onOneFragmentClick(){
        //TODO
    }
}
I Don't Want Your Money, I Want Aragaki Yui.