Get view of Fragment from FragmentActivity

前端 未结 5 1683
一个人的身影
一个人的身影 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:44

    Simply Use the getRootView() method to get the data of your fragment in Activity.

    //below block of code write in activity.
    
    EditText et = (EditText) v.getRootView().findViewById(R.id.name);
    Log.d("Name", et.getText().toString());
    

    name is the field of fragment.

    0 讨论(0)
  • 2021-01-01 10:45

    try this:

    public class YourFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              return inflater.inflate(R.layout.your_fragment, null);
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            View view = getView();
            if(view != null) {
                view.findViewById(R.id.my_button).setOnClickListener(new MyClickListener());
            }
        }
    }
    

    this code must works fine. I hope, this will help you.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-01 10:56

    If you want to access your component and set some data, I suggest you to make a method inside the fragment like this:

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    public class DetailFragment extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.details, container, false);
            return view;
        }
    
    
        public void setSettings(){
            Button button = (Button) getView().findViewById(R.id.my_button);
            button.setOnClickListener(new MyClickListener());
        }
    }
    
    0 讨论(0)
  • 2021-01-01 10:59

    When you initialize your button inside a class that extends Fragment it's better to do it inside the onViewCreated() method. This will guarantee that the view already exists.

    0 讨论(0)
提交回复
热议问题