fragment触发事件后传递会给activity,可以通过在fragment中定义一个接口,让activity实现这个接口。
具体代码如下
public class AAFragment extends Fragment { private OnFragmentInteractionListener mListener; public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface OnFragmentInteractionListener { void onFragmentInteraction(Uri uri); } }
只要activity实现OnFragmentInteractionListener这个接口,在fragment中调用onFragmentInteraction,就能将事件传递给activity。