Get view of Fragment from FragmentActivity

前端 未结 5 1748
一个人的身影
一个人的身影 2021-01-01 10:09

I have a FragmentActivity where I add a Fragment. Depending on some situation, I want to access Buttons from that fragment\'s layout

5条回答
  •  一个人的身影
    2021-01-01 10:52

    To set OnClickListeners in fragments, override onViewCreated in your fragment class.

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.yourId).setOnClickListener(this);
        //or
        getActivity().findViewById(R.id.yourId).setOnClickListener(this);
    }
    

    I was trying to set a timertask to automatically reload a webview in a fragment and this method was the only thing that worked for me. My project is on github.

    Source: this answer

提交回复
热议问题